(01)Install AWS CloudFormation
1sudo apt-get install -y awscli2aws --version3# CloudFormation uses AWS CLI — no separate installInfrastructure as code for AWS — stacks of EC2, VPC, RDS, and more
If you're on Terraform 1.7 or older
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.
Where to create or edit the main configuration — paths below match the setup steps.
template.yamlLocation: Local file or S3 — stack template
AWS resources as CloudFormation template
Step 01
1sudo apt-get install -y awscli2aws --version3# CloudFormation uses AWS CLI — no separate installStep 02
1aws configure2cat > vpc-stack.yaml << 'EOF'3AWSTemplateFormatVersion: "2010-09-09"4Description: Simple VPC5Resources:6 MyVPC:7 Type: AWS::EC2::VPC8 Properties:9 CidrBlock: 10.0.0.0/1610 EnableDnsSupport: true11 EnableDnsHostnames: true12EOF13aws cloudformation create-stack --stack-name my-vpc --template-body file://vpc-stack.yamlStep 03
1aws cloudformation describe-stacks --stack-name my-vpc2aws cloudformation validate-template --template-body file://vpc-stack.yamlStep 04
1aws cloudformation update-stack --stack-name my-vpc --template-body file://vpc-stack.yaml2aws cloudformation delete-stack --stack-name my-vpcStep 05
1aws cloudformation describe-stack-events --stack-name my-vpc | head -502aws cloudformation describe-stack-resources --stack-name my-vpc3# Delete failed stack and fix template:4aws cloudformation delete-stack --stack-name my-vpc1aws cloudformation validate-template --template-body file://vpc-stack.yaml2cfn-lint vpc-stack.yaml3 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
1terraform {2 required_providers {3 aws = {4 source = "hashicorp/aws"5 version = "~> 5.0"6 }7 }8}9 10provider "aws" {11 region = var.aws_region12}13 14resource "aws_security_group" "ubuntu_sg" {15 name = "ubuntu-sg"16 description = "Allow SSH and HTTP"17 18 ingress {19 from_port = 2220 to_port = 2221 protocol = "tcp"22 cidr_blocks = ["0.0.0.0/0"]23 }24 25 ingress {26 from_port = 8027 to_port = 8028 protocol = "tcp"29 cidr_blocks = ["0.0.0.0/0"]30 }31 32 egress {33 from_port = 034 to_port = 035 protocol = "-1"36 cidr_blocks = ["0.0.0.0/0"]37 }38}39 40resource "aws_instance" "ubuntu" {41 ami = var.ami42 instance_type = var.instance_type43 key_name = var.key_name44 vpc_security_group_ids = [aws_security_group.ubuntu_sg.id]45 46 tags = {47 Name = "Ubuntu-EC2"48 }49}variables.tf
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 region8}9 10variable "instance_type" {11 default = "t2.medium"12}13 14variable "key_name" {15 description = "Existing EC2 key pair name"16}outputs.tf
1output "instance_public_ip" {2 value = aws_instance.ubuntu.public_ip3}4 5output "instance_id" {6 value = aws_instance.ubuntu.id7}Step 01
1terraform init2terraform plan3terraform apply -auto-approve4terraform output instance_public_ip5ssh -i ~/.ssh/mykey.pem ubuntu@$(terraform output -raw instance_public_ip)1terraform destroy -auto-approve