

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 以程式設計方式啟用 Security Lake
<a name="get-started-programmatic"></a>

本教學課程說明如何以程式設計方式啟用和開始使用 Security Lake。Amazon Security Lake API 可讓您以程式設計方式存取 Security Lake 帳戶、資料和資源。或者，您可以使用 AWS 命令列工具 - [AWS Command Line Interface](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html)或 [AWS Tools for PowerShell](https://docs.aws.amazon.com/powershell/latest/userguide/pstools-welcome.html) - 或 [AWS SDKs](https://aws.amazon.com/developertools/)來存取 Security Lake。

## 步驟 1：建立 IAM 角色
<a name="prerequisites"></a>

如果您以程式設計方式存取 Security Lake，則必須建立某些 AWS Identity and Access Management (IAM) 角色，才能設定您的資料湖。

**重要**  
如果您使用 Security Lake 主控台來啟用和設定 Security Lake，則不需要建立這些 IAM 角色。

如果您要採取下列一或多個動作，您必須在 IAM 中建立角色 （選擇連結以查看每個動作的 IAM 角色詳細資訊）：
+ [建立自訂來源](custom-sources.md#iam-roles-custom-sources) – 自訂來源是原生支援以外的來源 AWS 服務 ，可將資料傳送至 Security Lake。
+ [建立具有資料存取的訂閱](prereqs-creating-subscriber.md#iam-role-subscriber)者 – 具有許可的訂閱者可以直接從您的資料湖存取 S3 物件。
+ [建立具有查詢存取權的訂閱](prereqs-query-subscriber.md#iam-role-query-subscriber)者 – 具有許可的訂閱者可以使用 Amazon Athena 等服務從 Security Lake 查詢資料。
+ [設定彙總區域](add-rollup-region.md#iam-role-replication) – 彙總區域會合併來自多個 的資料 AWS 區域。

建立先前提及的角色後，請將 [https://docs.aws.amazon.com/security-lake/latest/userguide/security-iam-awsmanpol.html#security-iam-awsmanpol-AmazonSecurityLakeAdministrator](https://docs.aws.amazon.com/security-lake/latest/userguide/security-iam-awsmanpol.html#security-iam-awsmanpol-AmazonSecurityLakeAdministrator) AWS 受管政策連接至您用來啟用 Security Lake 的角色。此政策授予管理許可，允許委託人加入 Security Lake 並存取所有 Security Lake 動作。

連接 [https://docs.aws.amazon.com/security-lake/latest/userguide/security-iam-awsmanpol.html#security-iam-awsmanpol-AmazonSecurityLakeAdministrator](https://docs.aws.amazon.com/security-lake/latest/userguide/security-iam-awsmanpol.html#security-iam-awsmanpol-AmazonSecurityLakeAdministrator) AWS 受管政策，以建立您的資料湖或從 Security Lake 查詢資料。Security Lake 需要此政策，才能支援從來源接收之原始日誌和事件資料的擷取、轉換和載入 (ETL) 任務。

## 步驟 2：啟用 Amazon Security Lake
<a name="enable-service-programmatic"></a>

若要以程式設計方式啟用 Security Lake，請使用 Security Lake API [https://docs.aws.amazon.com/security-lake/latest/APIReference/API_CreateDataLake.html](https://docs.aws.amazon.com/security-lake/latest/APIReference/API_CreateDataLake.html)的操作。如果您使用的是 AWS CLI，請執行 [create-data-lake](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/securitylake/create-data-lake.html) 命令。在您的請求中，使用 `configurations` 物件的 `region` 欄位來指定要啟用 Security Lake 之區域的區域代碼。如需區域代碼清單，請參閱《》中的 [Amazon Security Lake 端點](https://docs.aws.amazon.com/general/latest/gr/securitylake.html)*AWS 一般參考*。

**範例 1**

下列範例命令會在 `us-east-1`和 `us-east-2`區域中啟用 Security Lake。在這兩個區域中，此資料湖都會使用 Amazon S3 受管金鑰加密。物件會在 365 天後過期，而物件會在 60 天後轉換為 `ONEZONE_IA` S3 儲存類別。此範例已針對 Linux、macOS 或 Unix 格式化，並使用反斜線 (\$1) 行接續字元來改善可讀性。

```
$ aws securitylake create-data-lake \
--configurations '[{"encryptionConfiguration": {"kmsKeyId":"S3_MANAGED_KEY"},"region":"us-east-1","lifecycleConfiguration": {"expiration":{"days":365},"transitions":[{"days":60,"storageClass":"ONEZONE_IA"}]}}, {"encryptionConfiguration": {"kmsKeyId":"S3_MANAGED_KEY"},"region":"us-east-2","lifecycleConfiguration": {"expiration":{"days":365},"transitions":[{"days":60,"storageClass":"ONEZONE_IA"}]}}]' \
--meta-store-manager-role-arn "arn:aws:iam:us-east-1:123456789012:role/service-role/AmazonSecurityLakeMetaStoreManager"
```

**範例 2**

下列範例命令會在 區域中啟用 Security Lake`us-east-2`。此資料湖會使用在 AWS Key Management Service () 中建立的客戶受管金鑰進行加密AWS KMS。物件會在 500 天後過期，而物件會在 30 天後轉換為 `GLACIER` S3 儲存類別。此範例已針對 Linux、macOS 或 Unix 格式化，並使用反斜線 (\$1) 行接續字元來改善可讀性。

```
$ aws securitylake create-data-lake \
--configurations '[{"encryptionConfiguration": {"kmsKeyId":"1234abcd-12ab-34cd-56ef-1234567890ab"},"region":"us-east-2","lifecycleConfiguration": {"expiration":{"days":500},"transitions":[{"days":30,"storageClass":"GLACIER"}]}}]' \
--meta-store-manager-role-arn "arn:aws:iam:us-east-1:123456789012:role/service-role/AmazonSecurityLakeMetaStoreManager"
```

**注意**  
如果您已啟用 Security Lake，並想要更新區域或來源的組態設定，請使用 [https://docs.aws.amazon.com/security-lake/latest/APIReference/API_UpdateDataLake.html](https://docs.aws.amazon.com/security-lake/latest/APIReference/API_UpdateDataLake.html)操作，或使用 AWS CLI[update-data-lake](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/securitylake/update-data-lake.html) 命令。請勿使用 `CreateDataLake`操作。

## 步驟 3：設定來源
<a name="define-collection-objective-programmatic"></a>

Security Lake 會從各種來源以及您的 和 收集日誌 AWS 帳戶 和事件資料 AWS 區域。請依照這些指示來識別您希望 Security Lake 收集哪些資料。您只能使用這些指示來新增原生支援的 AWS 服務 作為來源。如需新增自訂來源的詳細資訊，請參閱 [從 Security Lake 中的自訂來源收集資料](custom-sources.md)。

若要以程式設計方式定義一或多個集合來源，請使用 Security Lake API 的 [CreateAwsLogSource](https://docs.aws.amazon.com/security-lake/latest/APIReference/API_CreateAwsLogSource.html) 操作。針對每個來源，指定 `sourceName` 參數的區域唯一值。選擇性地使用其他參數，將來源的範圍限制為特定帳戶 (`accounts`) 或特定版本 ()`sourceVersion`。

**注意**  
如果您未在請求中包含選用參數，Security Lake 會根據您排除的參數，將您的請求套用至指定來源的所有帳戶或所有版本。例如，如果您是組織的委派 Security Lake 管理員，而且您排除 `accounts` 參數，Security Lake 會將您的請求套用到組織中的所有帳戶。同樣地，如果您排除 `sourceVersion` 參數，Security Lake 會將您的請求套用至指定來源的所有版本。

如果您的請求指定您尚未啟用 Security Lake 的區域，則會發生錯誤。若要解決此錯誤，請確定`regions`陣列僅指定您已啟用 Security Lake 的區域。或者，您可以在 區域中啟用 Security Lake，然後再次提交您的請求。

當您第一次在帳戶中啟用 Security Lake 時，所有選取的日誌和事件來源都將成為 15 天免費試用期的一部分。如需用量統計資料的詳細資訊，請參閱 [檢閱用量和預估成本](reviewing-usage-costs.md)。

## 步驟 4：設定儲存設定和彙總區域 （選用）
<a name="define-target-objective-programmatic"></a>

您可以指定您希望 Security Lake 存放資料的 Amazon S3 儲存類別，以及存放資料的時間長度。您也可以指定彙總區域來合併來自多個區域的資料。這些是選用步驟。如需詳細資訊，請參閱[Security Lake 中的生命週期管理](lifecycle-management.md)。

若要在啟用 Security Lake 時以程式設計方式定義目標目標，請使用 Security Lake API [https://docs.aws.amazon.com/security-lake/latest/APIReference/API_CreateDataLake.html](https://docs.aws.amazon.com/security-lake/latest/APIReference/API_CreateDataLake.html)的操作。如果您已啟用 Security Lake 並想要定義目標目標，請使用 [https://docs.aws.amazon.com/security-lake/latest/APIReference/API_UpdateDataLake.html](https://docs.aws.amazon.com/security-lake/latest/APIReference/API_UpdateDataLake.html)操作，而非 `CreateDataLake`操作。

對於任一操作，請使用支援的參數來指定您想要的組態設定：
+ 若要指定彙總區域，請使用 `region` 欄位來指定您要將資料貢獻至彙總區域的區域。在 `replicationConfiguration` 物件`regions`陣列中，指定每個彙總區域的區域代碼。如需區域代碼清單，請參閱《》中的 [Amazon Security Lake 端點](https://docs.aws.amazon.com/general/latest/gr/securitylake.html)*AWS 一般參考*。
+ 若要指定資料的保留設定，請使用 `lifecycleConfiguration` 參數：
  + 針對 `transitions`，指定您要在特定 Amazon S3 儲存類別 (`days`) 中存放 S3 物件的總天數 (`storageClass`)。 Amazon S3 
  + 針對 `expiration`，指定建立物件後，使用任何儲存類別，在 Amazon S3 中儲存物件的總天數。當此保留期結束時，物件會過期，Amazon S3 會將其刪除。

  Security Lake 會將指定的保留設定套用至您在 `configurations` 物件的 `region` 欄位中指定的區域。

例如，下列命令會使用 建立資料湖`ap-northeast-2`做為彙總區域。`us-east-1` 區域會將資料貢獻至 `ap-northeast-2`區域。此範例也會為新增至資料湖的物件建立 10 天的過期期間。

```
$ aws securitylake create-data-lake \
--configurations '[{"encryptionConfiguration": {"kmsKeyId":"S3_MANAGED_KEY"},"region":"us-east-1","replicationConfiguration": {"regions": ["ap-northeast-2"],"roleArn":"arn:aws:iam::123456789012:role/service-role/AmazonSecurityLakeS3ReplicationRole"},"lifecycleConfiguration": {"expiration":{"days":10}}}]' \
--meta-store-manager-role-arn "arn:aws:iam::123456789012:role/service-role/AmazonSecurityLakeMetaStoreManager"
```

您現在已建立您的資料湖。使用 Security Lake API [https://docs.aws.amazon.com/security-lake/latest/APIReference/API_ListDataLakes.html](https://docs.aws.amazon.com/security-lake/latest/APIReference/API_ListDataLakes.html)的操作來驗證在每個區域中啟用 Security Lake 和您的資料湖設定。

如果在建立資料湖時發生問題或錯誤，您可以使用 [https://docs.aws.amazon.com/security-lake/latest/APIReference/API_ListDataLakeExceptions.html](https://docs.aws.amazon.com/security-lake/latest/APIReference/API_ListDataLakeExceptions.html)操作檢視例外狀況清單，並通知使用者 [https://docs.aws.amazon.com/security-lake/latest/APIReference/API_CreateDataLakeExceptionSubscription.html](https://docs.aws.amazon.com/security-lake/latest/APIReference/API_CreateDataLakeExceptionSubscription.html)操作的例外狀況。如需詳細資訊，請參閱[對資料湖狀態進行故障診斷](securitylake-data-lake-troubleshoot.md)。

## 步驟 5：檢視和查詢您自己的資料
<a name="explore-data-lake-programmatic"></a>

建立資料湖後，您可以使用 Amazon Athena 或類似服務來檢視和查詢 AWS Lake Formation 資料庫和資料表中的資料。當您以程式設計方式啟用 Security Lake 時，不會自動授予資料庫檢視許可。中的資料湖管理員帳戶 AWS Lake Formation 必須將`SELECT`許可授予您要用來查詢相關資料庫和資料表的 IAM 角色。角色至少必須具有*資料分析師*許可。如需許可層級的詳細資訊，請參閱 [Lake Formation 角色和 IAM 許可參考](https://docs.aws.amazon.com/lake-formation/latest/dg/permissions-reference.html)。如需授予`SELECT`許可的指示，請參閱《 *AWS Lake Formation 開發人員指南*》中的[使用具名資源方法授予資料目錄許可](https://docs.aws.amazon.com/lake-formation/latest/dg/granting-cat-perms-named-resource.html)。

## 步驟 6：建立訂閱者
<a name="subscribe-data-programmatic"></a>

建立資料湖之後，您可以新增訂閱者以取用您的資料。訂閱者可以透過直接存取 Amazon S3 儲存貯體中的物件或查詢資料湖來取用資料。如需訂閱者的詳細資訊，請參閱 [Security Lake 中的訂閱者管理](subscriber-management.md)。