Skip to content

Instantly share code, notes, and snippets.

@rcrowley
Created February 15, 2024 17:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcrowley/7c79ce4998a9868486f75f61a95697d7 to your computer and use it in GitHub Desktop.
Save rcrowley/7c79ce4998a9868486f75f61a95697d7 to your computer and use it in GitHub Desktop.
The simplest custom internal tool, protected by the Substrate Intranet, BYO functionality
data "archive_file" "example" {
output_file_mode = "0666" # before umask
output_path = "${path.module}/example.zip"
source_file = "${path.module}/example/index.js"
type = "zip"
}
data "aws_apigatewayv2_apis" "substrate" {
name = "Substrate"
protocol_type = "HTTP"
}
data "aws_apigatewayv2_api" "substrate" {
api_id = tolist(data.aws_apigatewayv2_apis.substrate.ids)[0]
}
data "aws_iam_role" "substrate" {
name = "Substrate"
}
resource "aws_apigatewayv2_integration" "example" {
api_id = data.aws_apigatewayv2_api.substrate.id
integration_method = "POST"
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.example.invoke_arn
}
resource "aws_apigatewayv2_route" "example" {
api_id = data.aws_apigatewayv2_api.substrate.id
authorization_type = "CUSTOM"
authorizer_id = data.aws_apigatewayv2_api.substrate.tags["AuthorizerId"]
route_key = "ANY /example"
target = "integrations/${aws_apigatewayv2_integration.example.id}"
}
resource "aws_cloudwatch_log_group" "example" {
name = "/aws/lambda/Example"
retention_in_days = 1
}
resource "aws_lambda_function" "example" {
depends_on = [aws_cloudwatch_log_group.example]
filename = "${path.module}/example.zip"
function_name = "Example"
handler = "index.handler"
role = data.aws_iam_role.substrate.arn
runtime = "nodejs18.x"
source_code_hash = data.archive_file.example.output_base64sha256
timeout = 60
}
resource "aws_lambda_permission" "example" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.example.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${data.aws_apigatewayv2_api.substrate.execution_arn}/*"
}
// example/index.js, the entrypoint to the Lambda function
exports.handler = async function(event, context) {
return {
body: "example\n",
headers: {"Content-Type": "text/plain"},
isBase64Encoded: false,
statusCode: 200,
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment