Skip to content

Instantly share code, notes, and snippets.

@seanturner026
Created April 18, 2021 19:56
Show Gist options
  • Select an option

  • Save seanturner026/850b4e79758385f19e2df554e8d3d2df to your computer and use it in GitHub Desktop.

Select an option

Save seanturner026/850b4e79758385f19e2df554e8d3d2df to your computer and use it in GitHub Desktop.
locals {
service_name = replace(var.service_name, "-", "_")
lambdas = {
events_debug_logger = {
description = "Logs eventbridge events for ${var.service_name}."
timeout = 10
}
}
null = {
lambda_binary_exists = { for key, _ in local.lambdas : key => fileexists("${path.module}/lambdas/bin/${key}") }
}
}
resource "null_resource" "lambda_build" {
for_each = local.lambdas
triggers = {
binary_exists = local.null.lambda_binary_exists[each.key]
main = join("", [
for file in fileset("${path.module}/lambdas/cmd/${each.key}", "*.go") : filebase64("${path.module}/lambdas/cmd/${each.key}/${file}")
])
}
provisioner "local-exec" {
command = "export GO111MODULE=on"
}
provisioner "local-exec" {
command = "GOOS=linux go build -ldflags '-s -w' -o ${path.module}/lambdas/bin/${each.key} ${path.module}/lambdas/cmd/${each.key}/."
}
}
data "archive_file" "this" {
depends_on = [null_resource.lambda_build]
for_each = local.lambdas
type = "zip"
source_file = "${path.module}/lambdas/bin/${each.key}"
output_path = "${path.module}/lambdas/archive/${each.key}.zip"
}
resource "aws_lambda_function" "this" {
for_each = local.lambdas
filename = "${path.module}/lambdas/archive/${each.key}.zip"
function_name = "${each.key}_${local.service_name}"
description = each.value.description
role = aws_iam_role.this.arn
handler = each.key
publish = false
source_code_hash = data.archive_file.this[each.key].output_base64sha256
runtime = "go1.x"
timeout = "10"
tags = var.tags
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment