Giter Club home page Giter Club logo

lambda-terraform's Introduction

Example lambda function with terraform

Requirements

  • Terraform >=v0.12.24
  • provider.aws >=v2.57.0

Preparing your lambda function

# Clone the repo
git clone https://github.com/netoht/lambda-terraform.git
cd lambda-terraform/

Implement your lambda function in lambda.py.

# Packaging your lambda function
./lambda-package.sh

This action will create a zip inside ./tf/target/lambda.zip

About Terraform

The Terraform needs to save states about resources, then we need tell to him about that. The backend.tf file ask to save state in s3 and provider.tf inform about cloud provider, in this case AWS.

The backend-config/dev configure the variables in which the environment it will usage to save states.

# AWS Region of the bucket
region="sa-east-1"
# The bucket name to save state
bucket="myproject-dev"
# The project key of the terraform state
key="tfstate/myproject-dev"

For each environment you need create a variables-{ENV}.tfvar like this variables-dev.tfvar:

env="dev"
aws_region="sa-east-1"
lambda_name="minimal_lambda_function"
lambda_handler="lambda.handler"
lambda_runtime="python3.6"
lambda_filename="target/lambda.zip"
tags={"Project": "test"}

Here was informed the environment and information about lambda function that should be used to create the resources and it will used inside lambda.tf. In the lambda.tf has the resources that will be created, the aws_lambda_function and the aws_iam_role.

Executing Terraform

Terraform needs to initialize (terraform init) the state and apply (terraform apply) the changes. The run-terraform.sh script was created to help us.

$ cd tf/
# ./run-terraform.sh -e <Environment>
./run-terraform.sh -e dev

...
Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

Validating the new resources created

Execute the command below to test the lambda function:

aws lambda invoke --invocation-type RequestResponse --function-name minimal_lambda_function_dev --region sa-east-1 --log-type Tail --payload '{"key1":"value1", "key2":"value2", "key3":"value3"}' target/outputfile.txt | jq .LogResult | sed 's/"//g' | base64 --decode

You should see something like this:

START RequestId: e02625f4-c922-4be1-be9f-15d04897a3ac Version: $LATEST
Received my event: {
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}
END RequestId: e02625f4-c922-4be1-be9f-15d04897a3ac
REPORT RequestId: e02625f4-c922-4be1-be9f-15d04897a3ac  Duration: 0.41 ms       Billed Duration: 100 ms Memory Size: 128 MB     Max Memory Used: 42 MB

References

lambda-terraform's People

Stargazers

Marcelo Rosa avatar

Watchers

Waldinar Oliveira Neto avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.