Deployment and Automation · Topic 7 of 7

Deployment Strategies, Terraform and Git

Choose in place, rolling, blue/green and canary deployments with CodeDeploy and Elastic Beanstalk, and manage infrastructure code with Terraform and Git.

  • 5 min
In a nutshell
  • In place and rolling updates reuse the same instances, so rollback means redeploying.
  • Blue/green and immutable keep the old version intact, so rollback is fast.
  • CodeDeploy supports EC2 and on premises servers, Lambda and ECS, and can roll back on failure or on an alarm.

What it is

A deployment strategy is the plan for replacing the running version of an application. Strategies differ in capacity needed, risk and how fast you can go back.

Terraform is a third party tool that does what CloudFormation does, and Git keeps the history of every change to templates and code.

CommitGit branchpull request reviewMergemain branchtagReleaseversion tagdeployEnvironmentnew version

How it works

Deployment strategies

StrategyWhat happensRollback
In place, all at onceThe new version replaces the old on every instance togetherSlow, redeploy
RollingInstances are updated in batches while the rest keep servingSlow, update the batches back
Rolling with additional batchAn extra batch is added first, so full capacity is keptSlow, update back
ImmutableNew instances in a new group run the new version, old ones go once it is healthyFast, discard the new instances
Blue/greenA full second environment runs the new version, then traffic switchesFastest, switch traffic back
CanaryA small share of traffic goes to the new version first, then the restFast, send the share back

Blue is the current environment and green is the new one. A canary shifts a small percentage first and the rest after a wait, and a linear shift moves traffic in equal steps.

AWS services that implement them

AWS CodeDeploy automates deployments to EC2 instances and on premises servers, Lambda functions and Amazon ECS services. For EC2, it can do an in place deployment, with batch settings such as one at a time, or a blue/green deployment behind a load balancer. For Lambda and ECS it shifts traffic as canary, linear or all at once. An appspec.yml file says what to copy and which scripts to run, and EC2 instances need the CodeDeploy agent. It can roll back automatically on failure or on a CloudWatch alarm.

AWS Elastic Beanstalk offers deployment policies per environment: all at once, rolling, rolling with additional batch, immutable and traffic splitting, which is a canary style test. Blue/green is done by cloning the environment and swapping the environment URLs.

Building blocks for traffic shifting

MechanismHow it shifts traffic
Route 53 weighted recordsSplits DNS answers by weight, but clients may cache the old answer until the TTL ends
ALB weighted target groupsA listener rule forwards a chosen share to each target group, and the shift is immediate
Lambda alias weightsAn alias points at two versions and sends a chosen percentage to the second one
Auto Scaling instance refreshReplaces instances in batches after a new launch template version
# AWS CLI: send 10 percent of an alias to version 2
aws lambda update-alias --function-name orders --name live \
  --function-version 1 \
  --routing-config '{"AdditionalVersionWeights":{"2":0.1}}'

Terraform compared with CloudFormation

Terraform reads configuration files written in HCL (HashiCorp Configuration Language) and calls provider plugins, such as the AWS provider, to create resources. The usual workflow has three commands.

terraform init     # download providers, set up the backend
terraform plan     # preview the changes
terraform apply    # make the changes

Terraform records what it built in a state file. For a team, keep it in a remote backend such as an S3 bucket, with locking so two runs cannot change it at once.

PointCloudFormationTerraform
FormatJSON or YAML templatesHCL files
StateHeld by the CloudFormation serviceState file you store and protect
PreviewChange setsterraform plan
ScopeAWS resourcesMany providers, including AWS

Git for infrastructure code

Store templates and Terraform files in a Git repository. Work on a branch, open a pull request for review, and merge after the checks pass. Tag the merge with a version, so you can return to an exact release.

Exam tip

When a question asks for the fastest rollback with no downtime, think blue/green. When it asks to test a release on a small share of users first, think canary.

What to remember for the exam

  • In place and rolling updates reuse the same instances, so rollback means redeploying.
  • Blue/green and immutable keep the old version intact, so rollback is fast.
  • CodeDeploy supports EC2 and on premises servers, Lambda and ECS, and can roll back on failure or on an alarm.
  • Elastic Beanstalk offers all at once, rolling, rolling with additional batch, immutable and traffic splitting.
  • Lambda aliases, weighted target groups and weighted Route 53 records shift traffic between versions.
  • Terraform uses init, plan and apply, and keeps state that should live in a remote, locked backend.
  • Keep infrastructure code in Git: branches, reviewed pull requests, tagged releases.

Common mistakes

Expecting weighted DNS to shift traffic instantly. Cached answers keep some clients on the old endpoint.

Keeping Terraform state on one laptop. Teammates overwrite each other. Use a remote backend with locking.

Editing resources by hand after a code based deployment. The code and the environment drift apart.

Watch out

Blue/green keeps two full environments for a time. Remove the old one once the new version is proven.

Check yourself

Quick questions.

  • Which strategy gives the fastest rollback with no downtime?

    Blue/green, because you switch traffic back to the untouched old environment.

  • How do you send 10 percent of Lambda traffic to a new version?

    Use an alias with weighted routing between the two versions, or a CodeDeploy canary configuration.

  • Where should a team keep Terraform state?

    In a shared remote backend, such as an S3 bucket, with locking enabled.

Topic 7 of 7 in Deployment and Automation

Test yourself on Deployment and Automation

The full AWS SOA-C03 question bank has 850 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by AWS. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.