Skip to content

Latest commit

 

History

History
90 lines (59 loc) · 2.71 KB

.header.md

File metadata and controls

90 lines (59 loc) · 2.71 KB

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

  1. Copy the example.tfvars file to <environment>.tfvars and update the values with your AWS credentials.
cp example.tfvars dev.tfvars
  1. Create and/or select a Terraform workspace.
terraform workspace new dev
  1. Initialize the Terraform configuration.
terraform init
  1. Plan the Terraform configuration (replace <environment> with the name of your environment):
terraform plan -var-file=<environment>.tfvars
  1. For the development environment:
terraform plan -var-file=dev.tfvars
  1. For the production environment:
terraform plan -var-file=prod.tfvars
  1. 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 function
export 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