Sub topic 1
Why infrastructure as code, and where CloudFormation fits
Prerequisites
This chapter assumes you know the basics of S3, Lambda and IAM, can read TypeScript or Python, and have the AWS CLI configured with credentials. Node.js must be installed, because the CDK command line tool runs on it. You don't need to install anything yet.
Why infrastructure as code
Creating resources by clicking in the console is quick, but it is hard to repeat. Two environments drift apart, nobody remembers which setting was changed, and rebuilding after a mistake means guessing.
Infrastructure as code (IaC) describes resources in files that you keep in version control. This gives you:
- Repeatable environments: the same files produce the same resources in a test account and in production.
- Reviewable changes: a pull request shows exactly what will change.
- A recorded history: you can see who changed what, and roll back.
Where CloudFormation fits
AWS CloudFormation is the service that creates and updates resources from a template. A template is a JSON or YAML document that lists resources and their properties. CloudFormation deploys a template as a stack, which is one managed group of resources. When you change the template, CloudFormation works out the difference and updates only what changed. If an update fails, it rolls back.
Here is a minimal template for one bucket:
Resources:
UploadsBucket:
Type: AWS::S3::Bucket
Properties:
VersioningConfiguration:
Status: EnabledTemplates are precise, but they get long. Permissions, naming and repeated patterns must all be written out by hand, and YAML has no loops or functions.
How the CDK turns code into CloudFormation
The AWS Cloud Development Kit (CDK) lets you write infrastructure in a general-purpose language such as TypeScript or Python. The CDK does not replace CloudFormation. It generates templates, and CloudFormation still performs the deployment.
The process has three steps:
- Your program runs. It creates objects, called constructs, that represent resources.
- Synthesis happens when the program finishes. The CDK walks the constructs and writes a cloud assembly, a folder (
cdk.out) holding one template per stack plus any files to upload, such as Lambda code. - Deployment sends each template to CloudFormation, which creates or updates the stack.
You can run the first half without touching AWS:
cdk synthThis prints the generated template. Because the template is ordinary output, you can read it, diff it and test it before anything is deployed.
The language gives you what YAML lacks. You can use loops, functions, types and shared libraries. A few lines of code, such as granting a function read access to a bucket, expand into the full IAM policy in the template.
Exercise
You run cdk synth and it succeeds. Has anything been created in your AWS account? Explain what was produced.
Show answer
No. Synthesis only runs your program and writes a cloud assembly, including CloudFormation templates, to the local cdk.out folder. Resources are created only when cdk deploy sends the template to CloudFormation.