CloudFormation ElasticIP Parameter

2019-09-21 08:00发布

问题:

I have a CloudFormation template which adds OpenVPN to an existing VPC and requires an Elastic IP allocation ID as a parameter. It also adds the public IP address from the same Elastic IP to the OpenVPN instance configuration (in it's UserData section).

I've currently implemented this as 2 parameters (using made-up defaults) i.e.

Parameters:

  ElasticIpAddress:
    Description: >-
      IP Address of an Elastic IP.
    Type: String
    Default: 53.176.52.215

  ElasticIpAllocationId:
    Description: >-
      Allocation id of the same Elastic IP to associate OpenVPN server with.
    Type: String
    Default: eipalloc-f2013ba5

  ...

NOTE - Both of these must point to the same ElasticIP in AWS!

The ElasticIpAddress parameter is used when creating the OpenVPN instance in a AWS::EC2::Instance section as follows: -

openVPN:
  Type: 'AWS::EC2::Instance'
  Properties:
    Tags:
      - Key: Name
        Value: openVPN server
    UserData:
      Fn::Base64: !Sub |
        public_hostname=${ElasticIpAddress}
        admin_user=${OpenVPNASAdminUser}
    ...

... and the ElasticIpAllocationId get used in an AWS::EC2::EIPAssociation section ...

IPAssoc:
  Type: 'AWS::EC2::EIPAssociation'
  Properties:
    AllocationId: !Ref ElasticIpAllocationId
    InstanceId: !Ref openVPN
  DependsOn: openVPN

It seems very redundant to have (a) allocation ID and (b) IP address of the same Elastic IP!

My main question is - does a function exists which: -

  1. Retrieve the IP address from an Elastic IP allocation Id or
  2. Retrieve an allocation Id from an IP address of an elastic IP?

My gut feeling is that I'll have to use the CLI inside the UserData: section of the instance and use AWS CLI commands - not sure how nicely that will work with the OpenVPN AMI as it currently just takes OpenVPN specific configuration.

Any advice is appreciated!

回答1:

The right way to achieve what you want, is to create a R53 record, for example vpn.mydomain.com which points to your Elastic IP address (A record). You can then assign this record in userdata instead of ${ElasticIpAddress}, and eliminate the need for the IP address parameter.

public_hostname=vpn.mydomain.com

As long as your EIP is updated on the R53 record, you should be good. This is also helpful if you need to change the EIP address, you still have control over the record on your deployed instance via Route53.

Getting Started with Route53