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.
- Create an AWS account if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
- AWS CLI installed and configured
- Git Installed
- AWS Serverless Application Model (AWS SAM) installed
- Go Installed v1.22.0
-
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
-
Change directory to the pattern directory:
cd apigw-lambda-dynamodb-go-sam
-
From the command line, usa AWS SAM to build the artifacts before deploying to AWS:
sam build
-
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
-
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 usesam 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.
-
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.
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:
-
Amazon API Gateway: Receives the HTTP POST request containing the prompt.
-
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.
-
Response: Lambda processes the request and returns a success or error as a response to the API Gateway.
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>
- 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