← Cloud Practitioner study path

Practice in your account · 15 min

Your first AWS CloudFormation stack

In this lab you will create an S3 bucket from a template, see how CloudFormation creates it as part of a stack and check that the bucket disappears when you delete the stack.

In this lab you will describe an S3 bucket, which is a place to store files on AWS, in a text file, ask AWS CloudFormation to create it and then delete everything in a single step. This way of working is called infrastructure as code: instead of creating each resource with clicks in the console, you write in a file which resources you want, and CloudFormation creates them in order and deletes them when you ask it to. For example, if an online store needs the same buckets and servers in another Region, it doesn't have to repeat every click: it uses the same file and CloudFormation creates everything the same way.

Template, CloudFormation and stack

The template is the file that describes the resources, and the stack is the set of resources that CloudFormation creates from that template. CloudFormation treats the stack as a single thing: it creates it whole and deletes it whole. For example, with a single template the store can create a test stack and, when it finishes testing, delete the stack and everything that was in it along with it. The template in this lab has a single resource, but CloudFormation follows the same mechanism when a template has a hundred.

Before you start

This lab doesn't charge you, because CloudFormation doesn't charge for the stack and an empty bucket doesn't charge either. Even so, do the zero spend budget lab first, so the email warns you if something starts charging.

In the full account, choose the Region before you create the stack, because the stack and its bucket are created in the Region you have selected. In the new experience the stack ends up in us-east-2, and CloudFormation and CloudShell are on its free plan.

The template

The template is a file that describes the bucket. Copy this into a file named primera-pila.yaml:

AWSTemplateFormatVersion: "2010-09-09"
Description: >-
  cloud path, curso CLF-C02. Tu primera pila: un bucket S3 privado,
  cifrado y con versionado. El bucket vacio no cobra.

Resources:
  BucketPrimeraPila:
    Type: AWS::S3::Bucket
    DeletionPolicy: Delete
    UpdateReplacePolicy: Delete
    Properties:
      BucketEncryption:
        ServerSideEncryptionConfiguration:
          - ServerSideEncryptionByDefault:
              SSEAlgorithm: AES256
      VersioningConfiguration:
        Status: Enabled
      PublicAccessBlockConfiguration:
        BlockPublicAcls: true
        BlockPublicPolicy: true
        IgnorePublicAcls: true
        RestrictPublicBuckets: true
      Tags:
        - Key: cloudpath:lab
          Value: lab02

Outputs:
  NombreDelBucket:
    Description: Nombre que CloudFormation le puso al bucket.
    Value: !Ref BucketPrimeraPila
  Region:
    Description: Region donde quedo el bucket.
    Value: !Ref AWS::Region

Read the template before you create it. In the Resources section there is a resource named BucketPrimeraPila, of type AWS::S3::Bucket, with these four options:

  • , in BucketEncryption.
  • Versioning, in VersioningConfiguration, which keeps every change to a file instead of replacing it.
  • Public access blocked, in PublicAccessBlockConfiguration, so nobody outside your account can see what you store.
  • A , in Tags, which marks the bucket as part of the lab.

The template doesn't give the bucket a name, so CloudFormation makes one up from the stack name, and the Outputs section returns that name along with the Region. For example, if the stack is named cloudpath-lab02-primera-pila, the bucket name starts with those same words and ends with random letters that CloudFormation chose.

The line DeletionPolicy: Delete tells CloudFormation to delete the bucket when you delete the stack. That policy has a limit: if you upload files to the bucket, CloudFormation can't delete it, and the stack stays in DELETE_FAILED, which means the deletion failed, until you empty the bucket. For example, if you upload a photo to the bucket and then delete the stack, the photo blocks the deletion, and you have to delete it in S3 before you try again.

What happens to the bucket?

You create the stack with this template, you don't upload anything to the bucket and then you delete the stack. What happens to the bucket?

Steps in the console

