Car*_*eon 3 amazon-web-services aws-cloudformation aws-cli
我正在尝试在 AWS Cloudformation CLI 中编写一个通用脚本,它将堆栈的参数 AMI 更新为新值,同时保留其余参数不变。
到目前为止,我尝试这样做:
aws cloudformation update-stack --stack-name asg-xxx-123 --use-previous-template --parameters ParameterKey=ApplicationName,UsePreviousValue=true ParameterKey=ArtefactVersion,UsePreviousValue=true ParameterKey=MachineImage,ParameterValue=ami-123
Run Code Online (Sandbox Code Playgroud)
请注意,有 2 个参数正在使用UsePreviousValue=true,只有 的值ParameterKey=MachineImage需要更改 - 这工作正常。
但是,由于我需要它作为通用脚本,如何处理某些堆栈具有比上面更多参数的情况(或者甚至有些堆栈具有不同的参数但仍然有ParameterKey=MachineImage)?有没有办法说只更改 的值ParameterKey=MachineImage,而所有其余的都应该使用以前的值而不在 中明确列出--parameters?
我能够使用 aws cli 编写 unix 脚本,如下所示:
curdate=`date +"%Y-%m-%d"`
newami=${1}
for sname in $(aws cloudformation describe-stacks --query "Stacks[?contains(StackName,'prefix-') ].StackName" --output text) ;
do
paramslist="--parameters ";
for paramval in $(aws cloudformation describe-stacks --stack-name $sname --query "Stacks[].Parameters[].ParameterKey" --output text) ;
do
if [ $paramval == "MachineImg" ] || [ $paramval == "AMI" ]
then
paramslist+="ParameterKey=${paramval},ParameterValue=${newami} "; #use the ami from args
else
paramslist+="ParameterKey=${paramval},UsePreviousValue=true "; #else keep using UsePreviousValue=true
fi
done
printf "aws cloudformation update-stack --stack-name ${sname} --use-previous-template ${paramslist};\n" >> "/tmp/ami-update-${curdate}.sh"
done
Run Code Online (Sandbox Code Playgroud)
它会生成一个新的 .sh 文件,其中包含更新命令,然后我查看生成的 .sh 的内容并创建一个源来执行这些命令:
source ./ami-update-2020-08-17.sh
Run Code Online (Sandbox Code Playgroud)