How to Create an EC2 Instance Using CloudFormation
Start automating your AWS infrastructure today with CloudFormation!
CloudFormation is an AWS service that allows you to define
and provision AWS infrastructure using code. By writing a CloudFormation
template, you can automate the creation of resources like EC2 instances, making
your deployments consistent and repeatable.
In this blog, we will walk through the process of creating
an EC2 instance using CloudFormation.
Prerequisites
- An
AWS account with appropriate permissions to create EC2 instances and other
resources.
- Basic
understanding of YAML/JSON and AWS services.
- AWS
CLI or AWS Management Console access.
Step 1: Write the CloudFormation Template
Below is a simple YAML template to create an EC2 instance:
AWSTemplateFormatVersion: '2010-09-09'
Description: Template to create an EC2
instance
Resources:
MyEC2Instance:
Type: "AWS::EC2::Instance"
Properties:
InstanceType: "t2.micro"
KeyName: "your-key-pair-name" # Replace with your actual key
pair name
ImageId: "ami-0abcdef1234567890" # Replace with the desired
AMI ID
SecurityGroupIds:
- !Ref MySecurityGroup
SubnetId: "subnet-0abcd1234efgh5678" # Replace with your
subnet ID
MySecurityGroup:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupDescription: "Enable SSH access"
VpcId: "vpc-0abcd1234efgh5678" # Replace with your VPC ID
SecurityGroupIngress:
- IpProtocol: "tcp"
FromPort: 22
ToPort: 22
CidrIp: "0.0.0.0/0"
Step 2: Validate the Template
Before deploying the template, validate it using the AWS
Management Console or AWS CLI:
Using AWS CLI:
aws CloudFormation validate-template --template-body
file://template.yaml
This command ensures the syntax and resources in your
template are correct.
Step 3: Deploy the CloudFormation Stack
Using AWS Management Console:
- Go
to the CloudFormation section in the AWS Management Console.
- Click
Create Stack > With New Resources (Standard).
- Upload
the template file or paste it into the editor.
- Follow
the prompts, providing any required parameters.
- Review
and click Create Stack.
Using AWS CLI:
Run the following command to create a stack:
aws cloudformation create-stack \
--stack-name MyEC2Stack \
--template-body file://template.yaml \
--parameters ParameterKey=KeyName,ParameterValue=your-key-pair-name \
ParameterKey=SubnetId,ParameterValue=subnet-0abcd1234efgh5678 \
ParameterKey=VpcId,ParameterValue=vpc-0abcd1234efgh5678
Step 4: Verify the Deployment
- Navigate
to the CloudFormation section in the AWS Management Console.
- Check
the status of your stack. Once it reaches CREATE_COMPLETE, your EC2
instance is ready.
- Go
to the EC2 section in the AWS Management Console to see your
instance.
Step 5: Clean Up
To avoid incurring costs, delete the CloudFormation stack
when you're done:
Using AWS Management Console:
- Select
the stack in the CloudFormation section.
- Click
Delete Stack.
Using AWS CLI:
aws cloudformation delete-stack
--stack-name MyEC2Stack
Conclusion
Using CloudFormation to create an EC2 instance simplifies
infrastructure management and ensures consistent deployments. This approach is
especially beneficial for teams practicing Infrastructure as Code (IaC).
Experiment with more advanced configurations to tailor your deployments to your
needs.
0 Comments