Boilerplate Lambda Typescript
Overview
This is a boilerplate to help you initiate AWS Lambda project using Typescript, in this boilerplate there are terraform code to provision the stacks and the initial Typescript source code in the sources directory
Features
- Terraform code to provision the AWS Lambda project
- Typescript source code in the
sources directory
- Automatically load AWS Secrets Manager (parameter store) as environment variables
- Automatically load DynamoDB (table name) as environment variables
- Automatically create models for DynamoDB tables with the ability to read, write, delete, and scan
- Decorator example to log the execution time of the method
- Datadog example integration to stream the metrics of statistic decorator to Datadog
- Lambda layer to store the dependencies of the project
- Lambda scheduler to schedule the function invocation
Project Structures
.
βββ sources
β βββ package-lock.json
β βββ package.json
β βββ src
β β βββ decorators
β β β βββ statistic.decorator.ts
β β βββ functions
β β β βββ booking-create.function.ts
β β β βββ booking-search.function.ts
β β β βββ flight-search.function.ts
β β βββ helpers
β β β βββ chunk.helper.ts
β β β βββ parameter-store.helper.ts
β β βββ index.ts
β β βββ libraries
β β β βββ datadog.library.ts
β β β βββ dynamodb.library.ts
β β βββ models
β β βββ booking.model.ts
β β βββ flight.model.ts
β β βββ model.ts
β β βββ transaction.model.ts
β βββ tsconfig.json
βββ README.md
βββ data.tf
βββ main.tf
βββ providers.tf
βββ terraform.tfvars.example
βββ variables.tf
While doing a terraform apply command, theese are the things that will be created:
- AWS Lambda Function, in the
main.tf there's a logic on creating AWS Lambda function based on files with format *.function.ts under sources/src/functions directory, so the number of AWS Lambda function created is based on *.function.ts files
- AWS System Manager Parameter Store, in the
main.tf there's a logic on creating AWS Parameter Store with prefix set on parameter_store_path under variables.tf based on:
- parameter_store_list attributes under
variables.tf file
- dynamodb_table_list attributes under
variables.tf file which will create a Parameter Store to store the table names of DynamoDB with format dynamodb-table-{table_name}
- service_version attributes under
variables.tf file which will create a Parameter Store to store the version of the service
Another context related to the Typescript source code:
- sources/src/helpers is the collection of functional helpers such as
populateEnvironmentVariables() you can freely add another functional helpers under this directory
- sources/src/libraries is the collection of class helpers such as
DatadogLibrary which contains all Datadog functionality such as publishMetrics and publishEvents, or another example DynamoDBLibrary which contains putItem and getItem
- sources/src/decorators is the collection of Typescript decorators, the initial example is
@statistic decorator which have the functionality to log the execution duration for the method that uses the decorators, the example also include the additional process to stream the statistic metrics into Datadog
- sources/src/index.ts is a bootstraper file which contains default
exports.handlers function, which is the default function that will be called by AWS Lambda Function, this file contains logic to create the sources/src/functions/*.function.ts instance and create object then call the handler method
- sources/src/models is the collection of Typescript models, the initial example is
Booking which is the model for DynamoDB table booking which automatically created by terraform code
Requirements
Providers
Modules
Resources
Inputs
| Name |
Description |
Type |
Default |
Required |
| default_tags |
The default tags for the service |
map(string) |
{} |
no |
| dynamodb_table_list |
The list of dynamodb tables to be used for the service, e.g. [ { "name": "booking", "key": "id" }, { "name": "flight", "key": "id" }, { "name": "transaction", "key": "booking_id", "range_key": "flight_id" } ] |
list(object({ name = string, key = string, range_key = optional(string), })) |
[] |
no |
| lambda_function_configuration |
The custom configuration for the Lambda Function, e.g. { "booking-create": { "lambda_memory_size": 1024, "lambda_timeout": 300 } } |
map(object({ lambda_memory_size = optional(number), lambda_timeout = optional(number), schedule_expression = optional(string), })) |
{} |
no |
| parameter_store_list |
The list of parameter store keys to be used for the service, e.g. [ "datadog-api-key", "datadog-app-key", "sentry-dsn", "sentry-environment" ] |
list(string) |
[] |
no |
| service_domain |
The 1st level of logical grouping of the service, e.g. 'api', 'web', 'db', etc. |
string |
n/a |
yes |
| service_environment |
The 3rd level of logical grouping of the service, e.g. 'dev', 'test', 'prod', etc. |
string |
n/a |
yes |
| service_name |
The 2nd level of logical grouping of the service, e.g. 'my-api', 'my-web', 'my-db', etc. |
string |
n/a |
yes |
| service_version |
The version of the service |
string |
"v1.0.0" |
no |
Outputs
How to Setup
To setup the example you can follow the following steps:
- Copy the
terraform.tfvars.example file to terraform.tfvars
- Run
terraform init
- Run
terraform apply