📚
Cloud CLI

AWS CloudFormation

Infrastructure as code for AWS — stacks of EC2, VPC, RDS, and more

🐧 Linux🍎 Mac🪟 Windows
Reviewed: Tested on: Kubernetes 1.29 · Terraform 1.8 · Ubuntu 22.04

If you're on Terraform 1.7 or older

  • S3 native locking (use_lockfile) differs from DynamoDB — don't mix backends mid-migration
  • Provider version constraints: run terraform init -upgrade after bump
  • terraform test (1.6+) replaces some external test harness patterns

Alternative — team standard for Infrastructure as Code

What is this?

CloudFormation creates AWS resources (VPCs, EC2, RDS) from YAML templates — like Terraform for AWS only.

📁

Config files for AWS CloudFormation

Where to create or edit the main configuration — paths below match the setup steps.

  • template.yaml

    Location: Local file or S3 — stack template

    AWS resources as CloudFormation template

📥

Step 01

Install AWS CloudFormation

(01)Install AWS CloudFormation

Linux
1sudo apt-get install -y awscli
2aws --version
3# CloudFormation uses AWS CLI — no separate install
⚙️

Step 02

Configure AWS CloudFormation

(01)Configure AWS CloudFormation

Linux
1aws configure
2cat > vpc-stack.yaml << 'EOF'
3AWSTemplateFormatVersion: "2010-09-09"
4Description: Simple VPC
5Resources:
6 MyVPC:
7 Type: AWS::EC2::VPC
8 Properties:
9 CidrBlock: 10.0.0.0/16
10 EnableDnsSupport: true
11 EnableDnsHostnames: true
12EOF
13aws cloudformation create-stack --stack-name my-vpc --template-body file://vpc-stack.yaml

Step 03

Verify

(01)Verify installation

Linux
1aws cloudformation describe-stacks --stack-name my-vpc
2aws cloudformation validate-template --template-body file://vpc-stack.yaml

Step 04

Manage AWS CloudFormation

(01)Common tasks

Linux
1aws cloudformation update-stack --stack-name my-vpc --template-body file://vpc-stack.yaml
2aws cloudformation delete-stack --stack-name my-vpc
🔧

Step 05

Common Problems

#1Stack stuck in CREATE_IN_PROGRESS or ROLLBACK

Linux
1aws cloudformation describe-stack-events --stack-name my-vpc | head -50
2aws cloudformation describe-stack-resources --stack-name my-vpc
3# Delete failed stack and fix template:
4aws cloudformation delete-stack --stack-name my-vpc

#2Template validation failed

Linux
1aws cloudformation validate-template --template-body file://vpc-stack.yaml
2cfn-lint vpc-stack.yaml

📋Config templates

3 YAML templates for AWS CloudFormation. Copy and deploy after setup.

3 ready-to-copy templates. Expand one, copy the YAML, then run the deploy commands.

main.tf

yaml
1terraform {
2 required_providers {
3 aws = {
4 source = "hashicorp/aws"
5 version = "~> 5.0"
6 }
7 }
8}
9
10provider "aws" {
11 region = var.aws_region
12}
13
14resource "aws_security_group" "ubuntu_sg" {
15 name = "ubuntu-sg"
16 description = "Allow SSH and HTTP"
17
18 ingress {
19 from_port = 22
20 to_port = 22
21 protocol = "tcp"
22 cidr_blocks = ["0.0.0.0/0"]
23 }
24
25 ingress {
26 from_port = 80
27 to_port = 80
28 protocol = "tcp"
29 cidr_blocks = ["0.0.0.0/0"]
30 }
31
32 egress {
33 from_port = 0
34 to_port = 0
35 protocol = "-1"
36 cidr_blocks = ["0.0.0.0/0"]
37 }
38}
39
40resource "aws_instance" "ubuntu" {
41 ami = var.ami
42 instance_type = var.instance_type
43 key_name = var.key_name
44 vpc_security_group_ids = [aws_security_group.ubuntu_sg.id]
45
46 tags = {
47 Name = "Ubuntu-EC2"
48 }
49}

variables.tf

yaml
1variable "aws_region" {
2 default = "us-east-1"
3}
4
5variable "ami" {
6 description = "Ubuntu AMI ID for your region"
7 default = "ami-0c7217cdde317cfec" # Ubuntu 22.04 us-east-1 — update for your region
8}
9
10variable "instance_type" {
11 default = "t2.medium"
12}
13
14variable "key_name" {
15 description = "Existing EC2 key pair name"
16}

outputs.tf

yaml
1output "instance_public_ip" {
2 value = aws_instance.ubuntu.public_ip
3}
4
5output "instance_id" {
6 value = aws_instance.ubuntu.id
7}
📄

Step 01

Provision EC2

(01)Initialize and apply

Linux
1terraform init
2terraform plan
3terraform apply -auto-approve
4terraform output instance_public_ip
5ssh -i ~/.ssh/mykey.pem ubuntu@$(terraform output -raw instance_public_ip)

(02)Destroy when done

Linux
1terraform destroy -auto-approve