-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create reusable lambda module; optimize package size
- Loading branch information
Showing
5 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module "golang_lambda" { | ||
source = "terraform-aws-modules/lambda/aws" | ||
|
||
create_function = true | ||
function_name = "${local.project_name}-${local.name}-lambda" | ||
description = var.description | ||
handler = var.go_handler | ||
runtime = "go1.x" | ||
source_path = [{ | ||
path = "${var.repo_dir}/lambda_golang/" | ||
commands = ["${local.go_build_flags} go build -o ./builds/${var.go_handler} ./cmd/${var.go_handler}", ":zip ./builds/${var.go_handler} ."] | ||
patterns = ["${var.go_handler}"] | ||
}] | ||
publish = true | ||
|
||
timeout = 900 | ||
cloudwatch_logs_retention_in_days = 7 | ||
|
||
reserved_concurrent_executions = -1 | ||
|
||
attach_policy_json = var.attach_policy_json | ||
policy_json = var.policy_json | ||
|
||
attach_policy_statements = var.attach_policy_statements | ||
policy_statements = var.policy_statements | ||
|
||
environment_variables = merge({ | ||
SLACK_WEBHOOK_URL = var.slack_post_webhook_url | ||
ENV = local.environment | ||
DEBUG = tostring(var.debug) | ||
}, var.environment_variables) | ||
|
||
tags = { | ||
Project = local.project_name | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
locals { | ||
project_name = var.environment_name != "" ? "${var.project_alias}-${var.environment_name}" : "${var.project_alias}" | ||
environment = var.environment_name != "" ? var.environment_name : "prod" | ||
} | ||
|
||
locals { | ||
# amd64 is the x86 instruction set | ||
# arm is not (like M1), not supported by AWS lambda go runtime yet | ||
# https://stackoverflow.com/questions/26951940/how-do-i-make-go-get-to-build-against-x86-64-instead-of-i386 | ||
go_build_flags = "GOOS=linux GOARCH=amd64 CGO_ENABLED=0 " | ||
|
||
name = replace(var.go_handler, "_", "-") | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
output lambda_function_arn { | ||
value = module.golang_lambda.lambda_function_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// unique variables | ||
|
||
variable description { | ||
type = string | ||
description = "Describe what the function is doing." | ||
} | ||
|
||
variable go_handler { | ||
type = string | ||
description = "The directory name of the go submodule." | ||
} | ||
|
||
variable debug { | ||
type = bool | ||
default = true | ||
description = "Set development mode to debug or not" | ||
} | ||
|
||
// upstream variables | ||
|
||
variable "project_alias" { | ||
type = string | ||
description = "Name prefix used for step function and related resources, including the domain name, so please only use [0-9a-z_-]" | ||
} | ||
|
||
variable environment_name { | ||
type = string | ||
default = "" | ||
description = "Empty string for Production, otherwise the environment name e.g. dev, stage, etc, make sure to use lowercase (s3 bucket only allows lower)" | ||
} | ||
|
||
variable "slack_post_webhook_url" { | ||
type = string | ||
} | ||
|
||
variable repo_dir { | ||
type = string | ||
description = "The absolute path of git repository path" | ||
} | ||
|
||
// downstream variables | ||
|
||
variable attach_policy_json { | ||
type = bool | ||
default = false | ||
} | ||
|
||
variable policy_json { | ||
type = string | ||
default = null | ||
} | ||
|
||
variable attach_policy_statements { | ||
type = bool | ||
default = false | ||
} | ||
|
||
variable policy_statements { | ||
type = map | ||
default = {} | ||
} | ||
|
||
variable environment_variables { | ||
type = map | ||
default = {} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
terraform { | ||
required_version = ">= 1.0.2" # minimum version that supports M1 | ||
|
||
required_providers { | ||
# please use env var to pass over credentials | ||
# https://registry.terraform.io/providers/hashicorp/aws/latest/docs | ||
aws = ">= 4.9.0" # minimum version based on `terraform init` error message | ||
} | ||
} |