• This repository has been archived on 06/Apr/2021
  • Stars
    star
    119
  • Rank 297,930 (Top 6 %)
  • Language HCL
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ”₯ An example of a Python (AWS) Lambda exposed with API Gateway, configured with Terraform.

Hello (AWS) Lambda with Terraform

CircleCI

This project is an example of a Python (AWS) Lambda exposed with API Gateway, configured with Terraform. This demo project is related to the following blog post: A Tour of AWS Lambda.

Introduction

This demo project creates a /hello endpoint with two methods (GET and POST). Both methods are bound to a single file containing two handlers (a.k.a. lambda functions, one for each method). This is defined by a handler parameter. The code for each lambda function is written in Python (method names are just a convention):

def handler(event, context):
    return { "message": "Hello, World!" }

def post_handler(event, context):
    return { "message": "I should have created something..." }

The Terraform configuration relies on two modules: lambda and api_method. See the Terraform Modules section for further information. This configuration creates two lambda functions on AWS Lambda, a (deployed) REST API with a single endpoint and two HTTP methods on API Gateway, and takes care of the permissions and credentials. The figure below is an example of what you get in the API Gateway dashboard:

Getting started

You must have an AWS account. Next, you must install Terraform first.

Clone this repository, then run:

$ make bootstrap

Create a terraform.tfvars file with the content below. This step is optional as Terraform will ask you to fill in the different values, but it is convenient.

aws_account_id = "account-id"
aws_access_key = "access-key"
aws_secret_key = "secret-key"
aws_region     = "eu-west-1"

You are now ready to use Terraform!

$ terraform plan

If everything is OK, you can build the whole infrastructure:

$ terraform apply

You can destroy all the components by running:

$ terraform destroy

For more information, please read the Terraform documentation.

Terraform Modules

lambda

module "lambda" {
  source  = "github.com/TailorDev/hello-lambda/lambda"
  name    = "my-lambda"
  handler = "handler"
  runtime = "python2.7" #Β could be nodejs | nodejs4.3 | java8 | python2.7
  role    = "my-role"
}

Important: this module assumes that the source file, the lambda (in AWS), and the zip file have the same name. For example, we use hello_lambda in this project. The handler parameter distinguishes the different lambda functions that can be invoked.

api_method

module "hello_post" {
  source      = "github.com/TailorDev/hello-lambda/api_method"
  rest_api_id = "rest-api-id"
  resource_id = "resource-id"
  method      = "POST"
  path        = "resource-path"
  lambda      = "my-lambda"
  region      = "eu-west-1"
  account_id  = "account-id"
}

License

This project and its Terraform modules are released under the MIT License. See the bundled LICENSE file for details.