

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

# 步驟 1：建立 Amazon S3 儲存貯體
<a name="using-s3-bucket"></a>

**重要**  
 AWS OpsWorks Stacks 此服務已於 2024 年 5 月 26 日終止，並已針對新客戶和現有客戶停用。我們強烈建議客戶盡快將其工作負載遷移至其他解決方案。如果您對遷移有任何疑問，請透過 [AWS re：Post](https://repost.aws/) 或透過 [AWS Premium Support](https://aws.amazon.com/support) 聯絡 AWS 支援 團隊。

您必須先建立 Amazon S3 儲存貯體。您可以使用 Amazon S3 主控台、API 或 CLI 直接執行此操作，但建立資源的簡單方式通常是使用 CloudFormation 範本。下列範本會為此範例建立 Amazon S3 儲存貯體，並使用 [IAM 角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/WorkingWithRoles.html)設定[執行個體描述](https://docs.aws.amazon.com/IAM/latest/UserGuide/instance-profiles.html)檔，以授予儲存貯體不受限制的存取權。然後，您可以使用 layer 設定，將執行個體描述檔連接到堆疊的應用程式伺服器執行個體，讓應用程式存取儲存貯體，如稍後所述。執行個體描述檔的實用性不限於 Amazon S3；它們對於整合各種 AWS 服務非常有用。

```
{
   "AWSTemplateFormatVersion" : "2010-09-09",
   "Resources" : {
      "AppServerRootRole": {
         "Type": "AWS::IAM::Role",
         "Properties": {
            "AssumeRolePolicyDocument": {
               "Statement": [ {
                  "Effect": "Allow",
                  "Principal": {
                     "Service": [ "ec2.amazonaws.com" ]
                  },
                  "Action": [ "sts:AssumeRole" ]
               } ]
            },
            "Path": "/"
         }
      },
      "AppServerRolePolicies": {
         "Type": "AWS::IAM::Policy",
         "Properties": {
            "PolicyName": "AppServerS3Perms",
            "PolicyDocument": {
               "Statement": [ {
                  "Effect": "Allow",
                  "Action": "s3:*",
                  "Resource": { "Fn::Join" : ["", [ "arn:aws:s3:::", { "Ref" : "AppBucket" } , "/*" ]
                  ] }
               } ]
            },
            "Roles": [ { "Ref": "AppServerRootRole" } ]
         }
      },
      "AppServerInstanceProfile": {
         "Type": "AWS::IAM::InstanceProfile",
         "Properties": {
            "Path": "/",
            "Roles": [ { "Ref": "AppServerRootRole" } ]
         }
      },
     "AppBucket" : {
      "Type" : "AWS::S3::Bucket"
      }
   },
   "Outputs" : {
       "BucketName" : {
           "Value" : { "Ref" : "AppBucket" }
       },
       "InstanceProfileName" : {
           "Value" : { "Ref" : "AppServerInstanceProfile" }
       }
   }
}
```

當您啟動範本時會發生幾種情況：
+ `[AWS::S3::Bucket](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket.html)` 資源會建立 Amazon S3 儲存貯體。
+ `[AWS::IAM::InstanceProfile](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iam-instanceprofile.html)` 資源會建立執行個體描述檔，以指派給應用程式伺服器執行個體。
+ `[AWS::IAM::Role](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iam-role.html)` 資源會建立執行個體描述檔的角色。
+ `[AWS::IAM::Policy](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-iam-policy.html)` 資源會設定角色的許可，以允許不受限制地存取 Amazon S3 儲存貯體。
+ 在您啟動範本之後， CloudFormation 主控台中的 `Outputs` 區段會顯示儲存貯體和執行個體描述檔名稱。

  您需要這些值來設定堆疊和應用程式。

如需如何建立 CloudFormation 範本的詳細資訊，請參閱[了解範本基本概念](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/gettingstarted.templatebasics.html)。

**建立 Amazon S3 儲存貯體**

1. 將範例範本複製到您系統中的文字檔案。

   此範例假設檔案名稱為 `appserver.template`。

1. 開啟 [CloudFormation](https://console.aws.amazon.com/cloudformation/) 主控台，然後選擇 **Create Stack (建立堆疊)**。

1. 在 **Stack Name (堆疊名稱)** 方塊中，輸入堆疊名稱。

   此範例假設名稱為 **AppServer**。

1. 依序選擇 **Upload template file (上傳範本檔案)** 和 **Browse (瀏覽)**，並選取您在步驟 1 中建立的 `appserver.template` 檔案，然後選擇 **Next Step (下一步)**。

1. 在 **Specify Parameters (指定參數)** 頁面中，選取 **I acknowledge that this template may create IAM resources (我知道此範本可能會建立 IAM 資源)**，然後選擇每個精靈頁面的 **Next Step (下一步)**，直到完成為止。選擇**建立**。

1. 在 **AppServer** 堆疊達到 **CREATE\$1COMPLETE** 狀態之後，請選取它，然後選擇 **Outputs (輸出)** 標籤。

   您可能需要重新整理幾次以更新狀態。

1. 在 **Outputs (輸出)** 標籤中，記錄 **BucketName** 和 **InstanceProfileName** 值，供日後使用。

**注意**  
CloudFormation 使用 *堆疊*一詞來參考從範本建立的資源集合；它與 OpsWorks Stacks 堆疊不同。