AWS CloudFormation is a service provided by Amazon Web Services (AWS) that enables developers and system administrators to create, manage, and provision AWS resources using Infrastructure as Code (IaC). It allows you to define your infrastructure in JSON or YAML templates, which are then used to automatically provision, configure, and update AWS services and resources.
Templates as Code: Infrastructure is defined using JSON or YAML templates, making it version-controllable and replicable across different environments.
Stack Management: A "stack" is a collection of AWS resources that you manage as a single unit. CloudFormation automates the process of creating, updating, and deleting these stacks.
Drift Detection: CloudFormation can detect if the actual configuration of AWS resources in a stack has deviated from the configuration defined in the template (called "drift").
Resource Dependencies: CloudFormation automatically handles dependencies between resources. For example, if a database instance needs to be created before an application server, CloudFormation ensures the correct order.
Update and Rollback: Stacks can be updated in a controlled manner, and if something goes wrong, CloudFormation supports rolling back to a previous known good state.
Cross-Stack References: You can share resources across different stacks, which improves modularity and reusability.
AWS Service Support: CloudFormation supports a wide range of AWS services, including EC2, S3, RDS, Lambda, and more.
- Template: The core of CloudFormation, a JSON or YAML file that describes your resources and their configurations.
- Stack: A collection of resources defined in a CloudFormation template. When you create a stack, CloudFormation provisions and configures the resources.
- Change Set: A preview of the changes that CloudFormation will make when you update a stack. It allows you to review potential modifications before applying them.
AWSTemplateFormatVersion: '2010-09-09'Resources:
MyEC2Instance:
Type: 'AWS::EC2::Instance'
Properties:
InstanceType: t2.micro
ImageId: ami-0abcdef1234567890
In this example, a basic EC2 instance is created using the specified instance type and image ID.
- Automating Infrastructure: Create, update, and manage infrastructure as code in a repeatable way.
- Environment Consistency: Deploy the same infrastructure across multiple environments (e.g., development, staging, production).
- Resource Management: Easily manage and track changes to infrastructure over time.
Step1: Create a CloudFormation template MyInfraSetupTemplate.yaml
Description: CloudFormation template to import an EC2 instance with existing VPC and Subnet.
Resources:
MyEC2Instance:
Type: 'AWS::EC2::Instance'
Properties:
InstanceType: t2.micro
KeyName: keypair # Ensure that this key pair exists
SecurityGroupIds:
- sg-073d0796e4533ade8 # <-- Replace with your existing Security Group ID
SubnetId: subnet-01647f388348b7bbc # <-- Replace with your existing Subnet ID
ImageId: ami-0522ab6e1ddcc7055 # <-- Replace with the correct AMI ID for your region
MyS3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: siraj-test-bucket
Step2: Validate the CloudFormation template
aws cloudformation validate-template --template-body file://MyInfraSetupTemplate.yaml
Step3: Create a CloudFormation stack
--stack-name MyStack \
--template-body file://MyInfraSetupTemplate.yaml \
--capabilities CAPABILITY_IAM
Step4: You can check CloudFormation stack creation progress
aws cloudformation describe-stack-events --stack-name MyStack
Step5: You can update CloudFormation stack (if needed)
--stack-name MyStack \
--template-body file://MyInfraSetupTemplate.yaml
Step6: You can see the created resources a S3 bucket and an EC2 instance running.
Step7: Delete CloudFormation stack and all its resources
aws cloudformation delete-stack --stack-name MyStack