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
- Terraform code to provision the AWS Lambda project
- Typescript source code in the
sourcesdirectory - 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
.
├── 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.tfthere's a logic on creating AWS Lambda function based on files with format*.function.tsundersources/src/functionsdirectory, so the number of AWS Lambda function created is based on*.function.tsfiles - AWS System Manager Parameter Store, in the
main.tfthere's a logic on creating AWS Parameter Store with prefix set onparameter_store_pathundervariables.tfbased on:- parameter_store_list attributes under
variables.tffile - dynamodb_table_list attributes under
variables.tffile which will create a Parameter Store to store the table names of DynamoDB with formatdynamodb-table-{table_name} - service_version attributes under
variables.tffile which will create a Parameter Store to store the version of the service
- parameter_store_list attributes under
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
DatadogLibrarywhich contains all Datadog functionality such aspublishMetricsandpublishEvents, or another exampleDynamoDBLibrarywhich containsputItemandgetItem - sources/src/decorators is the collection of Typescript decorators, the initial example is
@statisticdecorator 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.handlersfunction, which is the default function that will be called by AWS Lambda Function, this file contains logic to create thesources/src/functions/*.function.tsinstance and create object then call thehandlermethod - sources/src/models is the collection of Typescript models, the initial example is
Bookingwhich is the model for DynamoDB tablebookingwhich automatically created by terraform code
| Name | Version |
|---|---|
| terraform | ~> 1.1.9 |
| aws | ~> 4.10.0 |
| Name | Version |
|---|---|
| archive | 2.2.0 |
| aws | 4.10.0 |
| null | 3.1.1 |
| Name | Source | Version |
|---|---|---|
| dynamodb-table-name | git@github.com:traveloka/terraform-aws-resource-naming.git | v0.22.0 |
| lambda-function-name | git@github.com:traveloka/terraform-aws-resource-naming.git | v0.22.0 |
| lambda-layer-name | git@github.com:traveloka/terraform-aws-resource-naming.git | v0.22.0 |
| 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.[ |
list(object({ |
[] |
no |
| lambda_function_configuration | The custom configuration for the Lambda Function, e.g.{ |
map(object({ |
{} |
no |
| parameter_store_list | The list of parameter store keys to be used for the service, e.g. [ |
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 |
| Name | Description |
|---|---|
| dynamodb-table-list | List of DynamoDB Tables created |
| kms-alias | KMS Alias created |
| kms-key | KMS Key created |
| lambda-function-list | List of Lambda Functions created |
| lambda-function-role | Lambda Function Role created |
| lambda-layer | Lambda Layer created |
| ssm-parameter-list | List of SSM Parameters created |
To setup the example you can follow the following steps:
- Copy the
terraform.tfvars.examplefile toterraform.tfvars - Run
terraform init - Run
terraform apply