# AWS CloudFormation

> Describe your AWS infrastructure in a text file; AWS builds it exactly like that.

- Level: Intermediate
- Topics: AWS, Cloud
- URL: https://tidbit.legios.com.ar/aws-cloudformation/
- In Spanish: [AWS CloudFormation](https://tidbit.legios.com.ar/es/aws-cloudformation/)

**AWS CloudFormation** is the AWS service for infrastructure as code. Instead of clicking through the console to create a VPC, two servers, a database and the permissions between them, you write a **template** (a YAML or JSON file) that lists those resources and their settings. You hand the template to CloudFormation and it creates everything, in the right order, as a **stack**.

The value shows up the second time. Need the same environment for testing? Deploy the same template under another name. Need a bigger database? Edit the file, update the stack, and CloudFormation works out the difference and applies only that. Did the change break? It rolls back to the previous state. The template lives in Git, so every change to the infrastructure has an author, a review and a history.

Example: a 150-line template defines an AWS Lambda function, its IAM role, an Amazon API Gateway in front and an Amazon DynamoDB table. One command (`aws cloudformation deploy`) creates all four in a couple of minutes; deleting the stack deletes all four, with nothing forgotten and still billing.

Raw templates get verbose, so many teams generate them: the AWS CDK lets you describe the same resources in TypeScript or Python code that compiles to CloudFormation, and AWS SAM is a shorthand for serverless apps. Terraform is the popular alternative that works across clouds.

## An analogy

A LEGO instruction booklet. Anyone with the booklet builds the same model, and when you want a second one you do not design it again, you follow the booklet.

## Keep in mind

- A template (YAML or JSON) describes resources; a stack is the deployed result.
- Updates are diffs: CloudFormation changes only what differs, and rolls back on failure.
- Deleting a stack removes every resource it created.
- CDK and SAM generate CloudFormation templates from code; Terraform is the multi-cloud alternative.

## Go deeper

- [What is AWS CloudFormation? (official docs)](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)
- [AWS CloudFormation product page](https://aws.amazon.com/cloudformation/)

## Related terms

- [Infrastructure as code](https://tidbit.legios.com.ar/infrastructure-as-code/): Describing servers, networks and databases in files instead of clicking.
- [Amazon VPC](https://tidbit.legios.com.ar/amazon-vpc/): Your own private slice of the AWS network, with walls you draw yourself.
- [Server](https://tidbit.legios.com.ar/server/): A computer whose job is to answer requests from other computers, all day, every day.
- [Database](https://tidbit.legios.com.ar/database/): An organized store of data that programs can search and update safely.
- [JSON](https://tidbit.legios.com.ar/json/): The plain-text format programs use to hand each other structured data.
- [Git](https://tidbit.legios.com.ar/git/): A time machine for code: every change saved, by whom and why.
- [AWS Lambda](https://tidbit.legios.com.ar/aws-lambda/): Run a piece of code only when something happens, with no server to manage.
- [IAM](https://tidbit.legios.com.ar/iam/): The rulebook that says who can do what in your AWS account.
