Domesticate AWS nested stacks in Java: doing the chores Cloudformation doesn't do (w/ code samples)

Domesticate AWS nested stacks in Java: doing the chores Cloudformation doesn't do (w/ code samples)

In this article we’ll navigate through the creation of a Nested Stack in Cloudformation using the Java SDK. The child stack will be a lambda function, and the code will be uploaded with a zip archive.

What’s Cloudformation, and what’s a nested stack?

Cloudformation is the AWS offering of infrastructure as code. Instead of navigating the web UI adding and configuring resources, Cloudformation offers the capability of reading a user - supplied file (either JSON or YAML) containing the list of resources and their relationships and create them as the code states.
These resources must be grouped in Stacks, which is the parentmost object that Cloudformation can process.
Things get interesting when stacks reference other stacks, of course :-)

Read more
Please stop publishing AWS S3 buckets as static websites! Read here for a secure, fast, and free-ish approach [1st episode]

Please stop publishing AWS S3 buckets as static websites! Read here for a secure, fast, and free-ish approach [1st episode]

I promise this is not yet another tutorial on how to publish a static website using AWS S3, or at least not solely smashing the S3 content onto the web. I’d like to show you a GitHub project that uses Java to orchestrate Cloudformation when deploying the architecture of a static website.

The main purpose of this tool is going beyond the S3 out of the box website functionality, that is:

  • Make the S3 bucket private (so, secure)
  • Provide HTTPS certificates (secure, again)
  • Serve the content via cloudfront cache (so, fast)
  • Hide the complexities of working with Cloudformation

I’m in for fast and secure, but free…ish?

Not all the resources that need to be fired up for this architecture are within the AWS free tier, expecially the domain. Nevertheless, all the costs that I’ve seen after this website was published were only live costs. Let’s review them from the most to the least expensive:

  • The domain: 20€ / year if hosted on Route53 (as marcoaguzzi.it) but you can host it elsewere (on cloudns, and it’s free)
  • Route53 and Codepipeline: 1€ / month each. It’s one for the hosted zone and one for the pipeline. The pipeline comes with a good amount of free build / minutes
  • Secret manager: less than 0.5€ / month (there’s a grace period when started)
  • Cloudfront and S3: 0.01€ / month each

Of course these are starting costs, they can go a lot higher as the usage increase, but it should be a welcomed issue, I suppose

Hide the complexities of Cloudformation

Cloudformation migth be a burden to use, especially within the web UI. These are the main issue I addressed in the project:

  • Have a self - contained architecture
  • Be repeatable. Could it deploy the same architecture on another domain?
  • Ease the deploy process, especially when the domain is not hosted on Route53
  • The nested stacks are not automatically resolved by Cloudformation

The Java tool to the rescue

While experimenting with Java and Gradle, I wondered if I could use Java to mitigate the problems listed above by orchestrating the instructions that Cloudformation needs in order to deploy the website. This turned out as a Github project: https://github.com/maguzzi/s3_static_website_gradle. The Gradle build creates a distributable archive with all the needed jars.

Read more
Unforgettable deploy: keep resources coupled with Cloudformation Nested Stacks

Unforgettable deploy: keep resources coupled with Cloudformation Nested Stacks

Requirement

This website is served by an AWS Cloudfront distribution. The distribution has a cache behavior with a lambda@edge function attached to it to complete with “/index.html” the urls ending with a slash character.
Before this post, the Cloudformation Stack with the lambda and the one with the cloudfront distribution were separated. The only link between the two was the output value exported by the former and read by the latter.
Here’s what the AWS web UI lists:

The Cloudfront distribution can’t live without the lambda, so the deployment of the lambda should be done within the distribution one. The risk of having the two stacks completely separated is that an updated version of the lambda is not immediately referenced in the Cloudfront distribution (which is exactly what happened in the previous deploys of the website)

Taken approach and what’s needed

AWS Cloudformation Nested Stacks can be useful. One child stack is referenced in a parent stack and, when the parent is deployed, the resources of the child stack are deployed first.

Read more
Welcome to (yet) another dev blog

Welcome to (yet) another dev blog

Intro

This site is intended as a repository of dev attempts, mainly dedicated to aws. Please enjoy the journey!

About

The blog is a completely static website hosted on Amazon AWS S3, served by Cloudfront. The domain name is registered on Route53, and the configuration is (almost) on a CloudFormation template, which is resembled in the cover of this post

Two domains

Read more