

终止支持通知：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`中指定。
   + **模板同步** - 将更改提交到服务模板捆绑包存储库，其中，您将 `DIRECTLY_DEFINED` 指定为主要版本目录下面的 `.template-registration.yaml` 文件中的一个 `supported_component_sources:` 项目。有关此文件的更多信息，请参阅[同步服务模板](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. 编写一个组件 IaC 模板文件，该文件预置 Amazon S3 存储桶和相关访问策略，并将这些资源公开为输出。  
**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. 创建直接定义的组件。 AWS Proton 承担管理员为置备组件而定义的组件角色。

   在 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`。将 `templateSource` 和 `manifest` 设置为您创建的文件的路径。
**注意**  
在您指定服务和服务实例名称时，指定环境名称是可选的。这两者的组合在您的 AWS 账户中是独一无二的， AWS Proton 可以根据服务实例确定环境。

1. 更新您的服务实例以重新部署它。 AWS Proton 在呈现的服务实例模板中使用组件的输出，使您的应用程序能够使用该组件预配置的 Amazon S3 存储桶。