Deploying Multi-tier Infrastructure & Updating using CloudFormation

Sameed Uddin Mohammed
7 min readDec 27, 2020

This hands-on lab will guide you through the steps to improve reliability of a service by using automation to deploy a reliable cloud infrastructure. When this lab is completed, you will have deployed two CloudFormation templates. The first will deploy an Amazon Virtual Private Cloud (VPC).

The second will deploy into your VPC, a reliable 3-tier infrastructure using Amazon EC2 distributed across three Availability Zones.

You will then review the features of the deployed infrastructure and learn how they contribute to reliability.

Task 1 :- Lets Start with creating VPC

  • Download the simple_stack.yaml CloudFormation template
  • Open this file in a Text Editor
  • Preferably use an editor that is YAML aware like vim, VS Code, or Notepad++

The template is written in a format called YAML , which is commonly used for configuration files. The format of the file is important, especially indents and hyphens. CloudFormation templates can also be written in JSON.

Simple VPC

Task 2 :- Deploying an AWS CloudFormation stack to create a simple VPC

Go to the AWS CloudFormation console at https://console.aws.amazon.com/cloudformation and click Create Stack > With new resources

CloudFormation

Click Create Stack, then With new resources (standard) Then Click Upload a template file (simple_stack.yaml was downloaded earlier) and then click Choose file.

  • Leave Prepare template setting as-is
  • For Template source select Upload a template file
  • Click Choose file and supply the CloudFormation template you downloaded: simple_stack.yaml

Enter the following details:

  • Stack name: The name of this stack. For this lab, use Simple-VPC and match the case.
  • Parameters: Parameters may be left as defaults, you can find out more in the description for each.
  • Tags for example Type: WebApp & Permissions using IAM role can also be attached as optional.

Quick Tip: change Instance Type :t2.micro as part of free tier on AWS

For Review

  • Review the contents of the page
  • At the bottom of the page, select I acknowledge that AWS CloudFormation might create IAM resources with custom names
  • Click Create stack

This will take you to the CloudFormation stack status page, showing the stack creation in progress.

  • Click on the Events tab
  • Scroll through the listing. It shows the activities performed by CloudFormation (newest events at top), such as starting to create a resource and then completing the resource creation.
  • Any errors encountered during the creation of the stack will be listed in this tab.
  • When it shows status CREATE_COMPLETE, then you are finished with this step.
  • Deployment will take approximately 30 seconds to deploy.

Return to the AWS CloudFormation console

  • Click the Resources tab for the CloudFormationLab stack. The listing shows all the resources that were created. In this case just the VPC
  • Note the Logical ID for the VPC is SimpleVPC. Look at the CloudFormation template file and determine where this name came from
  • Under the Resources tab click on the Physical ID link for SimpleVPC
  • This takes you to the VPC console where you can see the VPC you created
  • Select the checkbox next to your VPC (if not already selected)
  • Look at the VPC attributes under the Description tab.

Then Click on Stacks

  • Click on the CloudFormationLab stack > Click Update
  • Leave Use current template selected. You have not yet changed the template > Click Next
  • On the Specify stack details screen you now have the opportunity to change the Parameters
  • Change PublicEnabledParam to true > Click Next
  • Click Next again, until you arrive at the Review CloudFormationLab screen
  • Scroll down to Change set preview and note several resources are being added
  • At the bottom of the page, select I acknowledge that AWS CloudFormation might create IAM resources with custom names
  • Click Create stack.
  • When stack status is CREATE_COMPLETE for your update (about one minute) then continue.

The current deployment is now represented by this architecture diagram:

Task 3 :-Edit the CloudFormation template file

Edit the simple_stack.yaml file you downloaded earlier to include an Amazon S3 bucket using below yaml template.

MyS3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref S3BucketName
  • Under the Resources section add the snippet you copied
  • Additionally it will help you create the S3 Bucket with custom name in Properties section
  • Indents are important in YAML — use two spaces for each indent. Look at the other resources for guidance
  • The correct solution only needs two lines — one for the Logical ID and one for the Type & Save the template.

