Skip to content

Instantly share code, notes, and snippets.

@juliankrispel
Last active September 17, 2019 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliankrispel/035fd526d2337998c8e35e2bd011fbc0 to your computer and use it in GitHub Desktop.
Save juliankrispel/035fd526d2337998c8e35e2bd011fbc0 to your computer and use it in GitHub Desktop.
Terraform config for basic lambda and api gateway integration
provider "aws" {
region = "eu-west-1"
profile = "my-profile"
}
resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_lambda_function" "test_lambda" {
filename = "function.zip"
function_name = "lambda_example_terraform"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "index.handler"
# The filebase64sha256() function is available in Terraform 0.11.12 and later
# For Terraform 0.11.11 and earlier, use the base64sha256() function and the file() function:
# source_code_hash = "${base64sha256(file("lambda_function_payload.zip"))}"
source_code_hash = "${filebase64sha256("function.zip")}"
runtime = "nodejs8.10"
}
resource "aws_lambda_permission" "handler_apigateway_permission" {
function_name = "${aws_lambda_function.test_lambda.arn}"
action = "lambda:InvokeFunction"
statement_id = "AllowExecutionFromApiGateway"
principal = "apigateway.amazonaws.com"
}
resource "aws_api_gateway_rest_api" "rest_api" {
name = "ServerlessExample"
description = "Terraform Serverless Application Example"
body = "${templatefile("${path.module}/openapi.yml", {
execution_arn = "${aws_lambda_function.test_lambda.invoke_arn}"
})}"
depends_on = ["aws_lambda_function.test_lambda"]
}
resource "aws_api_gateway_deployment" "stage" {
rest_api_id = "${aws_api_gateway_rest_api.rest_api.id}"
stage_name = "testing"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment