AWS LAMBDA Terraform Module

Terraform module to provision Lambda Functions on AWS.

Usage

Simple setup

Create a simple Lambda Function with default configurations.

    module "function" {
        source           = "app.terraform.io/ncodelibrary/lambda/aws"
        version          = "0.1.2"
        identifier       = "example-function"
        iam_role         = "arn:aws:iam::XXXXXXXX:role/XXXXXXX"
        handler          = "lambda_function.lambda_handler"
        runtime          = "python3.7"
        s3_bucket        = "example-default"
        s3_key           = "lambda_function.zip"
        source_code_hash = "vIBhQhmMUkvbKhUr3FaUm51UproR7zRAKhg4RFAgfAw="
        tags             = {
            Owner       = "sysops"
            env         = "dev"
            Cost_Center = "XYZ"
        }
        environment      = {
            SUBJECT = "nClouds"
        }
    }   

For more details on a working example, please visit examples/simple

Advanced Setup

If you want to create a Lambda Function with enhanced configuration e.g in a VPC, with event triggers, custom layers etc., you can use the module like this:

    module "function" {
        source           = "app.terraform.io/ncodelibrary/lambda/aws"
        version          = "0.1.2"
        identifier       = "example-function"
        iam_role         = "arn:aws:iam::XXXXXXXX:role/XXXXXXX"
        handler          = "lambda_function.lambda_handler"
        runtime          = "python3.7"
        s3_bucket        = "example-default"
        s3_key           = "lambda_function.zip"
        source_code_hash = "vIBhQhmMUkvbKhUr3FaUm51UproR7zRAKhg4RFAgfAw="
        tags             = {
            Owner       = "sysops"
            env         = "dev"
            Cost_Center = "XYZ"
        }
        environment      = {
            SUBJECT = "nClouds"
        }
        layers                = ["arn:aws:lambda:us-east-1:XXXXX:layer:python_requests:2]
        event_source_arn      = "arn:aws:sqs:us-east-1:XXXXXXXX:example-default"
        create_trigger        = true
        log_retention_in_days = 30
        memory_size           = 256
        timeout               = 30
        security_group_ids    = [sg-xxxxxxxxx]
        subnet_ids            = ["subnet-xxxxxxxxxxx", "subnet-xxxxxxxxxx"]
    }

For more options refer to a working example at examples/advanced

Examples

Here are some working examples of using this module:

Requirements

Name Version
terraform >= 0.12

Providers

Name Version
aws n/a

Modules

No Modules.

Resources

Name
aws_cloudwatch_log_group
aws_lambda_event_source_mapping
aws_lambda_function

Inputs

Name Description Type Default Required
append_workspace Appends the terraform workspace at the end of resource names, - bool true no
create_trigger Set to true if you specify ‘event_source_arn’ parameter bool false no
description Description of what your Lambda Function does string "Deployed by terraform" no
environment A map that defines environment variables for the Lambda function map {} no
event_source_arn The event source ARN - can be a Kinesis stream, DynamoDB stream, or SQS queue. If specified set ‘create_trigger’ parameter to true string null no
handler The function entrypoint in your code string n/a yes
iam_role IAM role attached to the Lambda Function string n/a yes
identifier The name of the security group string n/a yes
layers List of Lambda Layer Version ARNs (maximum of 5) to attach to your Lambda Function list(string) [] no
log_retention_in_days Specifies the number of days you want to retain log number 14 no
memory_size Amount of memory in MB your Lambda Function can use at runtime number 128 no
publish Whether to publish creation/change as new Lambda Function Version bool false no
reserved_concurrent_executions The amount of reserved concurrent executions for this lambda function number -1 no
runtime n/a string n/a yes
s3_bucket The S3 bucket location containing the function’s deployment package string n/a yes
s3_key The S3 key of an object containing the function’s deployment package string n/a yes
security_group_ids A list of security group IDs associated with the Lambda function list(string) [] no
source_code_hash Used to trigger updates. Must be set to a base64-encoded SHA256 hash of the package file specified string n/a yes
subnet_ids A list of subnet IDs associated with the Lambda function list(string) [] no
tags Tags to be applied to the resource map {} no
timeout The amount of time your Lambda Function has to run in seconds number 3 no

Outputs

Name Description
output n/a

Contributing

If you want to contribute to this repository check all the guidelines specified here before submitting a new PR.