Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
One of the biggest benefits of AWS Lambda is how easily it integrates with other AWS technologies. In this post, I’ll be giving a quick guide on how to create a DynamoDB table and deploy functions that will allow us to store and fetch records from our table. For this walk-through, we’ll be building a to-do-list API using the serverless framework.
If this is your first time working with the serverless framework, you can quickly follow the setup steps I’ve outlined on another post here. It should only take you a few minutes.
Create New SLSÂ Project
The first step is to initialise a new serverless project in a selected folder. You can do so with the following command:
sls create --template aws-nodejs-typescript --path aws-lambda-with-dynamodb
That will setup our app structure with some boilerplate code including a basic lambda function.
Install Dependencies & Update Manifest File
You can start by running npm i to install any missing dependencies. Additional packages that we’ll be making use of are uuid to generate unique id’s for the records in the table of our db, and the serverless-offline plugin so that we can emulate AWS Lambda and API Gateway locally.
npm i —-save aws-lambda uuidnpm i -D @types/uuid serverless-offline
Once you have installed those dependencies, you can add a script to your package.json file to trigger the start lifecycle event to run a command that starts the serverless-offline emulator on a specific port (4000 in my case).
"scripts": { "start": "sls offline start --port 4000", "test": "echo \"Error: no test specified\" && exit 1"}
Resource Configuration
Inside your serverless app directory, create a folder called resources. Inside this folder we’ll add a YAML file called dynamo-db.yml. In here, we’ll add the AWS::DynamoDB::Table resource which creates a DynamoDB table.
Resources: ToDoListTable: Type: AWS::DynamoDB::Table Properties: TableName: to-do-list AttributeDefinitions: - AttributeName: id AttributeType: S KeySchema: - AttributeName: id KeyType: HASH # Set the capacity ProvisionedThroughput: ReadCapacityUnits: 1 WriteCapacityUnits: 1
What are the properties being defined in this file 🤔 ?
- We set a description for the DynamoDB table resource to be created (ToDoListTable).
- We define the actual name (TableName) of the DynamoDB table as to-do-list.
- We set the AttributeDefinitions for the table which describe the key schema and indexes. Our table has an AttributeName of id and an AttributeType of String (S).
- We define the KeySchema which specifies the attributes that make up the primary key for the table, in this case it will be the id. The attributes in the KeySchema property must also be defined in the AttributeDefinitions property.
- Lastly, we provision the read and write capacity for our to-do-list table with the ProvisionedThroughput property. The ReadCapacityUnits and WriteCapacityUnits define the maximum strongly consistent reads consumed per second and the maximum writes consumed per second before DynamoDB throws a ThrottlingException. You can read more on these properties and the implications of their settings here.
Service Configuration
Let’s turn our attention to the serverless.yml file. I’ll start by setting the region to eu-west-1, but you can go ahead and select a different region that best suits you.
To speed up local development, we are going to configure the serverless-offline plugin we installed earlier that will emulate AWS Lambda and API Gateway as follows:
plugins: - serverless-webpack - serverless-offline
Next up, let’s reference the DynamoDB resource that we just added like this:
resources: # DynamoDB - ${file(resources/dynamodb-table.yml)}
We want to give our lambda functions permission to interact with our DynamoDB table. These permissions are set via an AWS IAM Role and we can set the permission policy statements within this role via the provider.iamRoleStatements property. It is important to note that with this configuration, every function we deploy in this service will share these permissions.
iamRoleStatements: - Effect: Allow Action: - dynamodb:DescribeTable - dynamodb:Query - dynamodb:Scan - dynamodb:GetItem - dynamodb:PutItem
For security purposes, we want to limit access permission of our lambda service to the to-do-list table resource, and so we can define that in the Resource property by specifying the resource using its ARN (Amazon Resource Name) like so:
Re
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.