Git Product home page Git Product logo

terraform-aws-api-gateway-lambda-sqs's Introduction

Terraform AWS API Gateway Lambda SQS

Terraform module for AWS API Gateway Lambda SQS infrastructure

License: MIT stability-stable Commitizen-friendly

Table of Contents

Features

Terraform module which deploys a serverless HTTP endpoint backed by AWS API Gateway, Lambda & SQS

Attention

Starting from version 1.6.0, this module targets Terraform 0.12+. If you are using Terraform <=v0.11 you must use up to version 1.5.0.

API Gateway

This module is created with a single stage that is given as parameter. The default path that is created is /api/messages. This can be expanded upon as the API GW ID, resources and methods are exposed. If you do not wish to have the default values, you can specify api_gw_disable_resource_creation = true and you can create the paths desired. Allows specification of Endpoint Configuration Type via variable api_gw_endpoint_configuration_type with EDGE, REGIONAL or PRIVATE. Defaults to EDGE

Note This results in having to create the final aws_api_gateway_deployment as well.

Lambda

This module is created with full customization by user.

  • Can use either local filename path lambda_file_name or remote S3 bucket configuration.
  • Supports Lambda Layers
  • Supports VPC

Must use either the local filename or S3 option as they are mutually exclusive. Exports S3 bucket to allow usage by multiple Lambda's but given lambda_code_s3_bucket_use_existing=true it will use existing S3 bucket provided in lambda_code_s3_bucket_existing.

  • This module by default, if created allows accompanying Lambda access to SQS if SQS entry is provided as parameters.
  • The Lambda gets by default in addition to user provided environment variables the SQS_QUEUES_URLS which is a list with the URL for each of the created queues for convenience.

SQS

This module is optional. Lambda is created with W permission for SQS to allow Lambda to add/read/delete messages from queue.

  • This module by default, if created has no permissions added.

Usage

module "api-gateway-lambda-sqs" {
  source  = "crisboarna/api-gateway-lambda-sqs/aws"
  version = "1.6.0"

  # insert the required variables here
}

Deployment

  1. Run build process to generate Lambda ZIP file locally to match lambda_zip_path variable path
  2. Provide all needed variables from variables.tf file or copy paste and change example below
  3. Create/Select Terraform workspace before deployment
  4. Run terraform plan -var-file="<.tfvars file> to check for any errors and see what will be built
  5. Run terraform apply -var-file="<.tfvars file> to deploy infrastructure

Example Deployment Script

#!/usr/bin/env bash

if [[ ! -d .terraform ]]; then
  terraform init
fi
if ! terraform workspace list 2>&1 | grep -qi "$ENVIRONMENT"; then
  terraform workspace new "$ENVIRONMENT"
fi
terraform workspace select "$ENVIRONMENT"
terraform get
terraform plan -var-file=$1
terraform apply -var-file=$1

Example

module "api_lambda_sqs" {
  source  = "crisboarna/terraform-aws-api-gateway-lambda-sqs"
  version = "v1.6.0"

  #Global
  region = "eu-west-1"
  project = "Awesome Project"
   
  #API Gateway
  api_gw_method = "POST"

  #Lambda
  lambda_function_name = "Awesome Endpoint"
  lambda_description = "Awesome HTTP Endpoint Lambda"
  lambda_runtime = "nodejs8.10"
  lambda_handler = "dist/bin/lambda.handler"
  lambda_timeout = 30
  lambda_code_s3_bucket = "awesome-project-bucket"
  lambda_code_s3_key = "awesome-project.zip"
  lambda_code_s3_storage_class = "ONEZONE_IA"
  lambda_code_s3_bucket_visibility = "private"
  lambda_zip_path = "../../awesome-project.zip"
  lambda_memory_size = 256
  lambda_vpc_security_group_ids = [aws_security_group.vpc_security_group.id]
  lambda_vpc_subnet_ids = [aws_subnet.vpc_subnet_a.id]
  lambda_layers = [data.aws_lambda_layer_version.layer.arn]

  #SQS
  sqs_queue_names = ["SQS_QUEUE_NAME"]
  sqs_queue_delay_seconds = [0]
  sqs_queue_max_message_sizes = [2046]
  sqs_queue_message_retention_seconds = [34565]
  sqs_queue_receive_wait_time_seconds = [10]
  sqs_queue_fifos = [true]
  sqs_queue_content_based_deduplications = [true]
  sqs_dead_letter_max_receive_counts = [3]
  
  #Tags
  tags = {
    project = "Awesome Project"
    managedby = "Terraform"
  }
  
  #Lambda Environment variables
  environment_variables = {
    NODE_ENV = "production"
  }
}

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.