Serverless Stack Update: Using AWS CDK

Hey folks,

Many of our readers have said defining resources in yaml is both cumbersome and error prone. We’ve now updated Serverless Stack to use AWS CDK instead of CloudFormation YAML. This is a big change! And it makes configuring your Serverless infrastructure a lot easier.

So defining a DynamoDB table looks like this:

- Resources:
-   NotesTable:
-     Type: AWS::DynamoDB::Table
-     Properties:
-       TableName: ${self:custom.tableName}
-       AttributeDefinitions:
-         - AttributeName: userId
-           AttributeType: S
-         - AttributeName: noteId
-           AttributeType: S
-       KeySchema:
-         - AttributeName: userId
-           KeyType: HASH
-         - AttributeName: noteId
-           KeyType: RANGE
-       # Set the capacity to auto-scale
-       BillingMode: PAY_PER_REQUEST

+ const table = new dynamodb.Table(this, "notes", {
+   partitionKey: { name: 'userId', type: dynamodb.AttributeType.STRING },
+   sortKey: { name: 'noteId', type: dynamodb.AttributeType.STRING },
+   billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
+ });

If you have followed the guide before, all the chapters in the Deploying The Backend to Production section have now been updated.

We also added a chapter to help you get started with AWS CDK. So have a look and let us know what you think!

5 Likes

What would happen to current serverless.yml? Would it still work?

3 Likes

Yeah, you are still Serverless Framework for Lambda and API Gateway. Only the resources (ie. DynamoDB table, S3 bucket) are provisioned using CDK.

Thanks for the clarification!