YouTube Tutorial by Cloud Path Here
A step-by-step tutorial on how to write AWS Lambda functions in NodeJS to get and set data in DynamoDB. We will create a Users table with two lambda functions: GetUserData and PutUserData.
- Create a table in DynamoDB:
Users
- Primary Key can be id, a string
- Add 1 item to the table
- Create an IAM role to give our lambda permissions to work with the table
- You will need the ARN from the table's overview
- Create Role
- AWS service
- Lambda
- Permissions:
AWSLambdaBasicExecutionRole
- Keys: optional. But useful if you have lots of roles and want to organize/search
- Add Inline Policy to Role after creation
- Service: DynamoDB
- Actions: GetItem, PutItem
- Resources: specific, paste in ARN
- name policy
- Create Lambda Function
- Use the exisiting role just created.
- The Designer Tab allows you to specify triggers (We will set up API Gateway later)
- Not recommended to edit code directly in the interface. Use VSC.
AWS Lambda Boilerplate Snippets
is a recommended extensionlambda-aws
will scaffold a starting function
aws-sdk
is imported- You can view the DynamoDB API to better understand how to retrieve items from it. Search for
GetItem
to learn how to model thegetUserData
function - After function has been written. Copy and paste to Lambda interface.
- Test Lambda Function
- Configure Test Event, deploy, test
- We're using API Gateway to create a simple REST API to interact with our lambda functions to get/set user data.
- API Gateway will call our lambda functions, which will in turn get/set data in the database.
- Select build REST API
- Edge optimized
- Actions > Create Resource
- Actions > Create Method
-
GET
- Checking the
Use Lambda Proxy Integration
box will allow access to request parameters/body - Choose Lambda Function:
getUserData
- Clicking
Method Request
will allow extra options- Authorization:
AWS_IAM
allows you to create authorization on who can access API Request Validator
:Validate body, query string parameters, and headers
- Authorization:
- Add
content-type
header
- Checking the
-
test endpoint with query strings and header:
content-type: application/json
-
POST
- Let's create a new model
- Link to API Gateway models docs
- After creating model, add to request body in POST settings
-