Chapter 5: Managing Data Persistence with DynamoDB
- Implement an update handler to update an existing movie item.
Answer: The handler expects a movie item in a JSON format; the input will be encoded to a Movie
struct. The PutItem
method is used to insert the movie to the table as follows:
func update(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { var movie Movie err := json.Unmarshal([]byte(request.Body), &movie) if err != nil { return events.APIGatewayProxyResponse{ StatusCode: 400, Body: "Invalid payload", }, nil } ... svc := dynamodb.New(cfg) req := svc.PutItemRequest(&dynamodb.PutItemInput{ TableName: aws.String(os.Getenv("TABLE_NAME")), Item: map[string]dynamodb.AttributeValue{ "ID": dynamodb.AttributeValue{ S: aws.String(movie.ID), }, "Name": dynamodb.AttributeValue{ S: aws.String(movie.Name), }, }, }) _, err = req.Send() if err != nil { return events.APIGatewayProxyResponse{ StatusCode: http.StatusInternalServerError, Body: "Error while updating the movie", }, nil } response, err := json.Marshal(movie) ... return events.APIGatewayProxyResponse{ StatusCode: 200, Body: string(response), Headers: map[string]string{ "Content-Type": "application/json", }, }, nil }
- Create a new PUT method in API Gateway to trigger the update Lambda function.
Answer: Expose a PUT
method on the /movies
resource and configure the target to be the Lambda function defined earlier. The following screenshot illustrates the results:

- Implement a single Lambda function to handle all type of events (GET, POST, DELETE, PUT).Answer:
func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { switch request.HTTPMethod { case http.MethodGet: // get all movies handler break case http.MethodPost: // insert movie handler break case http.MethodDelete: // delete movie handler break case http.MethodPut: // update movie handler break default: return events.APIGatewayProxyResponse{ StatusCode: http.StatusMethodNotAllowed, Body: "Unsupported HTTP method", }, nil } }
- Update the
findOne
handler to return a proper response code for a valid request but an empty data (for example, no movie for the ID requested).
Answer: When handling input of a user (movie ID in our case), validation is mandatory. Hence, you need to write a regular expression to ensure the ID given in parameter is properly formed. The following are examples of regular expressions to validate an ID:
- Pattern for alphanumeric ID:
[a-zA-Z0-9]+
- Pattern for digits only ID:
[0-9]+
- Pattern for alphanumeric ID:
- Implement a pagination system on the
findAll
endpoint using a Range header and using a Query string.
Answer: Use the Limit option in the ScanRequest
method to limit number of returned items:
dynamodbClient := dynamodb.New(cfg) req := dynamodbClient.ScanRequest(&dynamodb.ScanInput{ TableName: aws.String(os.Getenv("TABLE_NAME")), Limit: aws.Int64(int64(size)), })
The number of items to return can be read from the request headers:
size, err := strconv.Atoi(request.Headers["Size"])