

終止支援通知：將於 2026 年 10 月 7 日 AWS 結束對 的支援 AWS Proton。2026 年 10 月 7 日之後，您將無法再存取 AWS Proton 主控台或 AWS Proton 資源。您部署的基礎設施將保持不變。如需詳細資訊，請參閱[AWS Proton 服務棄用和遷移指南](https://docs.aws.amazon.com/proton/latest/userguide/proton-end-of-support.html)。

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

# 元件 CloudFormation 範例
<a name="ag-components-example-cfn"></a>

以下是 AWS Proton 直接定義元件的完整範例，以及如何在 AWS Proton 服務中使用它。元件會佈建 Amazon Simple Storage Service (Amazon S3) 儲存貯體和相關的存取政策。服務執行個體可以參考此儲存貯體並使用它。儲存貯體名稱是以環境、服務、服務執行個體和元件的名稱為基礎，這表示儲存貯體會與延伸特定服務執行個體之元件範本的特定執行個體結合。開發人員可以根據此元件範本建立多個元件，為不同的服務執行個體和功能需求佈建 Amazon S3 儲存貯體。

此範例涵蓋撰寫各種必要的 CloudFormation 基礎設施做為程式碼 (IaC) 檔案，以及建立必要的 AWS Identity and Access Management (IAM) 角色。此範例透過擁有人員角色來將步驟分組。

## 管理員步驟
<a name="ag-components-example-cfn.admin"></a>

**讓開發人員搭配 服務使用元件**

1. 建立 AWS Identity and Access Management (IAM) 角色，以縮小直接定義在環境中執行的元件可以佈建的資源範圍。 稍後會 AWS Proton 擔任此角色，以在環境中佈建直接定義的元件。

   在此範例中，請使用下列政策：  
**Example 直接定義的元件角色**  

------
#### [ JSON ]

****  

   ```
   {
     "Version":"2012-10-17",		 	 	 
     "Statement": [
       {
         "Effect": "Allow",
         "Action": [
           "cloudformation:CancelUpdateStack",
           "cloudformation:CreateChangeSet",
           "cloudformation:DeleteChangeSet",
           "cloudformation:DescribeStacks",
           "cloudformation:ContinueUpdateRollback",
           "cloudformation:DetectStackResourceDrift",
           "cloudformation:DescribeStackResourceDrifts",
           "cloudformation:DescribeStackEvents",
           "cloudformation:CreateStack",
           "cloudformation:DeleteStack",
           "cloudformation:UpdateStack",
           "cloudformation:DescribeChangeSet",
           "cloudformation:ExecuteChangeSet",
           "cloudformation:ListChangeSets",
           "cloudformation:ListStackResources"
         ],
         "Resource": "arn:aws:cloudformation:*:{{123456789012}}:stack/AWSProton-*"
       },
       {
         "Effect": "Allow",
         "Action": [
           "s3:CreateBucket",
           "s3:DeleteBucket",
           "s3:GetBucket*",
           "iam:CreatePolicy",
           "iam:DeletePolicy",
           "iam:GetPolicy",
           "iam:ListPolicyVersions",
           "iam:DeletePolicyVersion"
         ],
         "Resource": "*",
         "Condition": {
           "ForAnyValue:StringEquals": {
             "aws:CalledVia": "cloudformation.amazonaws.com"
           }
         }
       }
     ]
   }
   ```

------

1. 當您建立或更新環境時，請提供您在上一個步驟中建立的角色。在 AWS Proton 主控台中，於**設定環境**頁面上指定**元件角色**。如果您使用的是 AWS Proton API 或 AWS CLI，請指定 [CreateEnvironment](https://docs.aws.amazon.com/proton/latest/APIReference/API_CreateEnvironment.html) 或 [UpdateEnvironment](https://docs.aws.amazon.com/proton/latest/APIReference/API_UpdateEnvironment.html) API 動作`componentRoleArn`的 。

1. 建立服務範本，其參照連接至服務執行個體的直接定義元件。

   此範例說明如何撰寫強大的服務範本，如果元件未連接到服務執行個體，則不會中斷。  
**Example 使用元件的服務 CloudFormation IaC 檔案**  

   ```
   # service/instance_infrastructure/cloudformation.yaml
   
   Resources: 
     TaskDefinition:
       Type: AWS::ECS::TaskDefinition
       Properties:
         TaskRoleArn: !Ref TaskRole
         ContainerDefinitions:
           - Name: '{{service_instance.name}}'
             # ...
             {% if service_instance.components.default.outputs | length > 0 %}
             Environment:
               {{ service_instance.components.default.outputs |
                   proton_cfn_ecs_task_definition_formatted_env_vars }}
             {% endif %}
   
     # ...
   
     TaskRole:
       Type: AWS::IAM::Role
       Properties:
         # ...
         ManagedPolicyArns:
           - !Ref BaseTaskRoleManagedPolicy
           {{ service_instance.components.default.outputs
               | proton_cfn_iam_policy_arns }}
   
     # Basic permissions for the task
     BaseTaskRoleManagedPolicy:
       Type: AWS::IAM::ManagedPolicy
       Properties:
         # ...
   ```

1. 建立新的服務範本次要版本，將直接定義的元件宣告為支援的元件。
   + **Amazon S3 中的範本套件** – 在 AWS Proton 主控台中，當您建立服務範本版本時，針對**支援的元件來源**，選擇**直接定義**。如果您使用的是 AWS Proton API 或 AWS CLI，請在 [CreateServiceTemplateVersion](https://docs.aws.amazon.com/proton/latest/APIReference/API_CreateServiceTemplateVersion.html) 或 [UpdateServiceTemplateVersion](https://docs.aws.amazon.com/proton/latest/APIReference/API_UpdateServiceTemplateVersion.html) API 動作的 `supportedComponentSources` 參數`DIRECTLY_DEFINED`中指定 。
   + **範本同步** – 將變更遞交至您的服務範本套件儲存庫，您可以在主要版本目錄中`supported_component_sources:`的 `.template-registration.yaml` 檔案中將 指定`DIRECTLY_DEFINED`為 項目。如需有關此檔案的詳細資訊，請參閱[同步服務範本](create-template-sync.md#create-template-sync-service-templates)。

1. 發佈新的服務範本次要版本。如需詳細資訊，請參閱[註冊和發佈範本](template-create.md)。

1. 請務必允許使用此服務範本之開發人員的 IAM 角色`proton:CreateComponent`中的 。

## 開發人員步驟
<a name="ag-components-example-cfn.dev"></a>

**將直接定義的元件與服務執行個體搭配使用**

1. 建立使用管理員使用元件支援建立之服務範本版本的服務。或者，更新其中一個現有的服務執行個體，以使用最新的範本版本。

1. 撰寫佈建 Amazon S3 儲存貯體和相關存取政策的元件 IaC 範本檔案，並將這些資源公開為輸出。  
**Example 元件 CloudFormation IaC 檔案**  

   ```
   # cloudformation.yaml
   
   # A component that defines an S3 bucket and a policy for accessing the bucket.
   Resources:
     S3Bucket:
       Type: AWS::S3::Bucket
       Properties:
         BucketName: '{{environment.name}}-{{service.name}}-{{service_instance.name}}-{{component.name}}'
     S3BucketAccessPolicy:
       Type: AWS::IAM::ManagedPolicy
       Properties:
         PolicyDocument:
           Version: "2012-10-17"		 	 	 
           Statement:
             - Effect: Allow
               Action:
                 - 's3:Get*'
                 - 's3:List*'
                 - 's3:PutObject'
               Resource: !GetAtt S3Bucket.Arn
   Outputs:
     BucketName:
       Description: "Bucket to access"
       Value: !GetAtt S3Bucket.Arn
     BucketAccessPolicyArn:
       Value: !Ref S3BucketAccessPolicy
   ```

1. 如果您使用 AWS Proton API 或 AWS CLI，請撰寫元件的資訊清單檔案。  
**Example 直接定義的元件資訊清單**  

   ```
   infrastructure:
     templates:
       - file: "cloudformation.yaml"
         rendering_engine: jinja
         template_language: cloudformation
   ```

1. 建立直接定義的 component. AWS Proton assume 元件角色，由管理員定義來佈建元件。

   在 AWS Proton 主控台的[元件](https://console.aws.amazon.com//proton/#/components)頁面上，選擇**建立元件**。在**元件設定**中，輸入**元件名稱**和選用的**元件描述**。針對**元件連接**，選擇**將元件連接至服務執行個體。**選取您的環境、服務和服務執行個體。針對**元件來源**，選擇 **CloudFormation**，然後選擇元件 IaC 檔案。
**注意**  
您不需要提供資訊清單，主控台會為您建立一個資訊清單。

   如果您使用的是 AWS Proton API 或 AWS CLI，請使用 [CreateComponent](https://docs.aws.amazon.com/proton/latest/APIReference/API_CreateComponent.html) API 動作。設定元件`name`和選用的 `description`。設定 `environmentName`、 `serviceName`和 `serviceInstanceName`。將 `manifest` `templateSource`和 設定為您所建立檔案的路徑。
**注意**  
當您指定服務和服務執行個體名稱時，可選擇性指定環境名稱。這兩者的組合在 AWS 您的帳戶中是唯一的，並且 AWS Proton 可以從服務執行個體判斷環境。

1. 更新您的服務執行個體以重新部署它。 AWS Proton 會使用轉譯服務執行個體範本中元件的輸出，讓您的應用程式能夠使用元件佈建的 Amazon S3 儲存貯體。