阅读背景:

具有资源属性CloudFormation的UserData脚本

来源:互联网 

The main question: How to I reference dependent resource attributes in a cloud formation template to build out a user data script.

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

What I have tried:

我试过的:

  1. Approach listed here.
  2. 这里列出的方法。
  3. Examples from sub function
  4. 子功能的例子

I am building a CloudFormation template for a three node Kafka cluster.

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

The approach I am taking here is configuring Zookeeper and Kafka on each node of the cluster using a UserData script on the EC2 instance.

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

I am using the Sub and Base64 functions to populate my user data script with the PrimaryPrivateIpAddress of my NetworkInterface but they are coming across as empty strings instead of the actual values. I know that the values are being populated correctly because they are part of my output in the template.

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

I have included the resource block of my template below as a reference. I omitted some uninteresting parts for the sake of succinctness. I also am illustrating a couple different approaches that I have tried to the EC2 resource blocks are not consistent.

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

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: {}

When this script runs I get the following output in the zoo.cfg file:

当这个脚本运行时,我在zoo.cfg文件中得到以下输出:

maxClientCnxns=50
tickTime=2000
initLimit=10
syncLimit=5
server.1=:2888:3888
server.2=:2888:3888
server.3=:2888:3888

Please let me know if I'm doing something wrong here or if I have to change my approach. Thank you for the help.

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

1 个解决方案

#1


0  

I think you're on the right path. I would just modify a bit the way you pass the 3 "private" substitute variables, for something like this (which I use quite often in my templates):

我认为你走的是正确的道路。我只想修改一下你传递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

So no brackets {} and no commas ,

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


分享到: