将现有EIP与CloudFormation中的EC2实例相关联

时间:2017-06-13 18:17:11

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

我有一个CloudFormation来启动我的QA服务器并且有一些我预先分配的弹性IP。这些是为我的QA服务器保留的,只在需要时启动。我现在所拥有的是创建并为我的实例分配新的EIP。

Ec2Instance:
Type: AWS::EC2::Instance
Properties:
  UserData:
    Fn::Base64:
      Fn::Join:
      - ''
      - - IPAddress=
        - Ref: IPAddress
  ImageId:
    Fn::FindInMap:
    - RegionMap
    - Ref: AWS::Region
    - AMI
  IPAddress:
    Type: AWS::EC2::EIP
  IPAssoc:
    Type: AWS::EC2::EIPAssociation
    Properties:
      InstanceId:
        Ref: Ec2Instance
      EIP:
        Ref: IPAddress

是否有任何选项可以列出现有的EIP并将其与我的实例相关联?

1 个答案:

答案 0 :(得分:0)

I see it has to be put in userdata.

 "setup2" : {
            "commands" : {
              "00_associate_eip" : {
                "command" : {
                  "Fn::Join" : [ "", [ "#!/bin/bash\n", "export AWS_DEFAULT_REGION=", {
                    "Ref" : "AWS::Region"
                  }, "\n", "aws ec2 associate-address", " --allow-reassociation", " --instance-id $(curl http://169.254.169.254/latest/meta-data/instance-id)", " --allocation-id ", {
                    "Ref" : "ElasticVPN"
                  } ] ]
                }
              },