Skip to content

Repository files navigation

Terraform AWS Lambda Function

This is an opinionated setup that uses Terraform to manage an AWS Lambda function.

What will this setup do?

This is a Terraform configuration that manages the an AWS EKS stack. It will create the following resources:

  • AWS Lambda Function: Lambda function using a Docker image to run code
  • AWS S3 Bucket: Used to store the attachments for the Lambda Function
  • AWS Policy: Allows the Lambda Function to access the S3 Bucket
  • AWS ECR Repository: Used to store the Docker image for the Lambda Function
  • Docker Image Build and Push to AWS ECR repository

This setup the Terraform CLI to manage the IllumiDesk stack using Terraform Workspaces.

Requirements

Ensure you have the following installed on your local machine:

You must have an AWS account and provide your AWS Access Key ID and AWS Secret Access Key.

The values for AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY should be saved as environment variables on your workspace or they can be provided as variables in the *.tfvars file.

Quick Start

Copy the example.tfvars file to <environment>.tfvars and update the values with your AWS credentials.

cp example.tfvars dev.tfvars

Create and/or select a Terraform workspace.

terraform workspace new dev

Initialize the Terraform configuration.

terraform init

Plan the Terraform configuration (replace <environment> with the name of your environment):

terraform plan -var-file=<environment>.tfvars

For the development environment:

terraform plan -var-file=dev.tfvars

For the production environment:

terraform plan -var-file=prod.tfvars

Apply the Terraform configuration.

terraform apply -var-file=dev.tfvars

Assert the Lambda Function with Boto3

From the tests/lambda_function directory, run the following command to test the Lambda function with Boto3. Ensure that the ARN value is exported with the LAMBDA_FUNCTION_ARN environment variable before executing the test:

# example for development environment lambda functionexport LAMBDA_FUNCTION_ARN=arn:aws:lambda:us-east-1:860100747351:function:app-dev-docker-lambda_handler
python test_lambda_with_arn.py

This should output a result similar to the following:

{'statusCode': 200, 'body': '{"result": {"a": 5, "b": 20}}'}

Terraform Reference

Requirements

NameVersion
terraform>= 0.13.1
aws>= 3.19
docker>= 2.12
random>= 2.0

Providers

NameVersion
aws>= 3.19
random>= 2.0

Modules

NameSourceVersion
docker_image./docker-buildn/a
lambda_function_from_container_imageterraform-aws-modules/lambda/aws~> 4.13

Resources

NameType
random_pet.thisresource
aws_caller_identity.thisdata source
aws_ecr_authorization_token.tokendata source
aws_region.currentdata source

Inputs

No inputs.

Outputs

NameDescription
docker_image_uriThe ECR Docker image URI used to deploy Lambda Function
lambda_cloudwatch_log_group_arnThe ARN of the Cloudwatch Log Group
lambda_function_arnThe ARN of the Lambda Function
lambda_function_arn_staticThe static ARN of the Lambda Function. Use this to avoid cycle errors between resources (e.g., Step Functions)
lambda_function_invoke_arnThe Invoke ARN of the Lambda Function
lambda_function_kms_key_arnThe ARN for the KMS encryption key of Lambda Function
lambda_function_last_modifiedThe date Lambda Function resource was last modified
lambda_function_nameThe name of the Lambda Function
lambda_function_qualified_arnThe ARN identifying your Lambda Function Version
lambda_function_regionThe region of the Lambda Function
lambda_function_source_code_hashBase64-encoded representation of raw SHA-256 sum of the zip file
lambda_function_source_code_sizeThe size in bytes of the function .zip file
lambda_function_versionLatest published version of Lambda Function
lambda_layer_arnThe ARN of the Lambda Layer with version
lambda_layer_created_dateThe date Lambda Layer resource was created
lambda_layer_layer_arnThe ARN of the Lambda Layer without version
lambda_layer_source_code_sizeThe size in bytes of the Lambda Layer .zip file
lambda_layer_versionThe Lambda Layer version
lambda_role_arnThe ARN of the IAM role created for the Lambda Function
lambda_role_nameThe name of the IAM role created for the Lambda Function

About

Lambda function to run arbitrary Python code.

Resources

Contributing

Stars

0 stars

Watchers

7 watching

Forks

Releases

Packages

Contributors

Languages