Deploying A Highly Available Web Application On AWS Using CloudFormation

Deploying A Highly Available Web Application On AWS Using CloudFormation

Dhaval Baldha

29 Jan 2024

9 MINUTES READ

Introduction

AWS CloudFormation simplifies the process of provisioning and managing AWS resources by allowing you to define your infrastructure as code. In this blog post, we'll walk through a CloudFormation template that deploys a highly available web application on AWS. The infrastructure includes a Virtual Private Cloud (VPC), EC2 instances, an Elastic Load Balancer, Auto Scaling Group, and more.

CloudFormation Template Overview

The provided CloudFormation template begins with defining a VPC (MyVPC) with a specified CIDR block and DNS configurations. Subsequently, it creates a subnet (MySubnet) within the VPC in the Asia Pacific (Mumbai) region. This subnet will house the EC2 instances of our web application.

EC2 Instance and Elastic IP

The template then launches an EC2 instance (MyEC2Instance) using an Amazon Machine Image (AMI), specified instance type, key pair, and security group. An Elastic IP (MyElasticIP) is associated with this instance for a static public IP address.

Elastic Load Balancer and Listener

An Elastic Load Balancer (MyLoadBalancer) is created along with an associated listener (MyListener) that handles incoming HTTP traffic and responds with a fixed message. This ensures a basic health check for our web application.

Auto Scaling Group

To enhance availability and handle varying workloads, the template includes an Auto Scaling Group (MyAutoScalingGroup) with configurations for desired capacity, minimum and maximum sizes. The instances launched by this group are based on the specified Launch Template.

Security Group and UserData

A security group (MySecurityGroup) is defined to allow SSH and HTTP traffic to the instances. Additionally, the UserData script installs a simple web server to serve a "Hello, World!" message on port 80.

Deployment Steps

Accessing AWS CloudFormation:

  • Log in to the AWS Management Console.
  • Navigate to the CloudFormation service.

Launching the Stack:

  • Upload the provided CloudFormation template.
  • Yaml template

    AWSTemplateFormatVersion: '2010-09-09'
    Resources:
        MyVPC:
            Type: 'AWS::EC2::VPC'
            Properties:
                CidrBlock: '10.0.0.0/16'
                EnableDnsSupport: true
                EnableDnsHostnames: true
                Tags:
                    - Key: Name
                    Value: 'MyVPC'

        MySubnet:
            Type: 'AWS::EC2::Subnet'
            Properties:
                AvailabilityZone: 'ap-south-1'
                CidrBlock: '10.0.0.0/24'
                MapPublicIpOnLaunch: false
                VpcId: MyVPC
    Resources:
        MyEC2Instance:
            Type: 'AWS::EC2::Instance'
            Properties:
                ImageId: ami-0a7cf821b91bcccbc
                InstaceType: t2.micro
                KeyName: mumbai-region
                SecurityGroupIds:
                    - Ref: MySecurityGroup
            MyElasticIP:
                    Type: 'AWS::EC2::EIP'
            Properties:
                InstanceId: !Ref 
            MyListener:
                Type: 'AWS::ElasticLoadBalancingV2::Listener'
                Properties:
                    DefaultActions:
                    - Type: 'fixed-response'
                        FixedResponseConfig:
                        StatusCode: '200'
                        ContentType: 'text/plain'
                        Content: 'OK'
                LoadBalancerArn: !Ref MyLoadBalancer
                Port: 80
                Protocol: 'HTTP'

        MyAutoScalingGroup:
            Type: 'AWS::AutoScaling::AutoScalingGroup'
            Properties:
                DesiredCapacity: 2
                MinSize: 1
                MaxSize: 3
                VPCZoneIdentifier: !Ref 'MyVPC' # Replace with your subnet IDs
                LaunchTemplate:
                    Version: !GetAtt 'MyEC2Instance.LaunchTemplate.VersionNumber'
                    LaunchTemplateName: 'MyEC2Instance'
                TargetGroupARNs:
                    - !Ref MyTargetGroup
        MySecurityGroup:
            Type: 'AWS::EC2::SecurityGroup'
            Properties:
                GroupDescription: 'Allow SSH and HTTP traffic'
                SecurityGroupIngress:
                    - CidrIp: 0.0.0.0/0
                        IpProtocol: tcp
                        FromPort: 22
                        ToPort: 22
                    - CidrIp: 0.0.0.0/0
                        IpProtocol: tcp
                        FromPort: 80
                        ToPort: 80
                UserData:
                    Fn::Base64: |
                        #!/bin/bash
                        echo "Hello, World!" > index.html
                        nohup python -m SimpleHTTPServer 80 &
        

Monitoring Deployment:

  • Observe the stack creation progress in the CloudFormation console.
  • Monitor the creation of resources and address any errors that may arise.

Accessing the Web Application:

  • Once the stack is created successfully, access the web application through the Elastic Load Balancer's DNS name.

Conclusion

This blog post provided a hands-on guide to deploying a highly available Web application on AWS using CloudFormation. The template included essential components such as VPC, EC2 instances, Auto Scaling, Elastic Load Balancing, and more. By leveraging the power of Infrastructure as Code, you can easily replicate and manage your infrastructure, ensuring scalability, reliability, and ease of maintenance.

Harness the capabilities of AWS CloudFormation to streamline your deployment processes and empower your applications to scale seamlessly on the AWS cloud. Happy deploying!


Dhaval Baldha
Dhaval Baldha

Co-founder

Dhaval is a visionary leader driving technological innovation and excellence. With a keen strategic mindset and deep industry expertise, he propels the company towards new heights. His leadership and passion for technology make him a cornerstone of Techvoot Solutions' success.

Linkedin
Hire Skilled Developer

*Please fill all the required fields

Get our Newsletter

Customized solutions for your projects

*Please fill all the required fields