autsaling launchconfig组中的aws cloudformation“NetworkInterfaces”

时间:2013-11-07 13:10:54

标签: amazon-web-services amazon-cloudformation

在aws cloudformation中如何在Autoscaling launchconfig组中添加“NetworkInterfaces”,因为我想配置每个启动的实例,我需要“NetworkInterfaces”在同一个AWS :: EC2 :: Instance?

1 个答案:

答案 0 :(得分:1)

我目前使用的解决方案是确保每个实例都启用IAM Instance Profile允许包含政策

"PolicyDocument": {
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "ec2:CreateTags",
                "ec2:DescribeSubnets",
                "ec2:AttachNetworkInterface",
                "ec2:CreateNetworkInterface",
                "ec2:ModifyNetworkInterfaceAttribute"
            ],
            "Effect": "Allow",
            "Resource": "*"
        }
    ]
}

用于创建AutoScalingGroupLaunchConfiguration的我的云形成模板接收SubnetsSecurity Groups的参数,以附加到每个额外的ENI

"SecondaryNICSubnetIds":{
    "Type" : "CommaDelimitedList",
    "Description" : "Ensure that the spread of Availability Zones for these Subnets matches the SubnetIds used to create Instances, as when creating a Secondary ENI it must exist in the same AZ as the Instance"
},
"SecondaryNICSecurityGroupIds":{
    "Type" : "CommaDelimitedList",
    "Description" : "Security Groups to associate to the Secondary ENI"
},

然后LaunchConfiguration包含UserData属性

  1. 确保安装awscli
  2. 通过解析instance metadata身份证明文件
  3. 设置要使用的区域
  4. instance metadata
  5. 获取实例ID
  6. instance metadata
  7. 获取该实例的可用区 来自已传入的子网的
  8. 使用awscli describe-subnets调用找到与我的实例AZ匹配的第一个
  9. 在我选择的子网中创建网络接口,并使用awscli create-network-interface调用
  10. 向其添加安全组
  11. 使用ENI调用
  12. 标记我的awscli create-tags
  13. 使用ENI调用
  14. awscli attach-network-interface附加到我的实例
  15. 使用ENI调用修改附件以删除实例终止上的modify-network-interface-attribute
  16. "UserData": {
          "Fn::Base64" : {
            "Fn::Join": [ "\n",
              [
                "#!/bin/bash -xe",
                "sudo apt-get install -y awscli",
                "export AWS_DEFAULT_REGION=$(curl -sS http://169.254.169.254/latest/dynamic/instance-identity/document | python -c 'import sys, json; print(json.load(sys.stdin)[\"region\"])')",
                "INSTANCE_ID=$(curl -sS http://169.254.169.254/latest/meta-data/instance-id)",
                "AZ=$(curl -sS http://169.254.169.254/latest/meta-data/placement/availability-zone)",
                "echo Availability Zone: ${AZ}",
                {"Fn::Sub":[
                    "SUBNET_ID=$(aws ec2 describe-subnets --subnet-ids ${SubnetNetIds} --filters Name=availabilityZone,Values=${!AZ} --query 'Subnets[0].SubnetId' --output text)",
                    {"SubnetNetIds":  {"Fn::Join": [" ", {"Ref": "SecondaryNICSubnetIds"} ] }}
                ]},
                "echo Subnet Id: ${SUBNET_ID}",
                {"Fn::Sub":[
                  "ENI_ID=$(aws ec2 create-network-interface --subnet ${!SUBNET_ID} --description 'Secondary ENI' --groups ${SecurityGroups} --query 'NetworkInterface.NetworkInterfaceId' --output text)",
                  {"SecurityGroups":  {"Fn::Join": [" ", {"Ref": "SecondaryNICSecurityGroupIds"}]} }
                ]},
                "echo ENI ID: ${ENI_ID}",
                "aws ec2 create-tags --resources ${!ENI_ID} --tags Key=Some,Value=Tag",
                "ATTACHMENT_ID=$(aws ec2 attach-network-interface --network-interface-id ${ENI_ID} --instance-id ${INSTANCE_ID} --device-index 1 --output text)",
                "echo Attachment ID: ${ATTACHMENT_ID}",
                "echo Delete On Termination: $(aws ec2 modify-network-interface-attribute --network-interface-id ${ENI_ID} --attachment AttachmentId=${ATTACHMENT_ID},DeleteOnTermination=true --output text)"
               ]
            ]
          }
        }
    

    如果您不想将Subnets传递到云端格式模板,则可以尝试通过在awscli describe-subnets来电中向--query添加标记来查找它们,如果您的基础架构允许您以这种方式识别它们。