LearnWithShobhan

Your first CloudFormation template: YAML anatomy and creating a stack (DynamoDB example)

Shobhan Biswas··4 min read
>_From the course VidShare: 109 lessons, lifetime access.

CloudFormation sounds heavy until you see how small a working template is. A dozen lines of YAML can create a real DynamoDB table, and one click deletes it again. This walk-through builds that template, explains every part, and creates a stack from it in the AWS console.

If you are still deciding whether infrastructure as code is worth it, read AWS CDK tutorial in TypeScript first. This article is the hands-on companion: the raw template that CDK generates for you later.

A minimal template

CloudFormation templates can be written in JSON or YAML. YAML is much easier to read, so we'll use that.

AWSTemplateFormatVersion: "2010-09-09"
Resources:
  MyDynamoDBTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: table-with-cloudformation
      BillingMode: PAY_PER_REQUEST
      AttributeDefinitions:
        - AttributeName: artist
          AttributeType: S
      KeySchema:
        - AttributeName: artist
          KeyType: HASH

What each part means

  • AWSTemplateFormatVersion: the format version of the template. You mostly set it once and forget it.
  • Resources: the important section. Everything you want AWS to create goes in here.
  • MyDynamoDBTable: the logical name. This is not the table's name in AWS. It is only a label inside this template, used when another resource needs to refer to this one.
  • Type: what kind of resource this is, here AWS::DynamoDB::Table.
  • Properties: the configuration. Anything you can choose in the console or CLI can be set here.
  • TableName: the real name the table gets in DynamoDB.

Create the stack in the console

A stack is the group of resources created from one template.

  1. Open the CloudFormation service in the AWS console. The service itself is free; you pay for the resources it creates.
  2. Click Create stack, then With new resources.
  3. Choose Template is ready and upload a template file. Select your YAML file. CloudFormation stores it in an S3 bucket for you, creating the bucket the first time you use the service.
  4. Give the stack a name, for example my-first-stack.
  5. Skip tags and the permissions role for now.
  6. Look at the stack failure options. This one matters. If your template has several resources and one fails, you can choose Roll back all stack resources, which undoes everything, or Preserve successfully provisioned resources, which keeps whatever worked. Most of the time resources depend on each other, so rolling back is the safer choice.
  7. Review and submit.

Watch it happen

On the stack page:

  • Events shows progress, from creation in progress to CREATE_COMPLETE. Refresh if it doesn't update.
  • Resources lists everything the stack created.
  • Template shows the exact file you submitted.

Now open the DynamoDB console. Your new table is there, created from a text file.

Delete it in one go

On the stack, choose Delete. The status moves to deletion in progress, and when it finishes the table is gone from DynamoDB too. With ten tables in one template, it would still be one delete. That is the real power: your infrastructure is described in a file you can version in Git, recreate in another region, or tear down cleanly.

Where this leads

Writing every property by hand gets tedious. The next step is generating this template from a programming language, which is what AWS CDK does. It turns your TypeScript into CloudFormation, with autocomplete catching mistakes before you deploy. See AWS CDK tutorial in TypeScript, and for a real stack in context, the serverless video streaming platform architecture.

Free to watch. Get all 109 lessons →

Key takeaways

  • ✓A CloudFormation template lists resources in a Resources section; YAML is the readable choice.
  • ✓The logical name is only a label inside the template; TableName is the real name in AWS.
  • ✓A stack groups the resources created from a template, and deleting the stack deletes them all.
  • ✓Choose rollback on failure when your resources depend on each other.

Build the whole thing, end to end

VidShare: Build a Video Streaming Platform on AWS is 109 lessons that take you from architecture to a working system: API Gateway, Lambda, DynamoDB, S3, MediaConvert, OpenSearch and CDK, with tests along the way. Lifetime access, and three lessons are free to watch.

Free download

Linux command line cheat sheet

One printable page: navigation, files, permissions, pipes, processes, scripts and SSH.

Occasional emails with new tutorials and courses. Unsubscribe any time.

Keep reading