0

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.

1

2 Answers 2

0

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:

Outputs:
    myEc2IP: # just some identifier
        Value: !GetAtt myInstance.PublicIp # assuming that "myInstance" is your resource

Then, after deploying your stack, you can use the AWS CLI to extract the value:

aws cloudformation describe-stacks --stack-name $YOUR_STACK \
    --query 'Stacks[0].Outputs[?OutputKey==`myEc2IP`].OutputValue' \
    --output text

You can even load this into a shell variable by something like

export MY_ROLE_ARN="$(aws cloudformation describe-stacks …)"

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).

0

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.