DEV Community

Cover image for Deploying an Application Using CloudFormation with CDN Integration
AzeematRaji
AzeematRaji

Posted on

Deploying an Application Using CloudFormation with CDN Integration

Introduction:

This project automates the deployment of the application on AWS using Infrastructure as Code (IaC) with AWS CloudFormation. The infrastructure is divided into separate stacks: one for the network setup and one for the application deployment. The goal is to provision, configure, and tear down the necessary infrastructure with ease. It also integrates a Content Delivery Network (CDN) to enhance application performance and ensure global availability.

Infrastructure diagram

Project Overview

This project uses CloudFormation to provision the following resources:

  • VPC with public and private subnets
  • Load Balancer to handle HTTP/HTTPS traffic
  • EC2 instances running the application
  • S3 for static content storage
  • CloudFront for content delivery
  • IAM roles and policies for security

The deployment is divided into two independent stacks:

  • Network Stack: Managed by the network team, this stack provisions VPCs, subnets, and security groups.
  • Application Stack: Responsible for provisioning the application components, including EC2 instances, load balancers, S3, and CloudFront.

Prerequisites:

  • AWS account with appropriate permissions
  • AWSCLI Install awscli and configured
  • Basic knowledge of CloudFormation and its template structure.
  • CloudFormation templates for both the Network and Application stacks.
  • Scripts to create, update, deploy and delete the stacks.

Spin up instructions

To spin up your infrastructure using the provided scripts, follow the steps below:

  • script to create stack:
#!/bin/bash

# Usage: ./create.sh <stack-name> <template-file> <parameters-file>

aws cloudformation create-stack --stack-name $1 \
    --template-body file://$2   \
    --parameters file://$3  \
    --capabilities "CAPABILITY_NAMED_IAM"  \
    --region=us-east-1
Enter fullscreen mode Exit fullscreen mode

You can edit the scripts to either delete, deploy or update stack, the purpose of the scripts is to avoid running the multiple line commands everytime.

  • make the scripts executable

chmod +x <scripts.sh>

  • Use the create.sh script to create a new CloudFormation stack, create the network stack first since the application stack depends on it resources (like vpc, subnets)
  • The script requires three parameters (such as StackName, ParameterFile, and TemplateFile. You will need to pass these parameters as per the instructions in the script.

./create.sh StackName ParameterFile TemplateFile

In this case

./create.sh networkStack network-parameters.json network.yml

This script creates a new stack with the provided parameters.

  • Update an existing stack:

./update.sh StackName ParameterFile TemplateFile

This script will update the stack with new configurations based on the changes in the parameter or template file.

  • Deploy a stack:

./deploy.sh StackName ParameterFile TemplateFile

The deploy.sh script checks if the stack already exists. If it does, the script will update the stack; if it doesn't, it will create a new stack. It very useful for automation like CI/CD pipeline

./create.sh networkStack udagram-parameters.json udagram.yml

  • Confirm your application is live

CloudFrontURL: https://d1gjuuten5htu8.cloudfront.net

WebAppLBDNS: http://udagra-WebAp-mMkpfHQBWeXO-1923467063.us-east-1.elb.amazonaws.com

  • Tear down the deployed resources, follow these instructions:

./delete.sh StackName

Use the delete.sh script to delete a stack. The script only requires one parameter: the name of the stack you wish to delete.

Conclusion

  • Cloudformation streamlines resource creation and reduces manual errors.
  • Improves scalability, easy to replicate the setup in different environments.
  • CDN Integration improves performance and ensures low latency worldwide.

Top comments (0)