Skip to content

Latest commit

 

History

History
 
 

apigw-lambda-dynamodb-go-sam

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

API Gateway REST API to AWS Lambda to Amazon DynamoDB

This pattern explains how to deploy an AWS SAM application that includes an Amazon API Gateway, AWS Lambda Function, and writes items to an Amazon DynamoDB table.

Requirements

Deployment Instructions

  1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:

    git clone https://github.com/aws-samples/serverless-patterns
  2. Change directory to the pattern directory:

    cd apigw-lambda-dynamodb-go-sam
  3. From the command line, usa AWS SAM to build the artifacts before deploying to AWS:

    sam build
  4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:

    sam deploy --guided
  5. During the prompts:

    • Enter a stack name
    • Enter the desired AWS Region
    • Allow SAM CLI to create IAM roles with the required permissions.

    Once you have run sam deploy --guided mode once and saved arguments to a configuration file (samconfig.toml), you can use sam deploy in future to use these defaults.

    When asked "PutItemsFunction has no authentication. Is this okay? [y/N]", answer explicitly with y for the purposes of this sample application. As a result, anyone will be able to call this example REST API without any form of authentication.

    For production applications, you should enable authentication for the API Gateway using one of several available options and follow the API Gateway security best practices.

  6. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for next step as well as testing.

How it works

This SAM project uses Amazon DynamoDB to store user data in a single table. This is exposed through a serverless REST API.

Here's a breakdown of the steps:

  1. Amazon API Gateway: Receives the HTTP POST request containing the prompt.

  2. AWS Lambda: Triggered by the API Gateway, this function grabs the request body and uses the User data to create a new item in DynamoDB.

  3. Response: Lambda processes the request and returns a success or error as a response to the API Gateway.

Testing

Test the deployed content generation API by providing a prompt. You can use curl to send a HTTP POST request to the API. Make sure to replace API_Endpoint with the one from your sam deploy --guided output:

curl -d '{"id": "12345", "first_name":"John", "last_name":"Doe"}' -H 'Content-Type: application/json'  <API_Endpoint>

Cleanup

  1. To delete the resources deployed to your AWS account via AWS SAM, run the following command:
sam delete

Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0