

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

# 成功將 S3 儲存貯體匯入為 AWS CloudFormation 堆疊
<a name="successfully-import-an-s3-bucket-as-an-aws-cloudformation-stack"></a>

*Ram Kandaswamy，Amazon Web Services*

## 總結
<a name="successfully-import-an-s3-bucket-as-an-aws-cloudformation-stack-summary"></a>

如果您使用 Amazon Web Services (AWS) 資源，例如 Amazon Simple Storage Service (Amazon S3) 儲存貯體，並想要使用基礎設施做為程式碼 (IaC) 方法，則可以將資源匯入 AWS CloudFormation，並將其做為堆疊管理。

此模式提供將 S3 儲存貯體成功匯入為 AWS CloudFormation 堆疊的步驟。透過使用此模式的方法，您可以避免在單一動作中匯入 S3 儲存貯體時可能發生的錯誤。

## 先決條件和限制
<a name="successfully-import-an-s3-bucket-as-an-aws-cloudformation-stack-prereqs"></a>

**先決條件**
+ 作用中的 AWS 帳戶
+ 現有的 S3 儲存貯體和 S3 儲存貯體政策。如需詳細資訊，請參閱 [AWS 知識中心中的我應該使用哪些 S3 儲存貯體政策來符合 AWS Config 規則 s3-bucket-ssl-requests-onlyAWS Config](https://aws.amazon.com/premiumsupport/knowledge-center/s3-bucket-policy-for-config-rule/)。
+ 現有的 AWS Key Management Service (AWS KMS) 金鑰及其別名。如需詳細資訊，請參閱 AWS KMS 文件中的[使用別名](https://docs.aws.amazon.com/kms/latest/developerguide/programming-aliases.html)。
+ 下載到本機電腦的範例 `CloudFormation-template-S3-bucket` AWS CloudFormation 範本 （已連接）。

## Architecture
<a name="successfully-import-an-s3-bucket-as-an-aws-cloudformation-stack-architecture"></a>

![使用 CloudFormation 範本建立 CloudFormation 堆疊以匯入 S3 儲存貯體的工作流程。](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/aea7f6fe-8e67-46c4-8b90-1ab06b879111/images/ee143374-a0a4-42d9-b7ca-16593a597a84.png)


 

該圖顯示以下工作流程：

1. 使用者會建立 JSON 或 YAML 格式的 AWS CloudFormation 範本。

1. 範本會建立 AWS CloudFormation 堆疊來匯入 S3 儲存貯體。

1. AWS CloudFormation 堆疊會管理您在範本中指定的 S3 儲存貯體。

**技術堆疊**
+ AWS CloudFormation
+ AWS Identity and Access Management (IAM)
+ AWS KMS
+ Amazon S3

 

**工具**
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) – AWS CloudFormation 可協助您以可預測且重複的方式建立和佈建 AWS 基礎設施部署。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) – IAM 是一種 Web 服務，可安全地控制對 AWS 服務的存取。
+ [AWS KMS](https://docs.aws.amazon.com/kms/latest/developerguide/overview.html) – AWS Key Management Service (AWS KMS) 是一種針對雲端擴展的加密和金鑰管理服務。
+ [Amazon S3](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) – Amazon Simple Storage Service (Amazon S3) 是網際網路的儲存體。

## 史詩
<a name="successfully-import-an-s3-bucket-as-an-aws-cloudformation-stack-epics"></a>

### 以 AWS CloudFormation 堆疊形式匯入具有 AWS KMS key型加密的 S3 儲存貯體
<a name="import-an-s3-bucket-with-kms-key-long--based-encryption-as-an-aws-cloudformation-stack"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立範本以匯入 S3 儲存貯體和 KMS 金鑰。 | 在本機電腦上，使用下列範例範本建立範本以匯入 S3 儲存貯體和 KMS 金鑰：<pre>AWSTemplateFormatVersion: 2010-09-09<br /><br />Parameters:<br /><br />  bucketName:<br /><br />    Type: String<br /><br />Resources:<br /><br />  S3Bucket:<br /><br />    Type: 'AWS::S3::Bucket'<br /><br />    DeletionPolicy: Retain<br /><br />    Properties:<br /><br />      BucketName: !Ref bucketName<br /><br />      BucketEncryption:<br /><br />        ServerSideEncryptionConfiguration:<br /><br />          - ServerSideEncryptionByDefault:<br /><br />              SSEAlgorithm: 'aws:kms'<br /><br />              KMSMasterKeyID: !GetAtt <br /><br />                - KMSS3Encryption<br /><br />                - Arn<br /><br />  KMSS3Encryption:<br /><br />    Type: 'AWS::KMS::Key'<br /><br />    DeletionPolicy: Retain<br /><br />    Properties:<br /><br />      Enabled: true<br /><br />      KeyPolicy: !Sub |-<br /><br />        {<br /><br />            "Id": "key-consolepolicy-3",<br /><br />            "Version": "2012-10-17",		 	 	 <br /><br />            "Statement": [<br /><br />                {<br /><br />                    "Sid": "Enable IAM User Permissions",<br /><br />                    "Effect": "Allow",<br /><br />                    "Principal": {<br /><br />                        "AWS": ["arn:aws:iam::${AWS::AccountId}:root"]<br /><br />                    },<br /><br />                    "Action": "kms:*",<br /><br />                    "Resource": "*"<br /><br />                }<br /><br />                }<br /><br />            ]<br /><br />        }<br /><br />      EnableKeyRotation: true</pre> | AWS DevOps | 
| 建立堆疊。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/successfully-import-an-s3-bucket-as-an-aws-cloudformation-stack.html) | AWS DevOps | 
| 建立 KMS 金鑰別名。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/successfully-import-an-s3-bucket-as-an-aws-cloudformation-stack.html)<pre>KMSS3EncryptionAlias:<br /><br />    Type: 'AWS::KMS::Alias'<br /><br />    DeletionPolicy: Retain<br /><br />    Properties: <br /><br />    AliasName: alias/S3BucketKey<br /><br />    TargetKeyId: !Ref KMSS3Encryption</pre>如需詳細資訊，請參閱 [AWS CloudFormation 文件中的 AWS CloudFormation 堆疊更新](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks.html)。 AWS CloudFormation   | AWS DevOps | 
| 更新堆疊以包含 S3 儲存貯體政策。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/successfully-import-an-s3-bucket-as-an-aws-cloudformation-stack.html)<pre>S3BucketPolicy:<br /><br />  Type: 'AWS::S3::BucketPolicy'<br /><br />  Properties:<br /><br />    Bucket: !Ref S3Bucket<br /><br />    PolicyDocument: !Sub |-<br /><br />      {<br /><br />                  "Version": "2008-10-17",		 	 	 <br /><br />                  "Id": "restricthttp",<br /><br />                  "Statement": [<br /><br />                      {<br /><br />                          "Sid": "denyhttp",<br /><br />                          "Effect": "Deny",<br /><br />                          "Principal": {<br /><br />                              "AWS": "*"<br /><br />                          },<br /><br />                          "Action": "s3:*",<br /><br />                          "Resource": ["arn:aws:s3:::${S3Bucket}","arn:aws:s3:::${S3Bucket}/*"],<br /><br />                          "Condition": {<br /><br />                              "Bool": {<br /><br />                                  "aws:SecureTransport": "false"<br /><br />                              }<br /><br />                          }<br /><br />                      }<br /><br />                  ]<br /><br />              }</pre>此 S3 儲存貯體政策具有拒絕陳述式，可限制不安全的 API 呼叫。  | AWS DevOps | 
| 更新金鑰政策。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/successfully-import-an-s3-bucket-as-an-aws-cloudformation-stack.html)如需詳細資訊，請參閱 AWS KMS 文件中的 [中的金鑰政策 AWS KMS](https://docs.aws.amazon.com/kms/latest/developerguide/key-policies.html)。 | AWS 管理員 | 
| 新增資源層級標籤。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/successfully-import-an-s3-bucket-as-an-aws-cloudformation-stack.html)<pre>Tags:<br /><br />  - Key: createdBy<br /><br />    Value: Cloudformation</pre> | AWS DevOps | 

## 相關資源
<a name="successfully-import-an-s3-bucket-as-an-aws-cloudformation-stack-resources"></a>
+ [將現有資源帶入 AWS CloudFormation 管理](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resource-import.html)
+ [AWS re：Invent 2017：深入探討 AWS CloudFormation](https://www.youtube.com/watch?v=01hy48R9Kr8) （影片）

## 附件
<a name="attachments-aea7f6fe-8e67-46c4-8b90-1ab06b879111"></a>

若要存取與本文件相關聯的其他內容，請解壓縮下列檔案： [attachment.zip](samples/p-attach/aea7f6fe-8e67-46c4-8b90-1ab06b879111/attachments/attachment.zip)