Skip to content

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

Notifications You must be signed in to change notification settings

aashari/boilerplate-lambda-typescript

Repository files navigation

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

Name Version
terraform ~> 1.1.9
aws ~> 4.10.0

Providers

Name Version
archive 2.2.0
aws 4.10.0
null 3.1.1

Modules

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

Resources

Name Type
aws_cloudwatch_event_rule.lambda-function-trigger-schedule resource
aws_cloudwatch_event_target.lambda-function-trigger-schedule resource
aws_cloudwatch_log_group.lambda-function-log-group resource
aws_dynamodb_table.dynamodb-table resource
aws_iam_role.lambda-function-role resource
aws_iam_role_policy.function-policy resource
aws_kms_alias.service-alias resource
aws_kms_key.service-key resource
aws_lambda_function.lambda-function resource
aws_lambda_layer_version.lambda-layer resource
aws_lambda_permission.lambda-function-trigger-schedule resource
aws_ssm_parameter.ssm-parameter-custom resource
aws_ssm_parameter.ssm-parameter-dynamodb-table resource
aws_ssm_parameter.ssm-parameter-service-version resource
null_resource.lambda-function-source-builder resource
null_resource.lambda-layer-source-builder resource
null_resource.typescript-source-model-builder resource
archive_file.lambda-function-source data source
archive_file.lambda-layer-source data source
archive_file.typescript-source data source
aws_caller_identity.current data source
aws_iam_policy_document.function-policy data source
aws_region.current data source

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

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

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

About

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

Topics

Resources

Stars

Watchers

Forks