Skip to content

Instantly share code, notes, and snippets.

@patrick91
Created June 15, 2017 17:05
Show Gist options
  • Save patrick91/aca81c5f4de1e21b261fedb1d5839214 to your computer and use it in GitHub Desktop.
Save patrick91/aca81c5f4de1e21b261fedb1d5839214 to your computer and use it in GitHub Desktop.
resource "aws_api_gateway_method" "ResourceMethod" {
rest_api_id = "${var.rest_api_id}"
resource_id = "${var.resource_id}"
http_method = "${var.http_method}"
authorization = "${var.authorization}"
request_parameters = "${var.request_parameters}"
request_models = { "application/json" = "${var.request_model}" }
}
resource "aws_api_gateway_integration" "ResourceMethodIntegration" {
rest_api_id = "${var.rest_api_id}"
resource_id = "${var.resource_id}"
http_method = "${aws_api_gateway_method.ResourceMethod.http_method}"
type = "AWS"
uri = "arn:aws:apigateway:${var.region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${var.region}:${var.account_id}:function:${var.lambda_name}/invocations"
integration_http_method = "POST"
request_templates = { "application/json" = "${var.integration_request_template}" }
}
resource "aws_api_gateway_integration_response" "ResourceMethodIntegration200" {
rest_api_id = "${var.rest_api_id}"
resource_id = "${var.resource_id}"
http_method = "${aws_api_gateway_method.ResourceMethod.http_method}"
status_code = "${aws_api_gateway_method_response.ResourceMethod200.status_code}"
response_templates = { "application/json" = "$input.json('$')" }
}
resource "aws_api_gateway_method_response" "ResourceMethod200" {
rest_api_id = "${var.rest_api_id}"
resource_id = "${var.resource_id}"
http_method = "${aws_api_gateway_method.ResourceMethod.http_method}"
status_code = "200"
response_models = { "application/json" = "${var.response_model}" }
}
data "aws_region" "current" {
current = true
}
data "aws_caller_identity" "current" {}
resource "aws_api_gateway_rest_api" "ab_api" {
name = "API"
}
resource "aws_api_gateway_deployment" "ab_api" {
rest_api_id = "${aws_api_gateway_rest_api.ab_api.id}"
stage_name = "${terraform.env}"
depends_on = ["module.cms_post"]
}
resource "aws_api_gateway_resource" "cms" {
rest_api_id = "${aws_api_gateway_rest_api.ab_api.id}"
parent_id = "${aws_api_gateway_rest_api.ab_api.root_resource_id}"
path_part = "cms"
}
module "cms_post" {
source = "./api-gateway"
rest_api_id = "${aws_api_gateway_rest_api.ab_api.id}"
resource_id = "${aws_api_gateway_resource.cms.id}"
http_method = "POST"
lambda_name = "${aws_lambda_function.lambda_cms.function_name}"
account_id = "${data.aws_caller_identity.current.account_id}"
region = "${data.aws_region.current.name}"
integration_request_template = "#set($inputRoot = $input.path('$')){}"
request_model = "Empty"
integration_response_template = "#set($inputRoot = $input.path('$')){}"
response_model = "Empty"
integration_error_template = "#set ($errorMessageObj = $util.parseJson($input.path('$.errorMessage')) {\"message\" :\"$errorMessageObj.message\"}"
authorization = "AWS_IAM"
}
# First, we need a role to play with Lambda
resource "aws_iam_role" "iam_role_for_lambda_cms" {
name = "iam_role_for_lambda_cms"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
resource "aws_lambda_function" "lambda_cms" {
function_name = "${terraform.env}-contentful-ql"
role = "${aws_iam_role.iam_role_for_lambda_cms.arn}"
handler = "app.handler"
filename = "tmp/contentful-ql.zip"
source_code_hash = "${base64sha256(file("tmp/contentful-ql.zip"))}"
runtime = "nodejs6.10"
environment {
variables = {
foo = "bar"
}
}
}
resource "aws_lambda_permission" "permission_allow_calling_post_from_apigateway" {
statement_id = "permission_allow_calling_post_from_apigateway"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.lambda_cms.arn}"
principal = "apigateway.amazonaws.com"
source_arn = "arn:aws:execute-api:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${aws_api_gateway_rest_api.ab_api.id}/*/POST/*"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment