Skip to main content

CloudFormation and Disaster Recovery Techniques in AWS

Hands-On Lab

 

Photo of Julie  Elkins

Julie Elkins

AWS Training Architect I in Content

Length

01:00:00

Difficulty

Advanced

In this hands on lab, we will investigate different techniques to use in AWS for disaster recovery. The lesson will present AWS disaster recovery techniques and quantify the appropriate use cases, based RTO/RPO, for each technique. We will then utilize a CloudFormation template to recover a large environment with many AWS resources. We will also look at a common risk in using CloudFormation for disaster recovery, which is that hard-coded AMI IDs can change. So, we will examine a CloudFormation template that has a Lambda function and can retrieve AMI IDs in real-time, and then we will execute this template and verify that an EC2 instance was created. Let's get started.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

CloudFormation and Disaster Recovery Techniques in AWS

Introduction

In this hands on lab, we will investigate different techniques to use in AWS for disaster recovery. The lesson will present AWS disaster recovery techniques and quantify the appropriate use cases, based RTO/RPO, for each technique. We will then utilize a CloudFormation template to recover a large environment with many AWS resources. We will also look at a common risk in using CloudFormation for disaster recovery, which is that hard-coded AMI IDs can change. So, we will examine a CloudFormation template that has a Lambda function and can retrieve AMI IDs in real-time, and then we will execute this template and verify that an EC2 instance was created. Let's get started.

Logging In

Please log in to the AWS environment by using the cloud_user credentials provided. Once inside the AWS account, make sure you are using us-east-1 (N. Virginia) as the selected region.

Verify that an S3 bucket is provided with the lab, and that it contains a JSON template and a YAML template (the templates are the same, as far as the resources they create).

Additionally, you will need to upload three additional files to the S3 bucket. Open the GitHub repository in a new browser tab.

Save the files to your local machine, so that they can be uploaded later.

Create an EC2 Key Pair

  1. Navigate to EC2.
  2. Click Key Pairs in the left-hand menu.
  3. Click Create Key Pair.
  4. Give it the name drkeypair.
  5. Leave the File format set to pem.
  6. Click Create key pair.
  7. The key pair will get created and downloaded.

Create a CloudFromation Stack

  1. Navigate to S3.
  2. Open the provided S3 bucket, click into the CF_Wordpress_Blog.yml file. Copy the Object URL.
  3. Navigate to CloudFormation.
  4. Click Create stack (choosing With new resources (standard) from the dropdown).
  5. On the next screen:
    • Set the Prerequisite - Prepare template section to Template is ready.
    • Choose Template source in the Specify template section
    • Paste the Object URL we copied earlier into the Amazon S3 URL box.
  6. Click Next.
  7. On the following screen:
    • Give this a Stack name of drscenario.
    • Choose our drkeypair from the WebServerKeyName dropdown.
  8. Click Next, Next, and then Create stack.
  9. Keep clicking the refresh button to watch things being created, and note the total elapsed time. It should be around three or four minutes.

Add a Script to the Bucket

  1. We should have the GitHub repository for the lab open in a browser tab. Get into that, click on the amilookup.zip file, then click the View raw link. This will download the file. Extract the file (amilookup.js) from the archive:
    • Methods will vary, based on your operating system. Right clicking on it in a file browser and choosing an Extract option is what usually happens.
  2. Navigate to S3.
  3. Click on the provided bucket.
  4. Click Upload.
  5. Click Add files.
  6. Choose the amilookup.js that we just got from the GitHub amilookup.zip.
  7. Click Next, Next, Next, and Upload.

Delete the Existing Stack

  1. Navigate to CloudFormation
  2. Select our drscenario stack, click the Delete button, then click the Delete stack button in the window that pops up.

Update the CF_Wordpress_Blog.yml Template

  1. Navigate back to S3.
  2. Click on the provided bucket.
  3. Click Upload.
  4. Click Add files.
    • At this point, if you haven't downloaded the whole GitHub repository, do that now and get it extracted so that we can access the individual files locally.
  5. Choose the CF_WordPress_Blog_Revised.json from the downloaded and extracted GitHub repository on your local drive.
  6. Click Next, Next, Next, and Upload.
  7. Click on that newly uploaded file, and copy its Object URL, like we did earlier for the original CF_Wordpress_Blog.yml file.

Create a New Stack

  1. Head back to CloudFormation.
  2. Click Create stack (choosing With new resources (standard) from the dropdown).
  3. On the next screen:
    • Set the Prerequisite - Prepare template section to Template is ready.
    • Choose Template source in the Specify template section
    • Paste the Object URL we copied for the newer template file, CF_WordPress_Blog_Revised.json, into the Amazon S3 URL box.
  4. Click Next, then Next again.
  5. On the following screen:
    • Give this a Stack name of drscenario2.
    • Choose our drkeypair from the WebServerKeyName dropdown.
  6. Click Next, Next, and then Create stack.

Ensure That All Required Files Are in the S3 Bucket

  1. Go back to the S3 dashboard and click into our bucket.
  2. We have amilookup.js, but we need AMILook.json too.
  3. Click Upload.
  4. Click Add files.
  5. Choose the AMILook.json from the downloaded and extracted GitHub repository on your local drive.
  6. Click Next, Next, Next, and Upload.
  7. Now click on the AMILook.json file, and copy its Object URL.

Create a Second Stack

  1. Get back into CloudFormation.
  2. Click Create stack (choosing With new resources (standard) from the dropdown).
  3. On the next screen:
    • Set the Prerequisite - Prepare template section to Template is ready.
    • Choose Template source in the Specify template section
    • Paste the Object URL we copied from the AMILook.json file into the Amazon S3 URL box.
  4. Click Next, then Next again.
  5. On the following screen:
    • Give this a Stack name of amilookup.
    • In the Parameters section, set these:
      • InstanceType: t2.micro
      • ModuleName: amilookup
      • S3Bucket: We need the name of the S3 bucket we've been uploading files to. Open up S3 in a new tab, get into the bucket, and copy its name to paste in here.
      • S3Key: amilookup.zip
  6. Click Next, then Next again.
  7. On this next screen, check the box that acknowledges the possibility that CloudFormation might create IAM resources, then click Create stack.

Check Our Work

Navigate back to EC2, and you can see that there are now some instances spinning up. We've essentially recovered from a mock disaster. Congratulations!