3.9 The basics of AWS CloudFormation for automating infrastructures

Learning Objectives

By the end of this lesson you will be able to:

Create a CloudFormation Stack

  1. Go to the CloudFormation stack in the AWS Management Console. Click the Create New Stack button.
  2. In the Stack Name box, type a Stack name. For this example, use DITCJenkinsStack. The Stack Name must not contain spaces. Click ‘Provide a Template URL.’ In the box below, type or paste this URL: https://stelligentlabs.s3.amazonaws.com/templates/jenkins.template. The template creates a Jenkins environment. Click Continue.
  3. In the Key Name box, enter ditc as your EC2 Key Pair - from the key pair you created in sublesson 3.4. Click Continue and then click Close. Your stack may take several minutes to create
  4. The progress of the stack creation is shown in the top portion of the CloudFormation tab. The status for DITCJenkinsStack should be CREATE_IN_PROGRESS
  5. Select the checkbox next to the DITCJenkinsStack stack. In the bottom pane, click the Events tab. The Events Tab displays each step in the creation of the stack. The CREATE_COMPLETE event is shown when a resource has been successfully created. If AWS CloudFormation cannot create a particular resource, it displays a CREATE_FAILED event and rolls back the stack.
  6. When the stack DITCJenkinsStack has the status CREATE_COMPLETE, AWS CloudFormation has finished creating the stack and you can start using its resources. The sample Jenkins stack installs and configures a Jenkins CI application onto a Tomcat server. To complete the Jenkins installation, click on the link in the JenkinsURL row to launch the Jenkins application.

Delete a CloudFormation Stack

  1. Go to the AWS CloudFormation Console, select DITCJenkinsStack and click Delete Stack. In the confirmation message that appears click Yes, Delete. The status for DITCJenkinsStack changes to DELETE_IN_PROGRESS. In the same way you monitored the creation of the stack, you can monitor its deletion using the Events tab. When AWS CloudFormation completes the deletion of stack, it removes the stack from the list.

Identify CloudFormation template components

  1. Open the CloudFormation template at https://github.com/stelligent/devopsinthecloud/blob/master/infrastructure/templates/jenkins.template .
  2. Review the six top-level objects: AWSTemplateFormatVersion, Description, Parameters, Resources, Mappings, Outputs in the jenkins.template file. The Resources object is the only object that's required
  3. The Resources object contains the definitions of the AWS resources you want to create with the template. Each resource is listed separately and specifies the properties necessary for creating that particular resource. The resource declaration begins with a String that specifies the logical name for the resource. The logical name can be used to refer to the resources within the template.
  4. You use the Parameters object to declare values that can be passed to the template when you create the Stack. A parameter is an effective way to specify sensitive information - such as usernames/passwords - that you don't want to store in the template itself.
  5. You use Mappings to declare conditional values that are evaluated in a similar manner as a switch statement. The template uses Mappings to select the correct Amazon Machine Image for the Region and the Architecture Type for The Instance Type.
  6. Outputs define custom values that are returned by the cfn-describe-stacks command and in the AWS Management Console Outputs tab after the stack is created. You can use Output values to return information from the resources in the stacks such as the URL for a website created in the template.