Automated Infrastructure Deployment on AWS with CloudFormation: A Comprehensive Guide
Introduction :-
CloudFormation template is a blueprint for creating a foundational AWS infrastructure for a web application. CloudFormation is #Amazon Web Services’ (AWS) service for defining and provisioning #AWS infrastructure as code. This template, written in YAML, outlines the necessary resources and configurations to deploy an #EC2 instance, an #Application Load Balancer (ALB), security groups, subnets, and more. By using this template, you can automate the setup of this infrastructure, ensuring consistency, repeatability, and scalability in your AWS environment.
This is a CloudFormation template written in YAML that defines a stack for creating an #AWS infrastructure. Here’s a breakdown of what this template does:
AWSTemplateFormatVersion
: Specifies the template format version.Description
: Provides a description of the CloudFormation template.Parameters
: Defines input parameters that can be customized when creating the stack. In this case, it defines a parameter namedKeyName
for an existing EC2 KeyPair.Resources
: Defines the AWS resources to be created as part of this CloudFormation stack
MyVPC
: Creates an Amazon VPC (Virtual Private Cloud) with the specified properties, including the CIDR block.MySubnet
: Creates an Amazon EC2 Subnet within the VPC, with the specified properties, including the VPC it belongs to and the CIDR block.MyALB
: Creates an #Application Load Balancer (ALB) with the specified properties, including subnets, scheme, and load balancer attributes.MyTargetGroup
: Creates a target group for the ALB with properties such as health check settings and protocol.MyListener
: Creates an ALB listener with default actions.MySecurityGroup
: Defines a security group for the EC2 instance with ingress rules for SSH and HTTP access.MyEC2Instance
: Launches an EC2 instance with specified properties, including the instance type, security groups, key pair, Amazon Machine Image (AMI), subnet, and user data to install and configure a basic web server.
5. Outputs
: Defines the outputs of the CloudFormation stack. In this case, it provides the URL of the ALB by using the Fn::GetAtt
function to retrieve the DNS name of the ALB.
Deployment Steps
Follow these steps to upload and create the CloudFormation stack using the AWS Management Console:
- Sign in to the #AWS Management Console: Log in to your AWS account if you haven’t already.
2. Navigate to CloudFormation: Go to the #AWS CloudFormation service from the AWS Management Console.
3. Click the “Create stack” button.
4. Upload the CloudFormation template file (YAML).
Yamlfile :
AWSTemplateFormatVersion: "2010-09-09"
Description: CloudFormation template for creating an EC2 instance with ALB, listener, and target group.
Parameters:
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the EC2 instance.
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: Must be the name of an existing EC2 KeyPair.
Resources:
MyVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: "10.0.0.0/16"
EnableDnsSupport: true
EnableDnsHostnames: true
MySubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId:
Fn::Ref: MyVPC
CidrBlock: "10.0.0.0/24"
AvailabilityZone: "us-east-1a"
MyALB:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Name: MyALB
Subnets:
- Ref: MySubnet
Scheme: internet-facing
LoadBalancerAttributes:
- Key: idle_timeout.timeout_seconds
Value: "60"
MyTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
Name: MyTargetGroup
Protocol: HTTP
Port: 80
VpcId:
Fn::Ref: MyVPC
HealthCheckIntervalSeconds: 30
HealthCheckProtocol: HTTP
HealthCheckPath: /index.html
HealthCheckPort: traffic-port
Matcher:
HttpCode: 200
MyListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: fixed-response
FixedResponseConfig:
ContentType: text/plain
StatusCode: 200
LoadBalancerArn:
Fn::Ref: MyALB
Port: 80
Protocol: HTTP
MySecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable HTTP and SSH access
VpcId:
Fn::Ref: MyVPC
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
FromPort: 22
IpProtocol: tcp
ToPort: 22
- CidrIp: 0.0.0.0/0
FromPort: 80
IpProtocol: tcp
ToPort: 80
MyEC2Instance:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
SecurityGroups:
- Fn::Ref: MySecurityGroup
KeyName:
Fn::Ref: KeyName
ImageId: <Your_AMI_ID>
SubnetId:
Ref: MySubnet
UserData:
Fn::Base64:
Fn::Sub: |
#!/bin/bash -xe
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "Hello, World!" > /var/www/html/index.html
Outputs:
URL:
Description: The URL of the ALB.
Value:
Fn::GetAtt:
- MyALB
- DNSName
Enter a Stack name for your deployment.
Provide parameter values as needed.
Review and acknowledge the capabilities .
You can set additional stack options or tags if necessary.
6. Review and Create:
Review the stack details and configuration.
Click “Create stack” to initiate the deployment.
7. Monitor Stack Creation:
The CloudFormation stack creation process will begin.
Monitor the stack events in the AWS Management Console.
Conclusion:-
CloudFormation template serves as a powerful tool for automating the deployment of an #AWS infrastructure for web applications. It encapsulates key components such as #VPC, subnets, an ALB, and an EC2 instance, along with their configurations. When launched, it will enable the quick and reliable creation of a web server setup, saving time and reducing the risk of manual errors. With the ability to customize parameters and the infrastructure’s scalability, this template can serve as a foundation for more complex and robust web services, making #AWS resources easier to manage and maintain.