Automating Deployments with Infrastructure as Code
This lesson explains how infrastructure as code (IaC) and managed deployment services make environments repeatable and deployments fast, without manual configuration.
Learning outcomes
After completing this lesson, you will be able to:
- Describe infrastructure as code and its benefits.
- Distinguish AWS CloudFormation, the AWS CDK and AWS Elastic Beanstalk.
What infrastructure as code is
IaC means defining infrastructure (networks, servers, databases, permissions) in text files that can be version-controlled, reviewed and run to create the same environment every time. Its main benefits are as follows.
- Consistency, with no configuration drift.
- Speed and fewer human errors.
- Version control and a history of changes.
- Easy rollback and quick recreation of environments.
Benefits of IaC
Deployment services
Several services automate building infrastructure and deploying applications, so environments are created the same way every time. AWS CloudFormation creates resources from templates written in YAML or JSON. A deployed template is a stack, which is created, updated and deleted as one unit, so the same template can build identical development, test and production environments, or recreate them in another Region. The AWS CDK lets developers define the same infrastructure in programming languages such as Python or TypeScript, and turns it into CloudFormation templates.
AWS Elastic Beanstalk is a platform as a service: you upload application code, and it creates and manages the servers, load balancing, scaling and health monitoring. AWS Systems Manager manages fleets of servers, on AWS and on premises, for example patching them and running commands on many at once. The following table summarises the services.
| Service | What it does |
|---|---|
| AWS CloudFormation | Provisions resources from YAML or JSON templates; a deployed template is a stack created, updated and deleted as one unit. The same template builds identical dev, test and prod environments or recreates them in another Region |
| AWS CDK | Defines infrastructure in programming languages (TypeScript, Python, Java) and generates CloudFormation templates |
| AWS Elastic Beanstalk | Platform as a service: upload code (Java, .NET, Node.js, Python, PHP, Ruby, Go, Docker) and it handles capacity, load balancing, Auto Scaling and health monitoring |
| AWS Systems Manager | Manages fleets of EC2 and on-premises servers: patching, running commands at scale, Parameter Store, Session Manager |
Deployment services
Identical test and production environments
A team keeps finding that bugs appear in production but not in testing, because the two environments were set up by hand and differ slightly. It describes the environment once in a CloudFormation template:
Resources:
WebsiteBucket:
Type: AWS::S3::Bucket
Properties:
VersioningConfiguration:
Status: Enabled
AppQueue:
Type: AWS::SQS::QueueDeploying the same template twice creates two identical stacks, shop-test and shop-prod. Changes are made to the template, reviewed, and applied to both, so the environments cannot drift apart. If the team preferred to write infrastructure in Python or TypeScript, the AWS CDK would generate the template for them; if they only wanted to upload code and let AWS handle servers, Elastic Beanstalk would be simpler still.
What is infrastructure as code?
Show answer
Defining resources in templates or code, for example with AWS CloudFormation, so environments can be created the same way every time and changes can be reviewed.
Exam Tips
Remember for the exam
- To provision infrastructure from templates, use CloudFormation.
- To define infrastructure in a programming language, use the AWS CDK.
- To deploy a web application without managing infrastructure, use Elastic Beanstalk.
- To patch and run commands on many servers, use Systems Manager.
- Infrastructure as code gives consistent, repeatable environments.