With the template ready, create the stack from the CloudFormation console, the AWS web page where you manage your stacks with buttons:

  1. Open the CloudFormation console (https://console.aws.amazon.com/cloudformation).
  2. On the Stacks page, choose Create stack and then With new resources (standard).
  3. In Prerequisite - Prepare template, choose Choose an existing template. In Specify template, choose Upload a template file, then Choose file, and choose your primera-pila.yaml. Choose Next.
  4. In Stack name, enter cloudpath-lab02-primera-pila. This template has no parameters to fill in. Choose Next.
  5. In Configure stack options don't change anything and choose Next.
  6. In Review and create, choose Submit.
  7. Open the Events tab, where CloudFormation records everything it does. You will see CREATE_IN_PROGRESS (creating) for the stack and for the bucket, and then CREATE_COMPLETE (created).
  8. Open the Resources tab. The bucket is there with its physical ID, which is its real name. Open it and check in S3 that it has versioning enabled and the tag.

When you upload the file, CloudFormation stores it in an S3 bucket in your account, which it creates in each Region and whose name starts with cf-templates-. That bucket isn't part of your stack, so it isn't deleted with it, and it takes up a few . For example, if you create stacks from the console in Ohio and in São Paulo, you will have a cf-templates- bucket in each of those Regions. If you don't want it to be created, use the CloudShell option.

CloudShell option

CloudShell is a window inside the console where you type commands, which are text instructions, and the is the tool that understands those commands. With the CLI you pass the template straight to CloudFormation, so the cf-templates- bucket isn't created. Open CloudShell with the terminal icon in the console bar and create the file with nano primera-pila.yaml, which opens a simple text editor inside CloudShell, or upload it from Actions, Upload file. Then run these commands:

aws cloudformation create-stack \
  --stack-name cloudpath-lab02-primera-pila \
  --template-body file://primera-pila.yaml

aws cloudformation wait stack-create-complete --stack-name cloudpath-lab02-primera-pila

The first command creates the stack, and the second one waits for it to finish being created. For example, if the bucket takes a few seconds to be ready, CloudShell keeps waiting and doesn't let you type another command until the stack reaches CREATE_COMPLETE.

How to know you are done

The stack has to be in CREATE_COMPLETE, and the Outputs tab has to show the bucket name. You can also check it from CloudShell:

aws cloudformation describe-stacks \
  --stack-name cloudpath-lab02-primera-pila \
  --query "Stacks[0].[StackStatus,Outputs]" --output json

After you delete the stack, check that it was deleted:

aws cloudformation list-stacks \
  --stack-status-filter DELETE_COMPLETE \
  --query "StackSummaries[?StackName=='cloudpath-lab02-primera-pila'].[StackName,StackStatus,DeletionTime]" \
  --output table

Also look for the bucket by name with aws s3 ls | grep primera-pila, which lists your buckets and keeps only the ones with "primera-pila" in the name. That command shouldn't show anything, because the bucket was deleted with the stack.

Delete what you created

In this lab, deleting the stack is part of the exercise, because that way you check that the bucket goes with it. Follow these steps:

  1. On the Stacks page, select cloudpath-lab02-primera-pila.
  2. Choose Delete and confirm with Delete.
  3. In Events you will see DELETE_IN_PROGRESS, and at the end the stack disappears from the list. Deleted stacks aren't shown by default; to see them, change the status filter to Deleted.

From CloudShell, the stack is deleted with aws cloudformation delete-stack --stack-name cloudpath-lab02-primera-pila. If you uploaded files to the bucket, CloudFormation can't delete it and the stack stays in DELETE_FAILED; in that case, empty the bucket in the S3 console and delete the stack again.

The cf-templates- bucket stays in your account. If you want to remove it too, empty it and delete it from the S3 console, although CloudFormation creates another one the next time you upload a template from the console. For example, if tomorrow you do the EC2 lab with its template from the console, the cf-templates- bucket shows up again.

True or false?

0 of 4
  1. Deleting the stack also deletes the cf-templates- bucket that was created when you uploaded the template.

  2. If you uploaded files to the stack's bucket, the stack stays in DELETE_FAILED.

  3. CloudFormation charges you for having the stack created.

  4. Creating the stack from CloudShell doesn't create the cf-templates- bucket.

Next

  • Creating a stack on the CloudFormation console: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-create-stack.html
  • cloud path practice exams: /examenes?lang=en