Knowledge Center Monthly Newsletter - March 2025
Stay up to date with the latest from the Knowledge Center. See all new and updated Knowledge Center articles published in the last month and re:Post’s top contributors.
如何使用 AWSUtility::CloudFormation::CommandRunner 在 CloudFormation 堆栈中的某个资源之前或之后运行命令?
我想使用 AWSUtility::CloudFormation::CommandRunner 在 AWS CloudFormation 堆栈中的某个资源之前或之后运行命令。
解决方案
**注意:**如果您在运行 AWS 命令行界面 (AWS CLI) 命令时收到错误,请参阅 AWS CLI 错误故障排除。此外,请确保您使用的是最新版本的 AWS CLI。
注册 AWSUtility::CloudFormation::CommandRunner
如果您未注册 AWSUtility::CloudFormation::CommandRunner 资源,请运行以下命令进行注册:
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
register.sh 脚本将运行 AWS CLI 命令 register-type,并使用 awsutility-cloudformation-commandrunner.zip 在您的默认 AWS 区域中注册资源类型。要查看默认区域,请运行 aws configure get region 命令。有关 register.sh 脚本执行哪些操作的详细信息,请参阅 AWS GitHub 网站上的 User installation steps。
在 CloudFormation 模板中定义资源
要在 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
必须在 AWS CLI 命令中包含 --region 选项。然后,将命令的输出写入名为 /command-output.txt 的保留文件。
Role 属性必须为具有关联 IAM 角色的 AWS Identity and Access Management (IAM) 实例配置文件的名称。IAM 角色必须与 Amazon Elastic Compute Cloud (Amazon EC2) 服务 ec2.amazonaws.com 建立信任关系。为运行您的命令,AWSUtility::CloudFormation::CommandRunner 资源将代入 Role 属性。如果您指定 LogGroup 属性,则 LogGroup 会将命令执行中的日志写入 Amazon CloudWatch 日志组。
有关如何在模板中使用 AWSUtility::CloudFormation::CommandRunner 资源的详细信息,请参阅 GitHub 网站上的 README.md 和 docs/README.md。
使用 Fn::GetAtt 引用命令的输出。
示例模板代码片段:
S3Bucket: Type: AWS::S3::Bucket Properties: BucketName: !GetAtt CommandRunner.Output
要在逻辑名称为 Instance 的资源之后运行命令,请指定 DependsOn: Instance(在 AWSUtility::CloudFormation::CommandRunner 资源的定义中)。
示例模板:
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。然后,cut -d " " -f3 会选择在拆分由空格分隔的行之后创建的数组中的第三个元素。
相关信息

相关内容
- AWS 官方已更新 9 个月前
- AWS 官方已更新 2 年前
- AWS 官方已更新 3 年前
- AWS 官方已更新 2 年前