通过云形成为实例分配公共 IP 地址

通过云形成为实例分配公共 IP 地址

此云形成模板按我的预期工作。但是当我启用私有 IP 地址参数时,出现错误。

{
   "Resources":{
      "MySpotFleet":{
         "Type":"AWS::EC2::SpotFleet",
         "Properties":{
            "SpotFleetRequestConfigData":{
               "IamFleetRole": "arn:aws:iam::513469704633:role/aws-ec2-spot-fleet-tagging-role",
               "SpotPrice":"1",
               "TargetCapacity":"1",
               "LaunchSpecifications":[
                  {
                     "InstanceType":"t2.small",
                     "ImageId":"ami-009d6802948d06e52",
                     #"PrivateIpAddress": "18.208.241.12"
                  }
               ]
            }
         }
      }
   }
}

如何为新创建的竞价实例分配特定的弹性 IP 地址?

如果答案是通过 UserData,那么如何在此模板中提及用户数据字符串?(我在用户数据文件中还有其他一些命令)


更新:

我尝试使用用户数据设置 IP 地址。使用此脚本创建了 Spot 实例,但没有任何公共 IP 地址。

{
   "Resources":{
      "MySpotFleet":{
         "Type":"AWS::EC2::SpotFleet",
         "Properties":{
            "SpotFleetRequestConfigData":{
               "IamFleetRole": "arn:aws:iam::513469704633:role/aws-ec2-spot-fleet-tagging-role",
               "SpotPrice":"1",
               "TargetCapacity":"1",
               "LaunchSpecifications":[
                  {
                     "InstanceType":"t2.small",
                     "ImageId":"ami-009d6802948d06e52",
                     #"PrivateIpAddress": "18.208.241.12"
        "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
          "#!/bin/bash -xe\n",
"pip install aws-ec2-assign-elastic-ip\n",
"aws-ec2-assign-elastic-ip --access-key xxx --secret-key xxx --valid-ips 18.208.241.12\n"
        ]]}}
                  }
               ]
            }
         }
      }
   }
}

答案1

我设法添加了用户数据参数,最终代码如下所示:

{
   "Resources":{
      "MySpotFleet":{
         "Type":"AWS::EC2::SpotFleet",
         "Properties":{
            "SpotFleetRequestConfigData":{
               "IamFleetRole":"arn:aws:iam::513469704633:role/aws-ec2-spot-fleet-tagging-role",
               "SpotPrice":"1",
               "TargetCapacity":"1",
               "LaunchSpecifications":[
                  {
                     "InstanceType":"t2.small",
                     "ImageId":"ami-009d6802948d06e52",
                     "KeyName":"dec15a",
                     "BlockDeviceMappings":[
                        {
                           "DeviceName":"/dev/xvda",
                           "Ebs":{
                              "VolumeSize":100,
                              "VolumeType":"gp2"
                           }
                        }
                     ],

                   "UserData": {
 "Fn::Base64": {
 "Fn::Sub": 
          "#!/bin/bash -xe\n
           yum install -y docker mysql git python-pip\n  
           pip install aws-ec2-assign-elastic-ip\n 
           sudo amazon-linux-extras install -y docker\n
           aws-ec2-assign-elastic-ip --access-key xxx --secret-key xxx --valid-ips 18.208.241.12\n
           service docker start\n"
                    }
 },

                  }
               ]
            }
         }
      }
   }
}

相关内容