minimal-lambda-python-terraform

The most minimal AWS Lambda + Python + Terraform setup - https://www.davidbegin.com/the-most-minimal-aws-lambda-function-with-python-terraform/

Pure Nim score 15/100 · last commit 2020-09-30 · 1 stars · tests present · no docs generated

Summary

Latest Version Unknown
License Unknown
CI Status Failing
Stars 1
Forks 0
Open Issues 0
Last Commit 2020-09-30
Downloads 0
Last Indexed 2026-07-31 04:42

Installation

nimble install minimal-lambda-python-terraform
choosenim install minimal-lambda-python-terraform
git clone https://gitlab.com/russomi/minimal-lambda-python-terraform

OS Compatibility

Platform Linux macOS Windows FreeBSD OpenBSD NetBSD Android iOS WASM Embedded
minimal-lambda-python-terraform - - - - - - -

README

minimal-lambda-python-terraform

Create the python function

Note: For this example we are naming the file: lambda.py , which is important to sync up with the terraform setup:

import json

def handler(event, context):
    print("Received event: " + json.dumps(event, indent=2))

Package as a zip

zip lambda lambda.py

Define the terraform setup

In a file named anything ending with .tf* For Example: main.tf

provider "aws" {
  region = "us-east-1"
}

variable "function_name" {
  default = "minimal_lambda_function"
}

variable "handler" {
  default = "lambda.handler"
}

variable "runtime" {
  default = "python3.6"
}

resource "aws_lambda_function" "lambda_function" {
  role             = "${aws_iam_role.lambda_exec_role.arn}"
  handler          = "${var.handler}"
  runtime          = "${var.runtime}"
  filename         = "lambda.zip"
  function_name    = "${var.function_name}"
  source_code_hash = "${base64sha256(file("lambda.zip"))}"
}

resource "aws_iam_role" "lambda_exec_role" {
  name        = "lambda_exec"
  path        = "/"
  description = "Allows Lambda Function to call AWS services on your behalf."

  assume_role_policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "lambda.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
EOF
}

Create the lambda

terraform apply

Invoke the lambda

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

Resources