I am using a YAML file in the CloudFormation service on AWS. This file creates an EC2 server with a public IP and a stack output within the CloudFormation using the IP of the EC2 server that it just created. The output however, is not something we use a lot. Therefore I would like to close the server from time to time and open it again whenever I need it. The problem is that every time I launch the EC2 server again, it changes its public IP and the IP of the stack output doesn’t change with it.
I found a way to make it static, using an elastic IP address from the EC2 service. However, I can’t seem to find a way to select that IP address when choosing properties in creating the stack. So I would like some assistance on this.
2
Answers
You cannot define the IP address yourself, but you can extract it after it has been generated.
In your Cloudformation template, add an
Outputs
section like the following:Then, after deploying your stack, you can use the AWS CLI to extract the value:
You can even load this into a shell variable by something like
Learn more about Outputs: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/outputs-section-structure.html
See other possible output/return values for EC2 instances in Cloudformation: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html (in the “Return values” section).
You can’t select the IP address that you will get. Elastic IP addresses are chosen from Amazon’s pool of IP addresses.
If you want to specify a specific IP address that you own, you can use Bring your own IP addresses (BYOIP) in Amazon EC2. That will allow you to specify the IP that you own to be used by your EC2 instances.