如何使用 AWSUtility::CloudFormation::CommandRunner 在 CloudFormation 堆栈中的资源之前或之后运行命令?

上次更新日期:2021 年 3 月 2 日

我想使用 AWSUtility::CloudFormation::CommandRunner 在 AWS CloudFormation 堆栈中的资源之前或之后运行命令。

解决方案

注意:如果您在运行 AWS 命令行界面 (AWS CLI) 命令时遇到错误,请确保您使用的是最新版的 AWS CLI。

要在 CloudFormation 堆栈中的资源之前或之后运行命令,请在 CloudFormation 模板中定义 AWSUtility::CloudFormation::CommandRunner 资源。

例如:

Resources:
    CommandRunner:
        Type: AWSUtility::CloudFormation::CommandRunner
        Properties: 
            Command: 'aws ssm get-parameter --name BucketName --region us-east-1 --query Parameter.Value --output text > /command-output.txt'
            Role: EC2-Role
            LogGroup: my-cloudwatch-log-group

重要提示:如果您尚未注册 AWSUtility::CloudFormation::CommandRunner 资源,请运行以下命令。register.sh 脚本使用 awsutility-cloudformation-commandrunner.zip 将资源注册到您的账户中。有关 register.sh 脚本可以执行哪些操作的更多信息,请参阅 AWS GitHub 上的用户安装步骤。

git clone https://github.com/aws-cloudformation/aws-cloudformation-resource-providers-awsutilities-commandrunner.git

cd aws-cloudformation-resource-providers-awsutilities-commandrunner

curl -LO https://github.com/aws-cloudformation/aws-cloudformation-resource-providers-awsutilities-commandrunner/releases/latest/download/awsutility-cloudformation-commandrunner.zip

./scripts/register.sh --set-default

重要提示:属性 Role(角色)应当为 AWS Identity and Access Management (IAM) 实例配置文件的名称,且与之关联的 IAM 角色与 Amazon Elastic Compute Cloud (Amazon EC2) 服务 (ec2.amazonaws.com) 之间有信任关系。属性 Role(角色)由 AWSUtility::CloudFormation::CommandRunner 资源担任,以运行命令。可选属性 LogGroup(如果指定)会将命令执行中的日志写入 Amazon CloudWatch 日志组。有关在模板中使用 AWSUtility::CloudFormation::CommandRunner 资源的更多信息,请参阅 AWS GitHub 上的 aws-cloudformation-resource-providers-awsutilities-commandrunner 存储库中的 README.mddocs/README.md

您必须在 AWS CLI 命令中包含 --region 选项。然后,您必须将命令的输出写入名为 /command-output.txt 的保留文件,如前面的代码示例所示。

您可以使用 Fn::GetAtt 引用命令的输出。例如:

S3Bucket: 
    Type: AWS::S3::Bucket
    Properties: 
        BucketName: !GetAtt CommandRunner.Output

要在具有逻辑名称为实例的资源之后运行命令,请在 AWSUtility::CloudFormation::CommandRunner 资源的定义中指定 DependsOn:Instance。例如:

Resources:
   CommandRunner:
      DependsOn: Instance
      Type: AWSUtility::CloudFormation::CommandRunner
      Properties:
         Command: aws s3 ls | sed -n 1p | cut -d " " -f3 > /command-output.txt
         LogGroup: my-cloudwatch-log-group
         Role: EC2-Role
   Instance:
      Type: AWS::EC2::Instance
      Properties:
         Image: ami-abcd1234

要在资源之前运行命令,请将 DependsOn 设置为该资源定义中的 AWSUtility::CloudFormation::CommandRunner 资源的逻辑名称。例如:

Resources:
   CommandRunner:
      Type: AWSUtility::CloudFormation::CommandRunner
      Properties:
         Command: aws s3 ls | sed -n 1p | cut -d " " -f3 > /command-output.txt
         LogGroup: my-cloudwatch-log-group
         Role: EC2-Role
   Instance:
      DependsOn: CommandRunner
      Type: AWS::EC2::Instance
      Properties:
         Image: ami-abcd1234

注意:在前面的示例中,sed -n 1p 仅打印 aws s3 ls 返回的响应中的第一行。为了获取存储桶名称,sed -n 1p 将响应传送到 cut -d " " -f3,这将选择在拆分由空格分隔的行之后创建的数组中的第三个元素。