hafizali05 / apigw-lambda-dynamodb

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

typescript: 4.5.5 AWS: DynamoDB test: unit test: integration

Typescript: Amazon Api Gateway, AWS Lambda, Amazon DynamoDB Example

Introduction

This project contains introductory examples of TypeScript tests written for AWS Lambda. This is a great place to start!

The project uses the AWS Serverless Application Model (SAM) CLI for configuration, testing and deployment.


Contents


About this Pattern

System Under Test (SUT)

The SUT in this pattern is a synchronous API composed of Amazon API Gateway, AWS Lambda and a persistence layer in Amazon DynamoDB. Amazon API Gateway may contain an authorization feature like Cognito or a Lambda custom authorizer.

System Under Test (SUT)

Goal

The goal of this pattern is to test the SUT in environment as similar to production as possible by running tests against resources deployed to the cloud.

Description

The SUT will be deployed to the AWS cloud. The test code will create an HTTP client that will send requests to the deployed API Gateway endpoint. The endpoint will invoke the backing services, test resource configuration, IAM permissions, authorizers, and internal business logic of the SUT.

System Under Test Description (SUT)

About this Example

This specific sample project allows a user to call an API endpoint generate a custom "hello" message, and also tracks the messages it generates. A user provides an "id", which the endpoint uses to look up the person's name associated with that id, and generates a message. The message is recorded in DynamoDB and also returned to the caller:

Event Sequence

This project consists of an API Gateway, a single AWS Lambda function, and a Amazon DynamoDB table.

The DynamoDB Table uses a single-table design, as both the name lookup and the message tracking use the same table. The table schema is defined as follows:

  • For all records, the "Partition Key" is the id.
  • For name records, the "Sort Key" is set to a constant = "NAME#"
  • For message history records, the "Sort Key" is set to "TS#" appended with the current date-time stamp.
  • The payloads are in a field named "data".

Key Files in the Project

Run the Unit Tests

test-handler.test.ts

In the unit test, all references and calls to the DynamoDB service are mocked using aws-sdk-client-mock client. To run the unit tests

apigw-lambda-dynamodb$ cd src
src $ npm install
src $ npm run test:unit

Top


Run the Integration Tests

integ-handler.test.ts

For integration tests, deploy the full stack before testing:

apigw-lambda-dynamodb$ sam build
apigw-lambda-dynamodb$ sam deploy --guided

The integration tests need to be provided 2 environment variables.

  1. The DYNAMODB_TABLE_NAME is the name of the DynamoDB table providing persistence.
  2. The API_URL is the base URL of the API Gateway deployment stage, which should end with /Prod/ in this case.

Set up the environment variables, replacing the <PLACEHOLDERS> with your values:

src $ export DYNAMODB_TABLE_NAME=<YOUR_DYNAMODB_TABLE_NAME>
src $ export API_URL=<YOUR_APIGATEWAY_BASE_URL>

Then run the test suite.

apigw-lambda-dynamodb$ cd src
src $ npm install
src $ npm run test:integration

Alternatively, you can set the environment variables and run the test suite all in one command:

apigw-lambda-dynamodb$ cd src
src $ npm install
src $ DYNAMODB_TABLE_NAME=<YOUR_DYNAMODB_TABLE_NAME> API_URL=<YOUR_APIGATEWAY_BASE_URL> npm run test:integration

Top

About


Languages

Language:TypeScript 89.7%Language:JavaScript 6.9%Language:Makefile 3.4%