Deploy Infrastructure using a CloudFormation Stack

This lab illustrates best practices for reliability as described in the AWS Well-Architected Reliability pillar.

How do you implement change?

  • Best practice: Deploy changes with automation: Deployments and patching are automated to eliminate negative impact.
  • Design principle: Manage change in automation: Changes to your infrastructure should be made using automation. The changes that need to be managed include changes to the automation, which then can be tracked and reviewed.

When this lab is completed, you will have deployed and edited a CloudFormation template. Using this template you will deploy a VPC, an S3 bucket and an EC2 instance running a simple web server.

1.1 Log into the AWS console

If you are attending an in-person workshop and were provided with an AWS account by the instructor:

Click here for instructions to access your assigned AWS account:

If you are using your own AWS account:

Click here for instructions to use your own AWS account:

1.2 The CloudFormation template

You will begin by deploying a CloudFormation stack that creates a simple VPC as shown in this diagram:

SimpleVpcOnly

  1. Download the simple_stack.yaml CloudFormation template
  2. Open this file in a Text Editor
    • Preferably use an editor that is YAML aware like vi/vim, VS Code, or Notepad++
    • Do NOT use a Word Processor

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.

Look through the file. You will notice several sections:

  • The Parameters section is used to prompt for inputs that can be used elsewhere in the template. The template is asking for several inputs, but also provides default values for each one. Look through these and start to reason about what each one is.

  • The Conditions section is where you can setup if/then-like control of what happens during template deployment. It defines the circumstances under which entities are created or configured.

  • The Resources section is the “heart” of the template. It is where you define the infrastructure to be deployed. Look at the first resource defined.

    • It is the VPC (Amazon Virtual Private Cloud)
    • It has a logical ID which in this case is SimpleVPC. This logical ID is how we refer to the VPC resource within the CloudFormation template.
    • It has a Type which tells CloudFormation which type of resource to create
    • And it has Properties that define the values used to create the VPC
  • The Outputs section is used to display selective information about resources in the stack.

  • The Metadata section here is used to group and order how the CloudFormation parameters are displayed when you deploy the template using the AWS Console

CloudFormation tip
When editing CloudFormation templates written in YAML, be extra cautious that you maintain the correct number of spaces for each indentation
Indents are always in increments of two spaces. You can use your IDE (may require extensions) or a free online linter such as http://www.yamllint.com/ to identify and correct any syntax errors you may have within your YAML file.

You will now use this template to launch a CloudFormation stack that will deploy AWS resources in your AWS account.

1.3 Deploying an AWS CloudFormation stack to create a simple VPC

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

  2. 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 CFNUploadTemplateFile
  3. Click Next

  4. For Stack name use CloudFormationLab

  5. Parameters

    • Look over the Parameters and their default values.

    • Click Next

  6. For Configure stack options we recommend configuring tags, which are key-value pairs, that can help you identify your stacks and the resources they create. For example, enter Owner in the left column which is the key, and your email address in the right column which is the value. We will not use additional permissions or advanced options so click Next. For more information, see Setting AWS CloudFormation Stack Options.

  7. 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 CFNIamCapabilities
  8. 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. StackCreationStarted
  9. When it shows status CREATE_COMPLETE, then you are finished with this step.

  10. Deployment will take approximately 30 seconds to deploy.