Skip to content

Commit

Permalink
create reusable lambda module; optimize package size
Browse files Browse the repository at this point in the history
  • Loading branch information
rivernews committed Sep 30, 2022
1 parent 8d72d88 commit 3977565
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
36 changes: 36 additions & 0 deletions cloud_module/media_lambda/lambda.tf
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
}
}
13 changes: 13 additions & 0 deletions cloud_module/media_lambda/locals.tf
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, "_", "-")
}
3 changes: 3 additions & 0 deletions cloud_module/media_lambda/outputs.tf
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
}
66 changes: 66 additions & 0 deletions cloud_module/media_lambda/variables.tf
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 = {}
}
9 changes: 9 additions & 0 deletions cloud_module/media_lambda/version.tf
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
}
}

0 comments on commit 3977565

Please sign in to comment.