ReBarrington / AWS-Tutorial

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AWS Lambda & DynamoDB Tutorial

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.

Getting Started

  • 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 extension
      • lambda-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 the getUserData function
    • After function has been written. Copy and paste to Lambda interface.
  • Test Lambda Function
    • Configure Test Event, deploy, test

Amazon API Gateway

  • 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
      • Add content-type header
    • test endpoint with query strings and header: content-type: application/json

    • POST

About


Languages

Language:JavaScript 74.4%Language:HTML 21.0%Language:CSS 4.6%