Reflections on Infrastructure as Code for Serverless

Igor Soroka
AWS in Plain English
4 min readAug 27, 2021

I started my cloud journey from Heroku. It was a great experience because I was fascinated by the idea of making it so easy to deploy your code to the cloud. After some time spent in Java and on-premise servers, I went directly to AWS. It was started as a preparation for the certification. However, it changed my understanding of software development and career priorities. In this article, I will talk about my reflections on infrastructure as code. Here, my focus will be especially on serverless applications.

When I started the IaC gave me frustration. I did not get the idea at all. The first project was used because of the time-saving capabilities and repeatability. I was using the serverless framework and Terraform which made things more complicated.

What is IaC exactly? It is a methodology where the infrastructure configuration is treated as source code (or implementation code). It is a wide term for using YAML, Bash, Python, or any programming language. Let’s talk about the main options for this.

There are multiple of them for the AWS ecosystem which I encountered.

Cloudformation (CFN)

This is the most obvious solution which is provided by AWS. It has two formats — YAML and JSON. The second one is harder to read, to be honest. It is really the one which is the best as a starting point. As my journey continues, I am checking the documentation of CFN every day. It will be the only source of truth for the AWS infrastructure because all other frameworks and tools are the abstractions on top of it.

SAM (Serverless Application Model)

This is the one which is also officially coming from AWS. It is an improved version of CFN. They reworked some of the types which are done for AWS Lambdas. It comes with great tooling for deployments and local testing. It has

sam local invoke <LambdaFunctionID>

It gives an ability to run your Lambda functions locally with the needed payload. Highly recommended as a starting point in the journey of creating server-less projects.

Serverless Framework (SLS)

One of the easiest IaC to start with. It has a very clear YAML configuration that supports even CFN resources. It helped me in the beginning. However, the abstraction from CFN is not good for bigger projects. SLS has local invocation also.

Terraform

The most popular solution among outsourcing and consultancies. It gives the ability to write infrastructure for AWS, GCP, and Azure. Each one has special libraries. It has a special structured language. The documentation is great, by the way. I enjoyed reading it. I would say that the benefit will be for those who want to use multiple cloud providers. Also, it has a great ability to have modules. There are many ready-mades on GitHub.

Cloud Development Kit (CDK)

The top-notch IaC for the AWS ecosystem. The framework comes from AWS itself also. It gives an ability to set up infrastructure using Typescript, Javascript, Python, C#, and Java. I am using Typescript. It is an incredible experience. Especially when now they issued version 2. It has no issues with dependencies for different modules.

The typical app has stacks that are the same as CloudFormation ones. The modules are made as constructs. The good thing here is that AWS has plenty of examples that give the solution for most common problems. The real programming language gives great parametrization and control of what you are deploying in which environment. It is handy in the use case when you need to attach a custom domain to the API in the prod only.

One of the biggest advantages of the CDK is that you can use a command to generate a CloudFormation template.

cdk synth

With this functionality, you have the possibility to check the template before deployment. After that, even local invocation of SAM can be used from the generated template. This command is useful for further deployment using CloudFormation as part of the CodePipeline.

These days, I am using CDK for almost all of my projects. The most common issue is that it is quite common to migrate from CFN to CDK. After transforming a 1000 line template into nice Typescript self-documented code, you have a sense of relief. I also noticed that new people could be onboarded quite quickly with CDK. However, the experience with other IaC tools is helping with that. The serverless framework could be great for people who have zero knowledge of AWS offerings and want to try AWS Lambda functions. My recommendation will be to start in AWS infrastructure for serverless in this order:

AWS Management Console -> SAM -> CDK

Useful resources

CloudFormation Docs

CDK v2 Constructs

If you are interested in getting the AWS certification, I am telling you about my experience of preparation for Certified Developer Associate.

More content at plainenglish.io

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in AWS in Plain English

New AWS, Cloud, and DevOps content every day. Follow to join our 3.5M+ monthly readers.

Written by Igor Soroka

⚡ AWS Community Builder, 📺 wabi-sabi, 👨‍💻 Soroka Tech founder, 🏃 long-distance runner, 🇫🇮 Finland. Writing about tech, motivation, learning, and sports.

Responses (1)

Write a response

Good write-up. IMO, when you're a developer, CDK makes a lot of sense. But, it still generates Cloudformation templates and has all the challanges cloudformation has. CDK takes care of the nitty great, but in the end it's still Cloudformation that…