具有资源属性 CloudFormation 的 UserData 脚本 [英] UserData script with Resource Attribute CloudFormation

查看:31
本文介绍了具有资源属性 CloudFormation 的 UserData 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的尝试:

  1. 方法在此处.
  2. 来自子函数的示例

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

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

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

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.

我正在使用 SubBase64 函数来填充我的用户数据脚本与我的 NetworkInterface 的 PrimaryPrivateIpAddress 但它们作为空字符串而不是实际值出现.我知道这些值被正确填充,因为它们是我在模板中输出的一部分.

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.

我在下面包含了模板的资源块作为参考.为了简洁起见,我省略了一些无趣的部分.我还说明了我尝试过的几种不同的方法来解决 EC2 资源块不一致的问题.

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.

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 文件中得到以下输出:

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

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.

推荐答案

我认为您走对了路.我只是稍微修改一下您传递 3 个私有"替代变量的方式,例如(我在模板中经常使用):

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):

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 ,

这篇关于具有资源属性 CloudFormation 的 UserData 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