This section explains how to deploy a lambda function with library dependencies (on Node JS). Most of the parts will be similar to previous section. The change will be in the way the source is bundled. We will notice how terraform modules can be reused here.
Samples for this tutorial are available in samples/06 folder.
Following example will use a node.js library accounting.js to format a number into currency.
formatCurrencyLambda.js contains the lambda source.
From the samples/06 folder execute the following command to install accounting.js
npm install accountingHere, we will bundle the source file with its node_module dependencies. The compressed formatCurrencyLambda.zip
is the artifact will be deployed using Terraform.
Note: node_modules folder is included in the compressed formatCurrencyLambda.zip.
zip -r /tmp/formatCurrencyLambda.zip node_modules formatCurrencyLambda.jsTerraform script for deploying formatCurrencyLambda in AWS is in main.tf. This script (below) reuses the
modules lambda and lambda-role.
- Only the locals values are changed to reflect the lambda name and zip file name. Otherwise, this script
is very similar to the helloWorldLambda's
main.tf - Only the module reference has changed in
output.tf. Otherwise,vars.tfandoutput.tfhaven't changed as well.
provider "aws" {
region = var.aws_region
}
module "lambda_tf_way_role" {
source = "../modules/lambda-role"
}
locals {
lambda_name = "formatCurrencyLambda"
zip_file_name = "/tmp/formatCurrencyLambda.zip"
handler_name = "formatCurrencyLambda.handler"
}
module "format_currency_lambda" {
source = "../modules/lambda"
lambda_function_name = local.lambda_name
lambda_function_handler = local.handler_name
lambda_role_arn = module.lambda_tf_way_role.lambda_role_arn
lambda_zip_filename = local.zip_file_name
}Now we will run terraform script to create the Format Currency Lambda (with its IAM Role).
You need to be in the samples/06 folder to run the script.
Note: The
AWS_PROFILEis configured aslambda-tf-user
export AWS_PROFILE=lambda-tf-user
terraform init
terraform apply --auto-approveWe will invoke the formatCurrencyLambda via AWS CLI to test the deployment.
Note: If you are on AWS CLI v2, you have include the param
--cli-binary-format raw-in-base64-out.
aws lambda invoke --function-name formatCurrencyLambda \
--log-type Tail \
--cli-binary-format raw-in-base64-out \
--payload '{"value": 123456789}' \
--profile "$AWS_PROFILE" \
outputfile.txtPrint the contents outputfile.txt to see the formatted value of 123456789 as $123,456,789.00.
cat outputfile.txt
> Output :
{"amount":"$123,456,789.00"}%Now, we will delete the formatCurrencyLambda using terraform destroy, similar to helloWorldLambda.
From the samples/06/ folder run the following command.
export AWS_PROFILE=lambda-tf-user
terraform destroy --auto-approveNote: Once destroy completes, you should see a log similar to the one below.
module.lambda_tf_way_role.aws_iam_role_policy.lambda_tf_way_role_policy: Destroying... [id=tf_way_lambda_role:tf_way_lambda_role_policy]
module.format_currency_lambda.aws_lambda_function.lambda_tf_way_function: Destroying... [id=formatCurrencyLambda]
module.format_currency_lambda.aws_lambda_function.lambda_tf_way_function: Destruction complete after 0s
module.lambda_tf_way_role.aws_iam_role_policy.lambda_tf_way_role_policy: Destruction complete after 1s
module.lambda_tf_way_role.aws_iam_role.lambda_tf_way_role: Destroying... [id=tf_way_lambda_role]
module.lambda_tf_way_role.aws_iam_role.lambda_tf_way_role: Destruction complete after 3s
Destroy complete! Resources: 3 destroyed.
🏁 Congrats ! You deployed your 'first Lambda with dependencies' using Terraform and invoked it successfully. 🏁
Next: Lambda Layers