Skip to content

Instantly share code, notes, and snippets.

@calvernaz
Created January 11, 2022 21:10
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 calvernaz/c50b6ffff11584b7ba827fad3fc72048 to your computer and use it in GitHub Desktop.
Save calvernaz/c50b6ffff11584b7ba827fad3fc72048 to your computer and use it in GitHub Desktop.
resource "aws_api_gateway_rest_api" "api" {
name = "example"
}
resource "aws_api_gateway_resource" "resource" {
rest_api_id = aws_api_gateway_rest_api.api.id
parent_id = aws_api_gateway_rest_api.api.root_resource_id
path_part = "resource"
}
resource "aws_api_gateway_resource" "fallback-proxy-resource" {
rest_api_id = aws_api_gateway_rest_api.api.id
parent_id = aws_api_gateway_resource.resource.id
path_part = "{proxy+}"
}
resource "aws_api_gateway_method" "fallback-proxy-method" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.fallback-proxy-resource.id
http_method = "ANY"
authorization = "NONE"
request_parameters = {
"method.request.path.proxy" = true
"method.request.header.authorization" = false
}
}
resource "aws_api_gateway_integration" "fallback-proxy-method-integration" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.fallback-proxy-resource.id
http_method = aws_api_gateway_method.fallback-proxy-method.http_method
type = "HTTP_PROXY"
integration_http_method = "ANY"
uri = "https://httpbin.org/anything/{proxy}"
request_parameters = {
"integration.request.path.proxy" = "method.request.path.proxy"
"integration.request.header.authorization" = "method.request.header.authorization"
}
}
resource "aws_api_gateway_resource" "test-resource" {
rest_api_id = aws_api_gateway_rest_api.api.id
parent_id = aws_api_gateway_resource.resource.id
path_part = "test"
}
resource "aws_api_gateway_method" "test-get-method" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.test-resource.id
http_method = "ANY"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "integration" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.test-resource.id
http_method = aws_api_gateway_method.test-get-method.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.lambda.invoke_arn
}
resource "aws_lambda_function" "lambda" {
filename = "lambda.zip"
function_name = "mylambda"
role = aws_iam_role.role.arn
handler = "lambda.lambda_handler"
runtime = "python3.6"
source_code_hash = filebase64sha256("lambda.zip")
}
# IAM
resource "aws_iam_role" "role" {
name = "myrole"
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
POLICY
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment