A Simple Introduction to AWS CloudFormation Part 3: Updating a Stack

Tung Nguyen
BoltOps
4 min readMay 22, 2017

--

In the last 2 stories we created a EC2 instance and a Route53 record successfully with CloudFormation. We have yet to walk through the useful update-stack command though. In this post we’ll use the two templates created in the first 2 posts to play around with the update-stack command.

NOTE: All the source code for this post is available on Github: tongueroo/cloudformation-examples.

Stories in the series:

  1. A Simple Introduction to AWS CloudFormation Part 1: EC2 Instance
  2. A Simple Introduction to AWS CloudFormation Part 2: EC2 Instance and Route53
  3. A Simple Introduction to AWS CloudFormation Part 3: Updating a Stack
  4. A Simple Introduction to AWS CloudFormation Part 4: Change Sets = Dry Run Mode

Jumping Right In

We have 2 templates:

  1. single_instance.yml — has a single ec2 instance
  2. instance_and_route53.yml — has a ec2 instance and route53 record

Let’s first create a stack with the single_instance template with:

$ aws cloudformation create-stack --stack-name example --template-body file://templates/single_instance.yml --parameters file://parameters/single_instance.json

You can check on the status of the stack creation via the CloudFormation console.

Now let’s pretend we have updated the single_instance.yml template to the instance_and_route53.yml template and want to update the CloudFormation example stack so that there it will add the route53 record.

You can take a look at the diff of the 2 different templates:

$ diff templates/single_instance.yml templates/instance_and_route53.yml

You should see a diff with the added Parameters and DnsRecord Resource like so:

83a84,89
> HostedZoneName:
> Description: The route53 HostedZoneName. For example, "mydomain.com." Don't forget the period at the end.
> Type: String
> Subdomain:
> Description: The subdomain of the dns entry. For example, hello -> hello.mydomain.com, hello is the subdomain.
> Type: String
387a394,403
> DnsRecord:
> Type: AWS::Route53::RecordSet
> Properties:
> HostedZoneName: !Ref 'HostedZoneName'
> Comment: DNS name for my instance.
> Name: !Join ['', [!Ref 'Subdomain', ., !Ref 'HostedZoneName']]
> Type: CNAME
> TTL: '900'
> ResourceRecords:
> - !GetAtt EC2Instance.PublicIp

Updating the Stack

Now let’s run the command to update the stack and add the route53 record:

$ aws cloudformation update-stack --stack-name example --template-body file://templates/instance_and_route53.yml --parameters file://parameters/instance_and_route53.json

You can check on the status of the stack with the AWS CloudFormation console on the Events tab again:

Notice that CloudFormation smartly added the route53 record and associated it with the DNS public hostname of the instance.

Now let’s go ahead and remove the route53 record by updating to the old stack.

aws cloudformation update-stack --stack-name example --template-body file://templates/single_instance.yml --parameters file://parameters/single_instance.json

Here’s the CloudFormation console Events tab again:

Notice how CloudFormation disassociated the route53 dns entry and removed the record.

Summary

You have now successfully updated stacks with CloudFormation. This is common task you will need to learn developing with CloudFormation. Remember to delete the stack and clean up after yourself. I hope this helps!

In the next post, I’ll show you how to a very cool CloudFormation feature call Change Sets that give you the ability to preview stack updates before making them: A Simple Introduction to AWS CloudFormation Part 4: Change Sets = Dry Run Mode.

Prebuilt CloudFormation Templates

You may be interested in BoltOps Pro blueprints. Blueprints are essentially CloudFormation templates packaged up in a convenient and reusable way. The BoltOps Pro subscription gives you access to all the blueprints in the BoltOps Pro GitHub organization. Just configure, deploy, and run.

Also, if there is a blueprint you would like to see in the future. Please feel free to send us your suggestions: Pro Blueprint Suggestion.

Thanks for reading this far. If you found this post useful, I’d really appreciate it if you recommend this post (by clicking the clap button) so others can find it too! Also, follow me on Medium.

P.S. Be sure to join the BoltOps newsletter to receive free DevOps tips and updates.

--

--