Task 4 :- Update the CloudFormation stack — specify updated template

  1. Go to AWS CloudFormation console >Click on Stacks > Click on the stack
  2. Click Update again >Now click Replace current template selected
  3. Click Upload a template file >Click Choose file >Select simple_stack.yaml, your edited CloudFormation template file > Click Next > On the Specify stack details look at the Parameters
  4. You must enter a value for S3BucketName (you must replace the deafult value)
  5. Remember it must be a name that no other bucket in all of AWS is already using > click Next
  6. Click Next again, until you arrive at the Review CloudFormationLab screen
  7. Scroll down to Change set preview and note your S3 bucket is the only resource being added

At the bottom of the page, select I acknowledge that AWS CloudFormation might create IAM resources with custom names

  1. Click Create stack
  2. When stack status is CREATE_COMPLETE for your update (about one minute) then continue > Click the Resources tab
  • Note your new S3 bucket is listed among the resources deployed
  • Click on the Physical ID of the S3 bucket to view the bucket on the S3 console

Task 5 :- Add the EC2 instance resource to your CloudFormation template and deploy it

  1. Edit the CloudFormation Template, adding a new resource for an EC2 instance

2. Use this documentation page for assistance: AWS::EC2::Instance

  • Use the YAML format
  • For Logical ID (the line above Type) use MyEC2Instance
  • You only need to specify these six properties:
  • IamInstanceProfile: Refer to Web1InstanceInstanceProfile, which is defined elsewhere in the template
  • ImageId: Refer to LatestAmiId, which is the parameter discussed previously
  • InstanceType: Refer to InstanceType, another parameter
  • SecurityGroupIds: Refer to PublicSecurityGroup, which is defined elsewhere in the template
  • SubnetId: Refer to PublicSubnet1, which is defined elsewhere in the template
  • Tags: Use this YAML block:

Tags:
- Key: Name
Value: Simple Server

Remember

  • When referring to other resources in the same template, use !Ref. See the BucketName example you already implemented
  • When referring to SecurityGroupIds, the template is actually expecting a list of security groups. You therefore need to list the security group like this:
SecurityGroupIds:
- !Ref PublicSecurityGroup

3. Once you have edited the template, update the stack deployment with your revised template file.

  • On the Parameters screen of the CloudFormation update switch EC2SecurityEnabledParam to true
  • Change EC2SecurityEnabledParam to true
  • This will tell the template to create resources your EC2 instance will need such as the Security Group and IAM Role
  • This deployment of the CloudFormation stack will take about three minutes
  • The instance will now be displayed in the Resources tab.

4. Go to the EC2 console to see the Simple Server that was created. Explore the properties of this EC2 instance.

The final deployment is now represented by this architecture diagram:

Task 6 :- Remove AWS CloudFormation provisioned resources

You will now delete the CloudFormationLab stack.

How to delete an AWS CloudFormation stack

  1. Go to the AWS CloudFormation console: https://console.aws.amazon.com/cloudformation
  2. Select the CloudFormation stack to delete and click Delete
  3. In the confirmation dialog, click Delete stack
  4. The Status changes to DELETE_IN_PROGRESS
  5. Click the refresh button to update and status will ultimately progress to DELETE_COMPLETE
  6. When complete, the stack will no longer be displayed. To see deleted stacks use the drop down next to the Filter text box.
  7. To see progress during stack deletion
  • Click the stack name
  • Select the Events column
  • Refresh to see new events

Feel free to customize according to your own way & experiment it using different services and resources.

In the next post ill show you how deploy similar resources but in faster more efficient way using Terraform (infra-as-a-code) if you are interested.

Please click the clap 👏 button below to encourage & support me to keep posting more content.

--

--

Sameed Uddin Mohammed

Terraform|3x-AWS|2x-Azure|2x-GCP|CertifiedCloud/DevOpsEngineerLooking for better opportunities- Remote| Join me https://chat.whatsapp.com/EiCi7XYnCSD7BQnPz2mJIa