From Zero to Hero: Launching Your Frontend Application on Amazon S3 and CloudFront with CloudFormation

From Zero to Hero: Launching Your Frontend Application on Amazon S3 and CloudFront with CloudFormation

Introduction :-

AWS CloudFormation is a powerful #infrastructure as code (IaC) service that simplifies and automates the process of provisioning and managing AWS resources. It enables you to define your infrastructure using templates, making it easier to consistently deploy and manage applications and services.

In the context of hosting a frontend application, it’s common to leverage #Amazon S3 for storage and #Amazon CloudFront for content delivery. #Amazon S3 is a scalable and cost-effective storage solution, while #CloudFront is a content delivery network (CDN) that accelerates the distribution of your content globally. Together, they provide a reliable and performant platform for serving web applications to users worldwide.

In this script:

  • The Parameters section allows you to specify the S3 bucket name for hosting the frontend application and an optional custom error document (defaulting to index.html).

  • The Resources section defines two resources: an S3 bucket for hosting the website and a CloudFront distribution for content delivery.

  • The WebsiteBucket resource creates an S3 bucket and configures it to host a static website with the specified index document and error document.

  • The CloudFrontDistribution resource creates a CloudFront distribution with the S3 bucket as its origin, setting the default root object and cache behavior.

  • The Outputs section provides the URL of the deployed frontend application through the CloudFront distribution.

You can use this CloudFormation script as a starting point for deploying a basic frontend application on Amazon S3 and CloudFront. Customize the parameters as needed, and then create a CloudFormation stack using this template to deploy your frontend application.

Deployment Steps

Follow these steps to upload and create the CloudFormation stack using the AWS Management Console:

  1. 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).

AWSTemplateFormatVersion: '2010-09-09'
Description: AWS CloudFormation Template for Deploying a Frontend Application on S3 and CloudFront

Parameters:
  WebsiteBucketName:
    Description: S3 bucket name for hosting the frontend application
    Type: String
  ErrorDocument:
    Description: Custom error document (e.g., 404.html)
    Type: String
    Default: index.html

Resources:
  WebsiteBucket:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: !Ref WebsiteBucketName
      AccessControl: PublicRead
      WebsiteConfiguration:
        IndexDocument: index.html
        ErrorDocument: !Ref ErrorDocument

  CloudFrontDistribution:
    Type: 'AWS::CloudFront::Distribution'
    Properties:
      DistributionConfig:
        Origins:
          - Id: S3Origin
            DomainName: !GetAtt WebsiteBucket.DomainName
            S3OriginConfig:
              OriginAccessIdentity: ''
        DefaultRootObject: index.html
        DefaultCacheBehavior:
          TargetOriginId: S3Origin
          ForwardedValues:
            QueryString: 'false'
          ViewerProtocolPolicy: redirect-to-https

Outputs:
  WebsiteURL:
    Description: URL of the deployed frontend application
    Value: !GetAtt CloudFrontDistribution.DomainName

5. Specify Stack Details:

  • 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 :-

In conclusion, this #CloudFormation script serves as a foundational template for deploying a frontend application on Amazon S3 and #CloudFront. It enables you to automate the creation of resources and configurations, making it easy to maintain and update your deployment as your application evolves.

By defining the #S3 bucket and CloudFront distribution as AWS resources, you gain the benefits of scalability, security, and performance that AWS provides. The script also allows for customization by specifying the S3 bucket name and an optional custom error document. With the #CloudFormation stack created from this script, you can launch your frontend application with confidence, knowing that it’s built on AWS’s reliable and robust infrastructure.