Deploy a DynamoDB table to AWS using the Serverless Framework

Nik Graf
InstructorNik Graf
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 3 years ago

The Serverless Framework uses CloudFormation to deploy our functions. Using the resources section we can deploy and configure a wide range of AWS services using raw CloudFormation definitions. In this session we demonstrate this by adding a DynamoDB table to our application.

Your table can be seen here.

Instructor: [00:00] In order to store data, we need a database. One convenient way of doing so in AWS is DynamoDB. To add a DynamoDB table, we can leverage the resources section in the serverless.yml. It allows us to add raw cloud formation resources.

[00:16] Just for clarification, why we need to nest resources twice here, it has to be nested because cloud formation also supports other keywords, like output and input. Next up, we define a resource name, and inside set the type, AWS DynamoDB table.

[00:36] In the minimum configuration, a table needs to have a couple of properties defined, the table name, the attributes used for primary and secondary indexes. In our case, we have an ID of type string. Then we add a key schema and define the attributes that make up the primary key for our table. In our case, this is the field ID.

[01:00] Keep in mind, in the table we can still store other attributes, like text or createdAt. It's not necessary to define them here. It's only mandatory to define those used for the key schema.

[01:13] Last but not least, we define the throughput capacity for read and write. In our case, one is sufficient. In case you need to scale, I recommend you to activate autoscaling for the DynamoDB table. Then we run SLS deploy, and cloud formation will set up our table.

[01:42] Let's switch over to the console. As we can see, the table exists now.

Reilly Sweetland
Reilly Sweetland
~ 6 years ago

Do you mind clarifying this YAML syntax?

AttributeDefinitions:
  -                                    <---- ?
    AttributeName: id
    AttributeType: S
Nik Graf
Nik Grafinstructor
~ 6 years ago

@Reilly Sweetland sure, it's a list and in this case a list of objects

AttributeDefinitions:
 -
    AttributeName: id
    AttributeType: S
 -
    AttributeName: createdAt
    AttributeType: N # for number

A list of strings would look like this:

- "cat"
- "dog"
Reilly Sweetland
Reilly Sweetland
~ 6 years ago

Ah! Thank you. Fantastic course, btw. Really appreciate you putting it together.

Nik Graf
Nik Grafinstructor
~ 6 years ago

Glad you like it!

Daniel Uhl
Daniel Uhl
~ 6 years ago

Quick tip, if you deploy successfully and can't see the table in AWS make sure you're on the same location it's deploying to. I spent way too long scratching my head on that one.

Actum
Actum
~ 5 years ago

Quick tip, if you deploy successfully and can't see the table in AWS make sure you're on the same location it's deploying to. I spent way too long scratching my head on that one.

Thank you!

Fluffy Pink Unicorns
Fluffy Pink Unicorns
~ 5 years ago

Could you please help on this error:

validation error detected: Value 'Hash' at 'keySchema.1.member.keyType' failed to satisfy constraint: Member must satisfy enum value set: [HASH, RANGE] (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: C5PFA84D3TQ1EPFN15EAUBBH83VV4KQNSO5AEMVJF66Q9ASUAAJG)

Use HASH instead of Hash

Luke Dyson
Luke Dyson
~ 4 years ago

I've followed the guide, updated the YML with the DynamoDB defintion, run the script, which succeeds, but no new DynamoDB instance exists in my AWS account.

What am I doing wrong?

Luke Dyson
Luke Dyson
~ 4 years ago

I've followed the guide, updated the YML with the DynamoDB defintion, run the script, which succeeds, but no new DynamoDB instance exists in my AWS account.

What am I doing wrong?

Seems nothing - although the default region of us-east-1 doesn't seem to support DynamoDB, I changed to my AWS accounts default region, and can now see the Lambda and the DynamoDB instance

~ 4 years ago

Luke, worked for me with us-east-1 (which was set up for me by default, and I'm actually in a different region). The reason I've started reading comments is that it didn't work first. It was just a typo in my case.

Markdown supported.
Become a member to join the discussionEnroll Today