Step 2: Create Lambda and API Gateway (Nodejs) — AWS Serverless to DynamoDB

HK_IT_ER
3 min readAug 26, 2018

--

Create API Gateway

Create API
Setting on API Gateway
Create Method
Create GET Method

The “Lambda function” will fill after creating Lambda.

Create Lambda

Create Function
Setting on Function

Role: Create new role from template(s)

Role name: dynamodb_read

Policy templates: Basic Lambda@Edge permissions (for CloudFront trigger)

Lambda function is created.

IAM Setting

Select New Created Role
Add Inline Policy
Select a Service — DynamoDB
Click Read in Access level
Add ARN in Table Section
DynamoDB Detail
  1. Go back to DynamoDB Service
  2. Select your table and Get the ARN
Fill the ARN in Inline Policy
Review Policy and Create Policy
Inline policy is created.
Designer in Lambda

The DynamoDB will appear on the Designer section in Lambda.

dynamodb_read Coding

We use the Nodejs to connect the dynamoDB.

const AWS = require('aws-sdk');
AWS.config.update({ region: 'us-west-2' });
const dynamodb = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event, context) => {
return dynamodb.scan({ TableName: "dynamodb" })
.promise()
.then(response => response.Items)
};

Region: the location of the dynamoDB, the region code can find in the ARN.

arn:aws:dynamodb:us-west-2:5029XXXXXXXX:table/DynamoDB_Read

TableName: the table name of the dynamoDB — dynamodb

Remember to “Save”

Go Back API Gateway and fill the lambda function name
Message Prompt and Click “OK”
Deploy API
The detail of Deploy API
The API end-point is generated.
Add the API Gateway Trigger
Configure the Trigger

Remember to “Add” and “Save”

Configuration Complete
Type the API End Point and The dynamoDB record shows.

You can give me the response for support if you completed the lab or ask me questions when you face any difficulty.

Check following story to review the lab.

Create DynamoDB — AWS Serverless to DynamoDB

--

--