如何使用 AWSUtility::CloudFormation::CommandRunner 在我的 CloudFormation 堆疊中的資源之前或之後執行命令?

2 分的閱讀內容
0

我想要使用 AWSUtility::CloudFormation::CommandRunner,在我的 AWS CloudFormation 堆疊中的資源之前或之後執行命令。

解決方案

**注意事項:**如果您在執行 AWS Command Line Interface (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 以在您的 AWS 區域註冊資源類型。該指令碼會使用 register-type AWS CLI 命令在 AWS CLI 中設定的預設區域中註冊資源類型。您可以透過執行 aws configure get region 來檢查配置的預設區域。如需 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

重要事項:屬性角色應該是具有關聯 IAM 角色的 AWS Identity and Access Management (IAM) 執行個體設定檔的名稱。IAM 角色必須具有 Amazon Elastic Compute Cloud (Amazon EC2) 服務 (ec2.amazonaws.com) 的信任關係。該屬性角色AWSUtility::CloudFormation::CommandRunner 資源取得以執行您的命令。選用屬性 LogGroup (如果已指定),會將您命令執行中的日誌寫入 Amazon CloudWatch 日誌群組。如需在範本中使用 AWSUtility::CloudFormation::CommandRunner 資源的相關資訊,請參閱 AWS GitHub 上 aws-cloudformation-resource-providers-awsutilities-commandrunner repositoryREADME.mddocs/README.md

您必須在 AWS CLI 命令中包含 --region 選項。然後,您必須將命令的輸出寫入稱為 /command-output.txt 的保留檔案,如前面的程式碼範例所示。

您可以使用 Fn::GetAtt 參照命令的輸出。例如:

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

若要在邏輯名稱為執行個體的資源之後執行命令,請指定 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 CloudFormation 範本中執行 bash 命令

AWS 官方
AWS 官方已更新 3 年前