Add an Amazon S3 Bucket to the Stack

In this task, you will gain experience in editing a CloudFormation template and updating your CloudFormation stack

  • Your objective is to deploy a new Amazon S3 bucket

3.1 Edit the CloudFormation template file

  1. From the Amazon S3 Template Snippets documentation page, copy the YAML example for Creating an Amazon S3 Bucket with Defaults
  2. Edit the simple_stack.yaml file you downloaded earlier to include an Amazon S3 bucket
    • Under the Resources section add the snippet you copied
    • You do not require any Properties for this new S3 bucket resource
    • 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

Once you have edited the template, continue with the following steps to update the stack.

3.2 Update the CloudFormation stack - specify updated template

  1. Go to the AWS CloudFormation console
  2. Click on Stacks
  3. Click on the CloudFormationLab stack
  4. Click Update
  5. Now click Replace current template selected. You are replacing the template again.
  6. Click Upload a template file
  7. Click Choose file
    • Select simple_stack.yaml, your edited CloudFormation template file
  8. Click Next
  • At this point you may see an error where you remain on the Update stack screen and a red banner across the top of the page displays an error message
  • If you see Template format error then:
    • Check the indentation and punctuation in your simple_stack.yaml file
    • Once you have corrected the error, click Choose file again to reload you new corrected file

If you did not see an error you may proceed

3.3 Update the CloudFormation stack - complete the deployment

  1. On the Specify stack details click Next
  2. Click Next again, until you arrive at the Review CloudFormationLab screen
    1. Scroll down to Change set preview and note your S3 bucket is the only resource being added
    2. At the bottom of the page, select I acknowledge that AWS CloudFormation might create IAM resources with custom names
    3. Click Create stack
  3. When stack status is CREATE_COMPLETE for your update (about one minute) then continue
  4. 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
    • Note the name is cloudformationlab-mys3bucket-<some_random_string>.

The name for the S3 bucket was auto-generated by CloudFormation based on your CloudFormation stack name (converted to lowercase), plus the string “mys3bucket”, plus a randomly generated string.

  • The name for an S3 bucket must be unique across all S3 buckets in AWS
  • Your bucket was assigned an auto-generated name because you did not specify a name in the S3 bucket properties in your CloudFormation template
  • In the next exercise you will add a bucket name property for your S3 bucket and update the deployment

3.4 Assign name property for the S3 bucket

For this task you are going to specify a Parameter where you can set the bucket name. To do this you will add a property on the S3 bucket resource that uses this parameter.

  1. Under the Parameters section of your simple_stack.yaml template look at the S3BucketName parameter

    • It is not currently used in the template

        # S3 Bucket
        S3BucketName:
          Type: String
          Description: The name for the S3 bucket - must be unique across all of AWS (3-63 lowercase letters or numbers)
          Default: replaceme
          AllowedPattern: '^[a-z0-9]{5,40}$'
          ConstraintDescription: 3-63 characters; must contain only lowercase letters or numbers
      
    • It is a string for which we have configured certain constraints

    • The AllowedPattern is a regular expression specifying only lowercase letters or numbers and a string length between 3-63 characters

    • This satisfies the constraints on what is allowed in an S3 bucket name

    • It is actually more constrictive than what is allowed. See Rules for Bucket Naming under Bucket Restrictions and Limitations for more details.

  2. Add two more lines to your S3 bucket under in the Resources section of your template so it looks like this

    • Be caution to maintin the two-space indents where indicated

        MyS3Bucket:
          Type: AWS::S3::Bucket
          Properties:
            BucketName: !Ref S3BucketName
      
    • The Properties label defines that the items that follow (indented underneath) are properties of the S3 bucket

    • For the BucketName property you are specifying a reference to another value in the template. Specifically you are indicating that the string entered as the S3BucketName parameter should be used as the name of the bucket

  3. Go to the AWS CloudFormation console

  4. Click on Stacks

  5. Click on the CloudFormationLab stack

  6. Click Update

  7. Now click Replace current template selected. This is different from what you did for the last update.

  8. Click Upload a template file

  9. Click Choose file

    • Select simple_stack.yaml, your edited CloudFormation template file
  10. Click Next – Look for any errors reported

  11. On the Specify stack details look at the Parameters

    • You must enter a value for S3BucketName (you must replace the deafult value)
    • Remember it must be a name that no other bucket in all of AWS is already using
  12. Click Next again, until you arrive at the Review CloudFormationLab screen

    1. Scroll down to Change set preview and note your S3 bucket will be modified
    2. Note where it says Replacement is True. This means it will actually delete the current bucket and replace it with a new one with the newly specified name
    3. At the bottom of the page, select I acknowledge that AWS CloudFormation might create IAM resources with custom names
    4. Click Create stack
  13. When stack status is CREATE_COMPLETE for your update (about one minute) then continue

  • Under the resources see your newly named S3 bucket

Troubleshooting

  • If when trying to upload your new template you see Invalid template resource property
    • Check that the properties you specified for the resource you added match the properties in the documentation.
    • Once you have corrected the error, click Choose file again to reload you new corrected file
  • If your CloudFormation stack fails, then click on the Events tab and scroll down to find the source of the error
    • If you see a message like <your_chosen_bucket_name> already exists then re-do the CloudFormation update steps, but specify a more unique bucket name