我想将我的实例UserData更改为UserData
我的困难是使用我现有的UserData Bash脚本,因为它有变量,并且我希望在content文件内容部分中有这些变量。
这是现有的UserData,这是我想要传输到cfn-init的最后6行。内容部分中的变量是如何使用的?
UserData:
Fn::Base64: |
#!/bin/bash -xe
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
ID=`curl -s http://169.254.169.254/latest/meta-data/instance-id`
AVAIL_ZONE=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone`
REGION=`curl http://169.254.169.254/latest/dynamic/instance-identity/document|grep region|awk -F\" '{print $4}'`
echo "This is Server * $ID * in AWS Region $REGION in AZ $AVAIL_ZONE<br>" > /var/www/html/index.html
EC2_AVAIL_ZONE=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)
echo "<h1>Hello World from $(hostname -f) in AZ $EC2_AVAIL_ZONE </h1><br>" >> /var/www/html/index.html发布于 2020-08-17 16:51:36
这可能有点麻烦,但您可以使用Fn::Sub将参数插入到用户数据中:
UserData:
Fn::Base64:
Fn::Sub: |
#!/bin/bash -xe
echo "${ThisIsACloudFormationParameter}"其原因是CFN和Bash都可以使用${…}样式进行变量替换,所以确保只对CFN参数使用这种语法。
请注意,您可以对其中一个函数使用!Base64和!Sub简写,但不能同时使用两者,CFN不支持嵌套的速记函数调用。
https://stackoverflow.com/questions/63454581
复制相似问题