![](/img/trans.png)
[英]SSM Send Command Failed,Is it possible to run ssm command from one aws account to another
[英]AWS SSM Run command : use the current account number from a Cloudformation Template
我有一個 CloudFormation 模板,它創建了一組 SSM 命令來管理我的 Linux EC2 實例。 此命令必須有權訪問我的 AWS 帳號才能執行某些任務。
在我的 CloudFormation 模板上,我做了:
AWSTemplateFormatVersion: '2010-09-09'
Description: Sample SSM commands
MyCommand1:
Type: AWS::SSM::Document
Properties:
DocumentType: Command
Content:
schemaVersion: "2.2"
parameters: {}
mainSteps:
- action: aws:runShellScript
name : command1
inputs:
runCommand:
- echo "this command run on the selected EC2 instance" && echo "You are running this on account {{global:ACCOUNT_ID}}"
Outputs:
Command1ID:
Description: MyCommand1
Value: !Ref MyCommand1
此模板安裝該功能,我可以從 SSM Web 控制台運行它。
但是 {{global:ACCOUNT_ID}} 不值我的帳號。 它的值是字符串“{{global:ACCOUNT_ID}}”。 所以我認為這不是從 SSM 命令使用全局變量的好語法。
因此,在閱讀此處的文檔https://docs.aws.amazon.com/systems-manager/latest/userguide/walkthrough-cli.html 后,我嘗試僅通過 CLI 進行測試(以快速測試其他語法):
$> sh_command_id=$(aws ssm send-command --instance-ids "i-0cb0c0ea8ef7339f1" --document-name "AWS-RunShellScript" --parameters commands='echo You are running this on account {{global:ACCOUNT_ID}}' --output text --query "Command.CommandId")
但命令失敗,出現解析錯誤Error parsing parameter '--parameters': Expected: ',', received: '}' for input
在 SSM "runCommand" 操作中使用 {{global:*}} 事物的正確語法是什么?
您可以通過使用Fn::Sub
函數來實現這一點:
- !Sub 'echo "this command run on the selected EC2 instance" && echo "You are running this on account ${AWS::AccountId}"'
您將無法使用ACCOUNT_ID
的內置變量。 使用 SSM,您正在實例上運行命令,但此命令無法識別 ACCOUNT_ID。 基於雲形成模板,我假設您在基於 *ix 的系統上運行它。
一種方法是在您的實際命令中使用它:
ACCOUNT_ID=`curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | grep accountId| awk '{print $3}'|sed 's/"//g'|sed 's/,//g'` && echo "this command run on the selected EC2 instance" && echo "You are running this on account ${ACCOUNT_ID}"
這使用ec2 實例元數據來計算帳戶 ID。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.