Created
January 28, 2019 19:13
-
-
Save sbchisholm/337bb3a5a53fc5e9caf6f0be74db7402 to your computer and use it in GitHub Desktop.
API Gateway/Lambda Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resource "aws_api_gateway_rest_api" "example" { | |
name = "ServerlessExample" | |
description = "Terraform Serverless Application Example" | |
} | |
resource "aws_api_gateway_resource" "proxy" { | |
rest_api_id = "${aws_api_gateway_rest_api.example.id}" | |
parent_id = "${aws_api_gateway_rest_api.example.root_resource_id}" | |
path_part = "{proxy+}" | |
} | |
resource "aws_api_gateway_method" "proxy" { | |
rest_api_id = "${aws_api_gateway_rest_api.example.id}" | |
resource_id = "${aws_api_gateway_resource.proxy.id}" | |
http_method = "ANY" | |
authorization = "NONE" | |
} | |
resource "aws_api_gateway_integration" "lambda" { | |
rest_api_id = "${aws_api_gateway_rest_api.example.id}" | |
resource_id = "${aws_api_gateway_method.proxy.resource_id}" | |
http_method = "${aws_api_gateway_method.proxy.http_method}" | |
integration_http_method = "POST" | |
type = "AWS_PROXY" | |
uri = "${aws_lambda_function.example.invoke_arn}" | |
} | |
resource "aws_api_gateway_method" "proxy_root" { | |
rest_api_id = "${aws_api_gateway_rest_api.example.id}" | |
resource_id = "${aws_api_gateway_rest_api.example.root_resource_id}" | |
http_method = "ANY" | |
authorization = "NONE" | |
} | |
resource "aws_api_gateway_integration" "lambda_root" { | |
rest_api_id = "${aws_api_gateway_rest_api.example.id}" | |
resource_id = "${aws_api_gateway_method.proxy_root.resource_id}" | |
http_method = "${aws_api_gateway_method.proxy_root.http_method}" | |
integration_http_method = "POST" | |
type = "AWS_PROXY" | |
uri = "${aws_lambda_function.example.invoke_arn}" | |
} | |
resource "aws_api_gateway_deployment" "example" { | |
depends_on = [ | |
"aws_api_gateway_integration.lambda", | |
"aws_api_gateway_integration.lambda_root", | |
] | |
rest_api_id = "${aws_api_gateway_rest_api.example.id}" | |
stage_name = "test" | |
} | |
output "base_url" { | |
value = "${aws_api_gateway_deployment.example.invoke_url}" | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
provider "aws" { | |
region = "us-east-1" | |
} | |
variable "app_version" { | |
} | |
resource "aws_lambda_function" "example" { | |
function_name = "ServerlessExample" | |
# The bucket name as created earlier with "aws s3api create-bucket" | |
s3_bucket = "analyzere-terraform-serverless-example" | |
s3_key = "v${var.app_version}/example.zip" | |
# "main" is the filename within the zip file (main.js) and "handler" | |
# is the name of the property under which the handler function was | |
# exported in that file. | |
handler = "main.handler" | |
runtime = "nodejs6.10" | |
role = "${aws_iam_role.lambda_exec.arn}" | |
} | |
# IAM role which dictates what other AWS services the Lambda function | |
# may access. | |
resource "aws_iam_role" "lambda_exec" { | |
name = "serverless_example_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_permission" "apigw" { | |
statement_id = "AllowAPIGatewayInvoke" | |
action = "lambda:InvokeFunction" | |
function_name = "${aws_lambda_function.example.arn}" | |
principal = "apigateway.amazonaws.com" | |
# The /*/* portion grants access from any method on any resource | |
# within the API Gateway "REST API". | |
source_arn = "${aws_api_gateway_deployment.example.execution_arn}/*/*" | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
exports.handler = function(event, context, callback) { | |
var response = { | |
statusCode: 200, | |
headers: { | |
'Content-Type': 'text/html; charset=utf-8' | |
}, | |
body: '<p>Bonjour au monde!</p>' | |
} | |
callback(null, response) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment