具有资源属性CloudFormation的UserData脚本

时间:2018-02-05 15:21:05

标签: amazon-web-services amazon-ec2 yaml amazon-cloudformation

主要问题:如何在云形成模板中引用相关资源属性来构建用户数据脚本。

我尝试了什么

  1. 列出here的方法。
  2. sub function
  3. 中的示例

    我正在为三节点Kafka群集构建CloudFormation模板。

    我在这里采用的方法是使用EC2实例上的UserData脚本在集群的每个节点上配置Zookeeper和Kafka。

    我正在使用SubBase64函数使用我的NetworkInterface的as pDate填充我的用户数据脚本,但它们是空字符串而不是实际值。我知道值正在填充正确,因为它们是模板中输出的一部分。

    我已将下面的模板资源块作为参考。为简洁起见,我省略了一些无趣的部分。我还说明了我尝试过的EC2资源块不一致的几种不同方法。

    PrimaryPrivateIpAddress

    当此脚本运行时,我在EC2I8MWW: Type: 'AWS::EC2::Instance' DependsOn: - EC2NI2E8ES - EC2NI2PFST - EC2NI54B66 Properties: KeyName: !Ref DesiredKeyName InstanceType: !Ref InstanceType NetworkInterfaces: - NetworkInterfaceId: !Ref EC2NI54B66 DeviceIndex: "0" UserData: Fn::Base64: Fn::Sub: - | #!/bin/bash CONF="/etc/zookeeper/conf.dist/zoo.cfg" PRIVATE_1=${Private1} PRIVATE_2=${Private2} PRIVATE_3=${Private3} echo "# Zookeeper configuration for Talentreef" > "$CONF" cat <<EOT >> "$CONF" maxClientCnxns=50 tickTime=2000 initLimit=10 syncLimit=5 EOT echo "server.1=$PRIVATE_1:2888:3888" >> $CONF echo "server.2=$PRIVATE_2:2888:3888" >> $CONF echo "server.3=$PRIVATE_3:2888:3888" >> $CONF service zookeeper-server init --myid=$NODE_ID chkconfig zookeeper-server on - { Private1: !GetAtt EC2NI2E8ES.PrimaryPrivateIpAddress, Private2: !GetAtt EC2NI2PFST.PrimaryPrivateIpAddress, Private3: !GetAtt EC2NI54B66.PrimaryPrivateIpAddress } EC2I2JVJI: Type: 'AWS::EC2::Instance' DependsOn: EC2NI54B66 Properties: KeyName: !Ref DesiredKeyName InstanceType: !Ref InstanceType BlockDeviceMappings: - DeviceName: /dev/xvdb Ebs: VolumeType: st1 DeleteOnTermination: 'true' VolumeSize: '500' - DeviceName: /dev/xvda Ebs: VolumeType: gp2 DeleteOnTermination: 'true' VolumeSize: '8' ImageId: !FindInMap - AWSRegionArch2AMI - !Ref 'AWS::Region' - !FindInMap - AWSInstanceType2Arch - !Ref InstanceType - Arch NetworkInterfaces: - NetworkInterfaceId: !Ref EC2NI2PFST DeviceIndex: "0" UserData: Fn::Base64: !Sub | #!/bin/bash CONF="/etc/zookeeper/conf.dist/zoo.cfg" cp $CONF /etc/zookeeper/conf.dist/zoo.cfg.bak-$(date +%s) echo "# Zookeeper configuration for Talentreef" > "$CONF" cat <<EOT >> "$CONF" maxClientCnxns=50 tickTime=2000 initLimit=10 syncLimit=5 server.1=${EC2NI2E8ES.PrimaryPrivateIpAddress}:2888:3888 server.2=${EC2NI2PFST.PrimaryPrivateIpAddress}:2888:3888 server.3=${EC2NI54B66.PrimaryPrivateIpAddress}:2888:3888 EOT service zookeeper-server init --myid=$NODE_ID chkconfig zookeeper-server on service zookeeper-server start EC2I56LVQ: Type: 'AWS::EC2::Instance' DependsOn: EC2NI54B66 Properties: KeyName: !Ref DesiredKeyName InstanceType: !Ref InstanceType BlockDeviceMappings: - DeviceName: /dev/xvdb Ebs: VolumeType: st1 DeleteOnTermination: 'true' VolumeSize: '500' - DeviceName: /dev/xvda Ebs: VolumeType: gp2 DeleteOnTermination: 'true' VolumeSize: '8' ImageId: !FindInMap - AWSRegionArch2AMI - !Ref 'AWS::Region' - !FindInMap - AWSInstanceType2Arch - !Ref InstanceType - Arch NetworkInterfaces: - NetworkInterfaceId: !Ref EC2NI2E8ES DeviceIndex: "0" UserData: Fn::Base64: Fn::Sub: - | CONF="/etc/zookeeper/conf.dist/zoo.cfg" cp $CONF /etc/zookeeper/conf.dist/zoo.cfg.bak-$(date +%s) echo "# Zookeeper configuration for Talentreef" > "$CONF" cat <<EOT >> "$CONF" maxClientCnxns=50 tickTime=2000 initLimit=10 syncLimit=5 EOT echo "server.1=${Private1}:2888:3888" >> $CONF echo "server.2=${Private2}:2888:3888" >> $CONF echo "server.3=${Private3}:2888:3888" >> $CONF service zookeeper-server init --myid=$NODE_ID chkconfig zookeeper-server on - { Private1: !GetAtt EC2NI2E8ES.PrimaryPrivateIpAddress, Private2: !GetAtt EC2NI2PFST.PrimaryPrivateIpAddress, Private3: !GetAtt EC2NI54B66.PrimaryPrivateIpAddress } EC2NI54B66: Type: 'AWS::EC2::NetworkInterface' DependsOn: EC2NI2PFST Properties: {} EC2NI2PFST: Type: 'AWS::EC2::NetworkInterface' DependsOn: EC2NI2E8ES Properties {} EC2NI2E8ES: Type: 'AWS::EC2::NetworkInterface' Properties: {} 文件中获得以下输出:

    zoo.cfg

    如果我在这里做错了或者我必须改变方法,请告诉我。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

我认为你走的是正确的道路。我只想修改一下你传递3个“私有”替换变量的方法,就像这样(我经常在我的模板中使用):

UserData:
  Fn::Base64:
    Fn::Sub:
      - |
        CONF="/etc/zookeeper/conf.dist/zoo.cfg"
        cp $CONF /etc/zookeeper/conf.dist/zoo.cfg.bak-$(date +%s)
        echo "# Zookeeper configuration for Talentreef" > "$CONF"
        cat <<EOT >> "$CONF"
        maxClientCnxns=50
        tickTime=2000
        initLimit=10
        syncLimit=5
        EOT
        echo "server.1=${Private1}:2888:3888" >> $CONF
        echo "server.2=${Private2}:2888:3888" >> $CONF
        echo "server.3=${Private3}:2888:3888" >> $CONF
        service zookeeper-server init --myid=$NODE_ID
        chkconfig zookeeper-server on
      - Private1: !GetAtt EC2NI2E8ES.PrimaryPrivateIpAddress
        Private2: !GetAtt EC2NI2PFST.PrimaryPrivateIpAddress
        Private3: !GetAtt EC2NI54B66.PrimaryPrivateIpAddress

所以没有括号{}和逗号,