├── examples ├── complete │ ├── .gitignore │ ├── providers.tf │ ├── handler.js │ ├── fixtures.us-east-2.tfvars │ ├── versions.tf │ ├── outputs.tf │ ├── variables.tf │ ├── main.tf │ └── context.tf └── docker-image │ ├── providers.tf │ ├── handler.js │ ├── outputs.tf │ ├── Dockerfile │ ├── versions.tf │ ├── fixtures.us-east-2.tfvars │ ├── variables.tf │ ├── main.tf │ └── context.tf ├── test ├── .gitignore ├── src │ ├── .gitignore │ ├── Makefile │ ├── common.go │ ├── examples_docker_image_test.go │ ├── go.mod │ └── examples_complete_test.go ├── Makefile.alpine └── Makefile ├── .github ├── ISSUE_TEMPLATE │ ├── question.md │ ├── config.yml │ ├── bug_report.md │ ├── feature_request.md │ ├── bug_report.yml │ └── feature_request.yml ├── mergify.yml ├── banner.png ├── workflows │ ├── release.yml │ ├── scheduled.yml │ ├── chatops.yml │ └── branch.yml ├── renovate.json ├── settings.yml ├── PULL_REQUEST_TEMPLATE.md └── CODEOWNERS ├── versions.tf ├── lambda-permissions.tf ├── .gitignore ├── atmos.yaml ├── .editorconfig ├── outputs.tf ├── iam-role.tf ├── main.tf ├── README.yaml ├── variables.tf ├── context.tf ├── LICENSE └── README.md /examples/complete/.gitignore: -------------------------------------------------------------------------------- 1 | *.zip -------------------------------------------------------------------------------- /test/.gitignore: -------------------------------------------------------------------------------- 1 | .test-harness 2 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/question.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.github/mergify.yml: -------------------------------------------------------------------------------- 1 | extends: .github 2 | -------------------------------------------------------------------------------- /test/src/.gitignore: -------------------------------------------------------------------------------- 1 | .gopath 2 | vendor/ 3 | -------------------------------------------------------------------------------- /examples/complete/providers.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = var.region 3 | } 4 | -------------------------------------------------------------------------------- /examples/docker-image/providers.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = var.region 3 | } 4 | -------------------------------------------------------------------------------- /.github/banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cloudposse/terraform-aws-lambda-function/HEAD/.github/banner.png -------------------------------------------------------------------------------- /examples/complete/handler.js: -------------------------------------------------------------------------------- 1 | exports.handler = async function (_event, _context) { 2 | return {data: "Hello World"}; 3 | }; 4 | -------------------------------------------------------------------------------- /examples/docker-image/handler.js: -------------------------------------------------------------------------------- 1 | exports.handler = async function (_event, _context) { 2 | return {data: "Hello World"}; 3 | }; 4 | -------------------------------------------------------------------------------- /examples/docker-image/outputs.tf: -------------------------------------------------------------------------------- 1 | output "arn" { 2 | description = "ARN of the lambda function" 3 | value = module.lambda.arn 4 | } 5 | -------------------------------------------------------------------------------- /test/Makefile.alpine: -------------------------------------------------------------------------------- 1 | ifneq (,$(wildcard /sbin/apk)) 2 | ## Install all dependencies for alpine 3 | deps:: init 4 | @apk add --update terraform-docs@cloudposse json2hcl@cloudposse 5 | endif 6 | -------------------------------------------------------------------------------- /versions.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_version = ">= 0.14" 3 | 4 | required_providers { 5 | aws = { 6 | source = "hashicorp/aws" 7 | version = ">= 3.0" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /examples/docker-image/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM public.ecr.aws/lambda/nodejs:14 2 | 3 | # add app 4 | COPY handler.js ${LAMBDA_TASK_ROOT} 5 | 6 | ENV NODE_ENV=production 7 | 8 | # start app 9 | CMD [ "handler.handler" ] 10 | -------------------------------------------------------------------------------- /examples/docker-image/versions.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_version = ">= 0.14" 3 | 4 | required_providers { 5 | aws = { 6 | source = "hashicorp/aws" 7 | version = ">= 3.0" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /examples/docker-image/fixtures.us-east-2.tfvars: -------------------------------------------------------------------------------- 1 | region = "us-east-2" 2 | namespace = "eg" 3 | environment = "ue2" 4 | stage = "test" 5 | 6 | function_name = "example-docker-image" 7 | handler = "handler.handler" 8 | runtime = "nodejs14.x" 9 | -------------------------------------------------------------------------------- /examples/complete/fixtures.us-east-2.tfvars: -------------------------------------------------------------------------------- 1 | region = "us-east-2" 2 | namespace = "eg" 3 | environment = "ue2" 4 | stage = "test" 5 | 6 | function_name = "example-complete" 7 | handler = "handler.handler" 8 | runtime = "nodejs20.x" 9 | ephemeral_storage_size = 1024 10 | -------------------------------------------------------------------------------- /examples/complete/versions.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_version = ">= 0.14" 3 | 4 | required_providers { 5 | aws = { 6 | source = "hashicorp/aws" 7 | version = ">= 3.0" 8 | } 9 | archive = { 10 | source = "hashicorp/archive" 11 | version = ">= 2.0" 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: release 3 | on: 4 | release: 5 | types: 6 | - published 7 | 8 | permissions: 9 | id-token: write 10 | contents: write 11 | pull-requests: write 12 | 13 | jobs: 14 | terraform-module: 15 | uses: cloudposse/.github/.github/workflows/shared-release-branches.yml@main 16 | secrets: inherit 17 | -------------------------------------------------------------------------------- /.github/renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "config:base", 4 | ":preserveSemverRanges", 5 | ":rebaseStalePrs" 6 | ], 7 | "baseBranches": ["main"], 8 | "labels": ["auto-update"], 9 | "dependencyDashboardAutoclose": true, 10 | "enabledManagers": ["terraform"], 11 | "terraform": { 12 | "ignorePaths": ["**/context.tf"] 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /.github/settings.yml: -------------------------------------------------------------------------------- 1 | # Upstream changes from _extends are only recognized when modifications are made to this file in the default branch. 2 | _extends: .github 3 | repository: 4 | name: terraform-aws-lambda-function 5 | description: A module for launching Lambda Fuctions 6 | homepage: https://cloudposse.com/accelerate 7 | topics: "" 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /lambda-permissions.tf: -------------------------------------------------------------------------------- 1 | resource "aws_lambda_permission" "invoke_function" { 2 | for_each = local.enabled ? { for i, permission in var.invoke_function_permissions : i => permission } : {} 3 | 4 | action = "lambda:InvokeFunction" 5 | function_name = aws_lambda_function.this[0].function_name 6 | principal = each.value.principal 7 | source_arn = each.value.source_arn 8 | } 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Local .terraform directories 2 | **/.terraform/* 3 | 4 | # .tfstate files 5 | *.tfstate 6 | *.tfstate.* 7 | .terraform 8 | .terraform.tfstate.lock.info 9 | .terraform.lock.hcl 10 | 11 | **/.idea 12 | **/*.iml 13 | 14 | # Cloud Posse Build Harness https://github.com/cloudposse/build-harness 15 | **/.build-harness 16 | **/build-harness 17 | 18 | # Crash log files 19 | crash.log 20 | test.log 21 | -------------------------------------------------------------------------------- /.github/workflows/scheduled.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: scheduled 3 | on: 4 | workflow_dispatch: { } # Allows manually trigger this workflow 5 | schedule: 6 | - cron: "0 3 * * *" 7 | 8 | permissions: 9 | pull-requests: write 10 | id-token: write 11 | contents: write 12 | 13 | jobs: 14 | scheduled: 15 | uses: cloudposse/.github/.github/workflows/shared-terraform-scheduled.yml@main 16 | secrets: inherit 17 | -------------------------------------------------------------------------------- /.github/workflows/chatops.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: chatops 3 | on: 4 | issue_comment: 5 | types: [created] 6 | 7 | permissions: 8 | pull-requests: write 9 | id-token: write 10 | contents: write 11 | statuses: write 12 | 13 | jobs: 14 | test: 15 | uses: cloudposse/.github/.github/workflows/shared-terraform-chatops.yml@main 16 | if: ${{ github.event.issue.pull_request && contains(github.event.comment.body, '/terratest') }} 17 | secrets: inherit 18 | -------------------------------------------------------------------------------- /examples/complete/outputs.tf: -------------------------------------------------------------------------------- 1 | output "arn" { 2 | description = "ARN of the lambda function" 3 | value = module.lambda.arn 4 | } 5 | 6 | output "function_name" { 7 | description = "Lambda function name" 8 | value = module.lambda.function_name 9 | } 10 | 11 | output "role_name" { 12 | description = "Lambda IAM role name" 13 | value = module.lambda.role_name 14 | } 15 | 16 | output "cloudwatch_log_group_name" { 17 | description = "Name of Cloudwatch log group" 18 | value = module.lambda.cloudwatch_log_group_name 19 | } 20 | -------------------------------------------------------------------------------- /atmos.yaml: -------------------------------------------------------------------------------- 1 | # Atmos Configuration — powered by https://atmos.tools 2 | # 3 | # This configuration enables centralized, DRY, and consistent project scaffolding using Atmos. 4 | # 5 | # Included features: 6 | # - Organizational custom commands: https://atmos.tools/core-concepts/custom-commands 7 | # - Automated README generation: https://atmos.tools/cli/commands/docs/generate 8 | # 9 | 10 | # Import shared configuration used by all modules 11 | import: 12 | - https://raw.githubusercontent.com/cloudposse/.github/refs/heads/main/.github/atmos/terraform-module.yaml 13 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # Unix-style newlines with a newline ending every file 2 | [*] 3 | charset = utf-8 4 | end_of_line = lf 5 | indent_size = 2 6 | indent_style = space 7 | insert_final_newline = true 8 | trim_trailing_whitespace = true 9 | 10 | [*.{tf,tfvars}] 11 | indent_size = 2 12 | indent_style = space 13 | 14 | [*.md] 15 | max_line_length = 0 16 | trim_trailing_whitespace = false 17 | 18 | # Override for Makefile 19 | [{Makefile, makefile, GNUmakefile, Makefile.*}] 20 | tab_width = 2 21 | indent_style = tab 22 | indent_size = 4 23 | 24 | [COMMIT_EDITMSG] 25 | max_line_length = 0 26 | -------------------------------------------------------------------------------- /.github/workflows/branch.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Branch 3 | on: 4 | pull_request: 5 | branches: 6 | - main 7 | - release/** 8 | types: [opened, synchronize, reopened, labeled, unlabeled] 9 | push: 10 | branches: 11 | - main 12 | - release/v* 13 | paths-ignore: 14 | - '.github/**' 15 | - 'docs/**' 16 | - 'examples/**' 17 | - 'test/**' 18 | - 'README.md' 19 | 20 | permissions: {} 21 | 22 | jobs: 23 | terraform-module: 24 | uses: cloudposse/.github/.github/workflows/shared-terraform-module.yml@main 25 | secrets: inherit 26 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | 3 | contact_links: 4 | 5 | - name: Community Slack Team 6 | url: https://cloudposse.com/slack/ 7 | about: |- 8 | Please ask and answer questions here. 9 | 10 | - name: Office Hours 11 | url: https://cloudposse.com/office-hours/ 12 | about: |- 13 | Join us every Wednesday for FREE Office Hours (lunch & learn). 14 | 15 | - name: DevOps Accelerator Program 16 | url: https://cloudposse.com/accelerate/ 17 | about: |- 18 | Own your infrastructure in record time. We build it. You drive it. 19 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## what 2 | 3 | 7 | 8 | ## why 9 | 10 | 15 | 16 | ## references 17 | 18 | 22 | -------------------------------------------------------------------------------- /examples/docker-image/variables.tf: -------------------------------------------------------------------------------- 1 | variable "region" { 2 | type = string 3 | description = "AWS Region" 4 | } 5 | 6 | variable "function_name" { 7 | type = string 8 | description = "Unique name for the Lambda Function." 9 | } 10 | 11 | variable "handler" { 12 | type = string 13 | description = "The function entrypoint in your code." 14 | default = "" 15 | } 16 | 17 | variable "runtime" { 18 | type = string 19 | description = "The runtime environment for the Lambda function you are uploading." 20 | default = "" 21 | } 22 | 23 | variable "iam_policy_description" { 24 | type = string 25 | description = "Description of the IAM policy for the Lambda IAM role" 26 | default = "Minimum SSM read permissions for Lambda" 27 | } 28 | -------------------------------------------------------------------------------- /test/src/Makefile: -------------------------------------------------------------------------------- 1 | export TERRAFORM_VERSION ?= $(shell curl -s https://checkpoint-api.hashicorp.com/v1/check/terraform | jq -r -M '.current_version' | cut -d. -f1) 2 | 3 | .DEFAULT_GOAL : all 4 | .PHONY: all 5 | 6 | ## Default target 7 | all: test 8 | 9 | .PHONY : init 10 | ## Initialize tests 11 | init: 12 | @exit 0 13 | 14 | .PHONY : test 15 | ## Run tests 16 | test: init 17 | go mod download 18 | go test -v -timeout 60m -run TestExamplesComplete 19 | 20 | ## Run tests in docker container 21 | docker/test: 22 | docker run --name terratest --rm -it -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY -e AWS_SESSION_TOKEN -e GITHUB_TOKEN \ 23 | -e PATH="/usr/local/terraform/$(TERRAFORM_VERSION)/bin:/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" \ 24 | -v $(CURDIR)/../../:/module/ cloudposse/test-harness:latest -C /module/test/src test 25 | 26 | .PHONY : clean 27 | ## Clean up files 28 | clean: 29 | rm -rf ../../examples/complete/*.tfstate* 30 | -------------------------------------------------------------------------------- /examples/complete/variables.tf: -------------------------------------------------------------------------------- 1 | variable "region" { 2 | type = string 3 | description = "AWS Region" 4 | } 5 | 6 | variable "function_name" { 7 | type = string 8 | description = "Unique name for the Lambda Function." 9 | } 10 | 11 | variable "handler" { 12 | type = string 13 | description = "The function entrypoint in your code." 14 | default = "" 15 | } 16 | 17 | variable "runtime" { 18 | type = string 19 | description = "The runtime environment for the Lambda function you are uploading." 20 | default = "" 21 | } 22 | 23 | variable "iam_policy_description" { 24 | type = string 25 | description = "Description of the IAM policy for the Lambda IAM role" 26 | default = "Minimum SSM read permissions for Lambda" 27 | } 28 | 29 | variable "ephemeral_storage_size" { 30 | type = number 31 | description = "The amount of storage available to the function at runtime. Defaults to 512." 32 | default = 512 33 | } 34 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: 'bug' 6 | assignees: '' 7 | 8 | --- 9 | 10 | Found a bug? Maybe our [Slack Community](https://slack.cloudposse.com) can help. 11 | 12 | [![Slack Community](https://slack.cloudposse.com/badge.svg)](https://slack.cloudposse.com) 13 | 14 | ## Describe the Bug 15 | A clear and concise description of what the bug is. 16 | 17 | ## Expected Behavior 18 | A clear and concise description of what you expected to happen. 19 | 20 | ## Steps to Reproduce 21 | Steps to reproduce the behavior: 22 | 1. Go to '...' 23 | 2. Run '....' 24 | 3. Enter '....' 25 | 4. See error 26 | 27 | ## Screenshots 28 | If applicable, add screenshots or logs to help explain your problem. 29 | 30 | ## Environment (please complete the following information): 31 | 32 | Anything that will help us triage the bug will help. Here are some ideas: 33 | - OS: [e.g. Linux, OSX, WSL, etc] 34 | - Version [e.g. 10.15] 35 | 36 | ## Additional Context 37 | Add any other context about the problem here. -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature Request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: 'feature request' 6 | assignees: '' 7 | 8 | --- 9 | 10 | Have a question? Please checkout our [Slack Community](https://slack.cloudposse.com) or visit our [Slack Archive](https://archive.sweetops.com/). 11 | 12 | [![Slack Community](https://slack.cloudposse.com/badge.svg)](https://slack.cloudposse.com) 13 | 14 | ## Describe the Feature 15 | 16 | A clear and concise description of what the bug is. 17 | 18 | ## Expected Behavior 19 | 20 | A clear and concise description of what you expected to happen. 21 | 22 | ## Use Case 23 | 24 | Is your feature request related to a problem/challenge you are trying to solve? Please provide some additional context of why this feature or capability will be valuable. 25 | 26 | ## Describe Ideal Solution 27 | 28 | A clear and concise description of what you want to happen. If you don't know, that's okay. 29 | 30 | ## Alternatives Considered 31 | 32 | Explain what alternative solutions or features you've considered. 33 | 34 | ## Additional Context 35 | 36 | Add any other context or screenshots about the feature request here. 37 | -------------------------------------------------------------------------------- /outputs.tf: -------------------------------------------------------------------------------- 1 | output "arn" { 2 | description = "ARN of the lambda function" 3 | value = local.enabled ? aws_lambda_function.this[0].arn : null 4 | } 5 | 6 | output "invoke_arn" { 7 | description = "Invoke ARN of the lambda function" 8 | value = local.enabled ? aws_lambda_function.this[0].invoke_arn : null 9 | } 10 | 11 | output "qualified_arn" { 12 | description = "ARN identifying your Lambda Function Version (if versioning is enabled via publish = true)" 13 | value = local.enabled ? aws_lambda_function.this[0].qualified_arn : null 14 | } 15 | 16 | output "function_name" { 17 | description = "Lambda function name" 18 | value = local.enabled ? aws_lambda_function.this[0].function_name : null 19 | } 20 | 21 | output "role_name" { 22 | description = "Lambda IAM role name" 23 | value = local.enabled ? aws_iam_role.this[0].name : null 24 | } 25 | 26 | output "role_arn" { 27 | description = "Lambda IAM role ARN" 28 | value = local.enabled ? aws_iam_role.this[0].arn : null 29 | } 30 | 31 | output "cloudwatch_log_group_name" { 32 | description = "Name of Cloudwatch log group" 33 | value = local.enabled ? module.cloudwatch_log_group.log_group_name : null 34 | } 35 | -------------------------------------------------------------------------------- /test/src/common.go: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | import ( 4 | "math/rand" 5 | "strconv" 6 | "testing" 7 | "time" 8 | "strings" 9 | 10 | "github.com/gruntwork-io/terratest/modules/terraform" 11 | "github.com/stretchr/testify/assert" 12 | ) 13 | 14 | func testNoChanges(t *testing.T, terraformDir string) { 15 | rand.Seed(time.Now().UnixNano()) 16 | randID := strconv.Itoa(rand.Intn(100000)) 17 | attributes := []string{randID} 18 | 19 | terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ 20 | // The path to where our Terraform code is located 21 | TerraformDir: terraformDir, 22 | Upgrade: true, 23 | // Variables to pass to our Terraform code using -var-file options 24 | VarFiles: []string{"fixtures.us-east-2.tfvars"}, 25 | // We always include a random attribute so that parallel tests 26 | // and AWS resources do not interfere with each other 27 | Vars: map[string]interface{}{ 28 | "enabled": false, 29 | "attributes": attributes, 30 | }, 31 | }) 32 | 33 | terraform.Init(t, terraformOptions) 34 | plan := terraform.Plan(t, terraformOptions) 35 | planContainsNoChanges := strings.Contains(plan, "No changes.") || strings.Contains(plan, "0 to add, 0 to change, 0 to destroy.") 36 | 37 | assert.True(t, planContainsNoChanges) 38 | } 39 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Use this file to define individuals or teams that are responsible for code in a repository. 2 | # Read more: 3 | # 4 | # Order is important: the last matching pattern has the highest precedence 5 | 6 | # These owners will be the default owners for everything 7 | * @cloudposse/engineering @cloudposse/contributors 8 | 9 | # Cloud Posse must review any changes to Makefiles 10 | **/Makefile @cloudposse/engineering 11 | **/Makefile.* @cloudposse/engineering 12 | 13 | # Cloud Posse must review any changes to GitHub actions 14 | .github/* @cloudposse/engineering 15 | 16 | # Cloud Posse must review any changes to standard context definition, 17 | # but some changes can be rubber-stamped. 18 | **/*.tf @cloudposse/engineering @cloudposse/contributors @cloudposse/approvers 19 | README.yaml @cloudposse/engineering @cloudposse/contributors @cloudposse/approvers 20 | README.md @cloudposse/engineering @cloudposse/contributors @cloudposse/approvers 21 | docs/*.md @cloudposse/engineering @cloudposse/contributors @cloudposse/approvers 22 | 23 | # Cloud Posse Admins must review all changes to CODEOWNERS or the mergify configuration 24 | .github/mergify.yml @cloudposse/admins 25 | .github/CODEOWNERS @cloudposse/admins 26 | -------------------------------------------------------------------------------- /test/Makefile: -------------------------------------------------------------------------------- 1 | TEST_HARNESS ?= https://github.com/cloudposse/test-harness.git 2 | TEST_HARNESS_BRANCH ?= master 3 | TEST_HARNESS_PATH = $(realpath .test-harness) 4 | BATS_ARGS ?= --tap 5 | BATS_LOG ?= test.log 6 | 7 | # Define a macro to run the tests 8 | define RUN_TESTS 9 | @echo "Running tests in $(1)" 10 | @cd $(1) && bats $(BATS_ARGS) $(addsuffix .bats,$(addprefix $(TEST_HARNESS_PATH)/test/terraform/,$(TESTS))) 11 | endef 12 | 13 | default: all 14 | 15 | -include Makefile.* 16 | 17 | ## Provision the test-harnesss 18 | .test-harness: 19 | [ -d $@ ] || git clone --depth=1 -b $(TEST_HARNESS_BRANCH) $(TEST_HARNESS) $@ 20 | 21 | ## Initialize the tests 22 | init: .test-harness 23 | 24 | ## Install all dependencies (OS specific) 25 | deps:: 26 | @exit 0 27 | 28 | ## Clean up the test harness 29 | clean: 30 | [ "$(TEST_HARNESS_PATH)" == "/" ] || rm -rf $(TEST_HARNESS_PATH) 31 | 32 | ## Run all tests 33 | all: module examples/complete 34 | 35 | ## Run basic sanity checks against the module itself 36 | module: export TESTS ?= installed lint module-pinning provider-pinning validate terraform-docs input-descriptions output-descriptions 37 | module: deps 38 | $(call RUN_TESTS, ../) 39 | 40 | ## Run tests against example 41 | examples/complete: export TESTS ?= installed lint validate 42 | examples/complete: deps 43 | $(call RUN_TESTS, ../$@) 44 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | description: Create a report to help us improve 4 | labels: ["bug"] 5 | assignees: [""] 6 | body: 7 | - type: markdown 8 | attributes: 9 | value: | 10 | Found a bug? 11 | 12 | Please checkout our [Slack Community](https://slack.cloudposse.com) 13 | or visit our [Slack Archive](https://archive.sweetops.com/). 14 | 15 | [![Slack Community](https://slack.cloudposse.com/badge.svg)](https://slack.cloudposse.com) 16 | 17 | - type: textarea 18 | id: concise-description 19 | attributes: 20 | label: Describe the Bug 21 | description: A clear and concise description of what the bug is. 22 | placeholder: What is the bug about? 23 | validations: 24 | required: true 25 | 26 | - type: textarea 27 | id: expected 28 | attributes: 29 | label: Expected Behavior 30 | description: A clear and concise description of what you expected. 31 | placeholder: What happened? 32 | validations: 33 | required: true 34 | 35 | - type: textarea 36 | id: reproduction-steps 37 | attributes: 38 | label: Steps to Reproduce 39 | description: Steps to reproduce the behavior. 40 | placeholder: How do we reproduce it? 41 | validations: 42 | required: true 43 | 44 | - type: textarea 45 | id: screenshots 46 | attributes: 47 | label: Screenshots 48 | description: If applicable, add screenshots or logs to help explain. 49 | validations: 50 | required: false 51 | 52 | - type: textarea 53 | id: environment 54 | attributes: 55 | label: Environment 56 | description: Anything that will help us triage the bug. 57 | placeholder: | 58 | - OS: [e.g. Linux, OSX, WSL, etc] 59 | - Version [e.g. 10.15] 60 | - Module version 61 | - Terraform version 62 | validations: 63 | required: false 64 | 65 | - type: textarea 66 | id: additional 67 | attributes: 68 | label: Additional Context 69 | description: | 70 | Add any other context about the problem here. 71 | validations: 72 | required: false 73 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature Request 3 | description: Suggest an idea for this project 4 | labels: ["feature request"] 5 | assignees: [""] 6 | body: 7 | - type: markdown 8 | attributes: 9 | value: | 10 | Have a question? 11 | 12 | Please checkout our [Slack Community](https://slack.cloudposse.com) 13 | or visit our [Slack Archive](https://archive.sweetops.com/). 14 | 15 | [![Slack Community](https://slack.cloudposse.com/badge.svg)](https://slack.cloudposse.com) 16 | 17 | - type: textarea 18 | id: concise-description 19 | attributes: 20 | label: Describe the Feature 21 | description: A clear and concise description of what the feature is. 22 | placeholder: What is the feature about? 23 | validations: 24 | required: true 25 | 26 | - type: textarea 27 | id: expected 28 | attributes: 29 | label: Expected Behavior 30 | description: A clear and concise description of what you expected. 31 | placeholder: What happened? 32 | validations: 33 | required: true 34 | 35 | - type: textarea 36 | id: use-case 37 | attributes: 38 | label: Use Case 39 | description: | 40 | Is your feature request related to a problem/challenge you are trying 41 | to solve? 42 | 43 | Please provide some additional context of why this feature or 44 | capability will be valuable. 45 | validations: 46 | required: true 47 | 48 | - type: textarea 49 | id: ideal-solution 50 | attributes: 51 | label: Describe Ideal Solution 52 | description: A clear and concise description of what you want to happen. 53 | validations: 54 | required: true 55 | 56 | - type: textarea 57 | id: alternatives-considered 58 | attributes: 59 | label: Alternatives Considered 60 | description: Explain alternative solutions or features considered. 61 | validations: 62 | required: false 63 | 64 | - type: textarea 65 | id: additional 66 | attributes: 67 | label: Additional Context 68 | description: | 69 | Add any other context about the problem here. 70 | validations: 71 | required: false 72 | -------------------------------------------------------------------------------- /test/src/examples_docker_image_test.go: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | import ( 4 | "encoding/json" 5 | "strings" 6 | "testing" 7 | 8 | "github.com/aws/aws-sdk-go/aws" 9 | "github.com/aws/aws-sdk-go/aws/session" 10 | "github.com/aws/aws-sdk-go/service/lambda" 11 | "github.com/gruntwork-io/terratest/modules/random" 12 | "github.com/gruntwork-io/terratest/modules/terraform" 13 | "github.com/stretchr/testify/assert" 14 | ) 15 | 16 | // Test the Terraform module in examples/docker-image using Terratest. 17 | func TestExamplesDockerImage(t *testing.T) { 18 | randID := strings.ToLower(random.UniqueId()) 19 | attributes := []string{randID} 20 | 21 | terraformOptions := &terraform.Options{ 22 | // The path to where our Terraform code is located 23 | TerraformDir: "../../examples/docker-image", 24 | Upgrade: true, 25 | // Variables to pass to our Terraform code using -var-file options 26 | VarFiles: []string{"fixtures.us-east-2.tfvars"}, 27 | // We always include a random attribute so that parallel tests 28 | // and AWS resources do not interfere with each other 29 | Vars: map[string]interface{}{ 30 | "attributes": attributes, 31 | }, 32 | } 33 | 34 | // At the end of the test, run `terraform destroy` to clean up any resources that were created 35 | defer terraform.Destroy(t, terraformOptions) 36 | 37 | // This will run `terraform init` and `terraform apply` and fail the test if there are any errors 38 | terraform.InitAndApply(t, terraformOptions) 39 | 40 | // Run `terraform output` to get the value of an output variable 41 | arn := terraform.Output(t, terraformOptions, "arn") 42 | assert.NotNil(t, arn) 43 | 44 | sess := session.Must(session.NewSessionWithOptions(session.Options{ 45 | SharedConfigState: session.SharedConfigEnable, 46 | })) 47 | 48 | client := lambda.New(sess, &aws.Config{Region: aws.String("us-east-2")}) 49 | 50 | result, err := client.Invoke(&lambda.InvokeInput{FunctionName: aws.String(arn)}) 51 | assert.Nil(t, err) 52 | 53 | var output map[string]interface{} 54 | err = json.Unmarshal([]byte(result.Payload), &output) 55 | assert.Nil(t, err) 56 | 57 | assert.Equal(t, "Hello World", output["data"]) 58 | } 59 | 60 | // Test the Terraform module in examples/docker-image doesn't attempt to create resources with enabled=false. 61 | func TestExamplesDockerImageDisabled(t *testing.T) { 62 | testNoChanges(t, "../../examples/docker-image") 63 | } 64 | -------------------------------------------------------------------------------- /examples/docker-image/main.tf: -------------------------------------------------------------------------------- 1 | data "aws_region" "this" { 2 | count = module.this.enabled ? 1 : 0 3 | } 4 | 5 | data "aws_caller_identity" "this" { 6 | count = module.this.enabled ? 1 : 0 7 | } 8 | 9 | module "label" { 10 | source = "cloudposse/label/null" 11 | version = "0.25.0" 12 | attributes = [var.function_name] 13 | 14 | context = module.this.context 15 | } 16 | 17 | # Cloud Posse does NOT recommend building and pushing images to ECR via Terraform code. This is a job for your CI/CD 18 | # pipeline. It is only done here for convenience and so that the example can be run locally. 19 | module "ecr" { 20 | source = "cloudposse/ecr/aws" 21 | version = "0.34.0" 22 | name = module.label.id 23 | 24 | context = module.this.context 25 | } 26 | 27 | # Need to sleep for a few seconds after creating the ECR repository before we can push to it 28 | resource "time_sleep" "wait_15_seconds" { 29 | count = module.this.enabled ? 1 : 0 30 | 31 | create_duration = "15s" 32 | 33 | depends_on = [module.ecr] 34 | } 35 | 36 | resource "null_resource" "docker_build" { 37 | count = module.this.enabled ? 1 : 0 38 | 39 | provisioner "local-exec" { 40 | command = "docker build -t ${module.ecr.repository_url} ." 41 | } 42 | } 43 | 44 | resource "null_resource" "docker_login" { 45 | count = module.this.enabled ? 1 : 0 46 | 47 | provisioner "local-exec" { 48 | command = "aws ecr get-login-password --region ${join("", data.aws_region.this.*.name)} | docker login --username AWS --password-stdin ${join("", data.aws_caller_identity.this.*.account_id)}.dkr.ecr.${join("", data.aws_region.this.*.name)}.amazonaws.com" 49 | } 50 | 51 | depends_on = [null_resource.docker_build] 52 | } 53 | 54 | resource "null_resource" "docker_push" { 55 | count = module.this.enabled ? 1 : 0 56 | 57 | provisioner "local-exec" { 58 | command = "docker push ${module.ecr.repository_url}:latest" 59 | } 60 | 61 | depends_on = [ 62 | time_sleep.wait_15_seconds, 63 | null_resource.docker_login 64 | ] 65 | } 66 | 67 | module "lambda" { 68 | source = "../.." 69 | 70 | function_name = module.label.id 71 | image_uri = "${module.ecr.repository_url}:latest" 72 | package_type = "Image" 73 | iam_policy_description = var.iam_policy_description 74 | 75 | context = module.this.context 76 | 77 | depends_on = [null_resource.docker_push] 78 | } 79 | -------------------------------------------------------------------------------- /test/src/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/cloudposse/terraform-aws-lambda-function 2 | 3 | go 1.24 4 | 5 | toolchain go1.24.0 6 | 7 | require ( 8 | github.com/aws/aws-sdk-go v1.44.122 9 | github.com/gruntwork-io/terratest v0.40.17 10 | github.com/stretchr/testify v1.8.0 11 | ) 12 | 13 | require ( 14 | cloud.google.com/go v0.104.0 // indirect 15 | cloud.google.com/go/compute v1.10.0 // indirect 16 | cloud.google.com/go/iam v0.5.0 // indirect 17 | cloud.google.com/go/storage v1.27.0 // indirect 18 | github.com/agext/levenshtein v1.2.3 // indirect 19 | github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect 20 | github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect 21 | github.com/davecgh/go-spew v1.1.1 // indirect 22 | github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect 23 | github.com/golang/protobuf v1.5.2 // indirect 24 | github.com/google/go-cmp v0.5.9 // indirect 25 | github.com/google/uuid v1.3.0 // indirect 26 | github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect 27 | github.com/googleapis/gax-go/v2 v2.6.0 // indirect 28 | github.com/hashicorp/errwrap v1.0.0 // indirect 29 | github.com/hashicorp/go-cleanhttp v0.5.2 // indirect 30 | github.com/hashicorp/go-getter v1.7.5 // indirect 31 | github.com/hashicorp/go-multierror v1.1.0 // indirect 32 | github.com/hashicorp/go-safetemp v1.0.0 // indirect 33 | github.com/hashicorp/go-version v1.6.0 // indirect 34 | github.com/hashicorp/hcl/v2 v2.9.1 // indirect 35 | github.com/hashicorp/terraform-json v0.13.0 // indirect 36 | github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a // indirect 37 | github.com/jmespath/go-jmespath v0.4.0 // indirect 38 | github.com/klauspost/compress v1.15.11 // indirect 39 | github.com/mattn/go-zglob v0.0.2-0.20190814121620-e3c945676326 // indirect 40 | github.com/mitchellh/go-homedir v1.1.0 // indirect 41 | github.com/mitchellh/go-testing-interface v1.14.1 // indirect 42 | github.com/mitchellh/go-wordwrap v1.0.1 // indirect 43 | github.com/pmezard/go-difflib v1.0.0 // indirect 44 | github.com/tmccombs/hcl2json v0.3.3 // indirect 45 | github.com/ulikunitz/xz v0.5.10 // indirect 46 | github.com/zclconf/go-cty v1.9.1 // indirect 47 | go.opencensus.io v0.23.0 // indirect 48 | golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect 49 | golang.org/x/net v0.1.0 // indirect 50 | golang.org/x/oauth2 v0.1.0 // indirect 51 | golang.org/x/sys v0.1.0 // indirect 52 | golang.org/x/text v0.4.0 // indirect 53 | golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect 54 | google.golang.org/api v0.100.0 // indirect 55 | google.golang.org/appengine v1.6.7 // indirect 56 | google.golang.org/genproto v0.0.0-20221025140454-527a21cfbd71 // indirect 57 | google.golang.org/grpc v1.50.1 // indirect 58 | google.golang.org/protobuf v1.28.1 // indirect 59 | gopkg.in/yaml.v3 v3.0.1 // indirect 60 | ) 61 | -------------------------------------------------------------------------------- /test/src/examples_complete_test.go: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | import ( 4 | "encoding/json" 5 | "strings" 6 | "testing" 7 | 8 | "github.com/aws/aws-sdk-go/aws" 9 | "github.com/aws/aws-sdk-go/aws/session" 10 | "github.com/aws/aws-sdk-go/service/lambda" 11 | "github.com/gruntwork-io/terratest/modules/random" 12 | "github.com/gruntwork-io/terratest/modules/terraform" 13 | "github.com/stretchr/testify/assert" 14 | ) 15 | 16 | // Test the Terraform module in examples/complete using Terratest. 17 | func TestExamplesComplete(t *testing.T) { 18 | randID := strings.ToLower(random.UniqueId()) 19 | attributes := []string{randID} 20 | 21 | terraformOptions := &terraform.Options{ 22 | // The path to where our Terraform code is located 23 | TerraformDir: "../../examples/complete", 24 | Upgrade: true, 25 | // Variables to pass to our Terraform code using -var-file options 26 | VarFiles: []string{"fixtures.us-east-2.tfvars"}, 27 | // We always include a random attribute so that parallel tests 28 | // and AWS resources do not interfere with each other 29 | Vars: map[string]interface{}{ 30 | "attributes": attributes, 31 | }, 32 | } 33 | 34 | // At the end of the test, run `terraform destroy` to clean up any resources that were created 35 | defer terraform.Destroy(t, terraformOptions) 36 | 37 | // This will run `terraform init` and `terraform apply` and fail the test if there are any errors 38 | terraform.InitAndApply(t, terraformOptions) 39 | 40 | // Run `terraform output` to get the value of an output variable 41 | arn := terraform.Output(t, terraformOptions, "arn") 42 | function_name := terraform.Output(t, terraformOptions, "function_name") 43 | role_name := terraform.Output(t, terraformOptions, "role_name") 44 | cloudwatch_log_group_name := terraform.Output(t, terraformOptions, "cloudwatch_log_group_name") 45 | 46 | // Validate values returned by terraform 47 | assert.NotNil(t, arn) 48 | assert.Equal(t, "eg-ue2-test-" + randID + "-example-complete", function_name) 49 | assert.Equal(t, "eg-ue2-test-" + randID + "-example-complete-us-east-2", role_name) 50 | assert.Equal(t, "/aws/lambda/eg-ue2-test-" + randID + "-example-complete", cloudwatch_log_group_name) 51 | 52 | sess := session.Must(session.NewSessionWithOptions(session.Options{ 53 | SharedConfigState: session.SharedConfigEnable, 54 | })) 55 | 56 | client := lambda.New(sess, &aws.Config{Region: aws.String("us-east-2")}) 57 | 58 | result, err := client.Invoke(&lambda.InvokeInput{FunctionName: aws.String(arn)}) 59 | assert.Nil(t, err) 60 | 61 | var output map[string]interface{} 62 | err = json.Unmarshal([]byte(result.Payload), &output) 63 | assert.Nil(t, err) 64 | 65 | assert.Equal(t, "Hello World", output["data"]) 66 | } 67 | 68 | // Test the Terraform module in examples/complete doesn't attempt to create resources with enabled=false. 69 | func TestExamplesCompleteDisabled(t *testing.T) { 70 | testNoChanges(t, "../../examples/complete") 71 | } 72 | -------------------------------------------------------------------------------- /iam-role.tf: -------------------------------------------------------------------------------- 1 | locals { 2 | custom_iam_policy_arns_map = length(var.custom_iam_policy_arns) > 0 ? { for i, arn in var.custom_iam_policy_arns : i => arn } : {} 3 | } 4 | 5 | resource "aws_iam_role" "this" { 6 | count = local.enabled ? 1 : 0 7 | 8 | name = var.role_name == null ? "${var.function_name}-${local.region_name}" : var.role_name 9 | assume_role_policy = join("", data.aws_iam_policy_document.assume_role_policy[*].json) 10 | permissions_boundary = var.permissions_boundary 11 | 12 | tags = module.this.tags 13 | } 14 | 15 | data "aws_iam_policy_document" "assume_role_policy" { 16 | count = local.enabled ? 1 : 0 17 | 18 | statement { 19 | actions = ["sts:AssumeRole"] 20 | 21 | principals { 22 | type = "Service" 23 | identifiers = concat(["lambda.amazonaws.com"], var.lambda_at_edge_enabled ? ["edgelambda.amazonaws.com"] : []) 24 | } 25 | } 26 | } 27 | 28 | resource "aws_iam_role_policy_attachment" "cloudwatch_logs" { 29 | count = local.enabled ? 1 : 0 30 | 31 | policy_arn = "arn:${local.partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" 32 | role = aws_iam_role.this[0].name 33 | } 34 | 35 | resource "aws_iam_role_policy_attachment" "cloudwatch_insights" { 36 | count = local.enabled && var.cloudwatch_lambda_insights_enabled ? 1 : 0 37 | 38 | policy_arn = "arn:${local.partition}:iam::aws:policy/CloudWatchLambdaInsightsExecutionRolePolicy" 39 | role = aws_iam_role.this[0].name 40 | } 41 | 42 | resource "aws_iam_role_policy_attachment" "vpc_access" { 43 | count = local.enabled && var.vpc_config != null ? 1 : 0 44 | 45 | policy_arn = "arn:${local.partition}:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole" 46 | role = aws_iam_role.this[0].name 47 | } 48 | 49 | resource "aws_iam_role_policy_attachment" "xray" { 50 | count = local.enabled && var.tracing_config_mode != null ? 1 : 0 51 | 52 | policy_arn = "arn:${local.partition}:iam::aws:policy/AWSXRayDaemonWriteAccess" 53 | role = aws_iam_role.this[0].name 54 | } 55 | 56 | # Allow Lambda to access specific SSM parameters 57 | data "aws_iam_policy_document" "ssm" { 58 | count = try((local.enabled && var.ssm_parameter_names != null && length(var.ssm_parameter_names) > 0), false) ? 1 : 0 59 | 60 | statement { 61 | actions = [ 62 | "ssm:GetParameter", 63 | "ssm:GetParameters", 64 | "ssm:GetParametersByPath", 65 | ] 66 | 67 | resources = formatlist("arn:${local.partition}:ssm:${local.region_name}:${local.account_id}:parameter%s", var.ssm_parameter_names) 68 | } 69 | } 70 | 71 | resource "aws_iam_policy" "ssm" { 72 | count = try((local.enabled && var.ssm_parameter_names != null && length(var.ssm_parameter_names) > 0), false) ? 1 : 0 73 | 74 | name = "${var.function_name}-ssm-policy-${local.region_name}" 75 | description = var.iam_policy_description 76 | policy = data.aws_iam_policy_document.ssm[count.index].json 77 | 78 | tags = module.this.tags 79 | } 80 | 81 | resource "aws_iam_role_policy_attachment" "ssm" { 82 | count = try((local.enabled && var.ssm_parameter_names != null && length(var.ssm_parameter_names) > 0), false) ? 1 : 0 83 | 84 | policy_arn = aws_iam_policy.ssm[count.index].arn 85 | role = aws_iam_role.this[0].name 86 | } 87 | 88 | resource "aws_iam_role_policy_attachment" "custom" { 89 | for_each = local.enabled ? local.custom_iam_policy_arns_map : {} 90 | 91 | role = aws_iam_role.this[0].name 92 | policy_arn = each.value 93 | } 94 | 95 | resource "aws_iam_role_policy" "inline" { 96 | count = try((local.enabled && var.inline_iam_policy != null), false) ? 1 : 0 97 | 98 | role = aws_iam_role.this[0].name 99 | policy = var.inline_iam_policy 100 | } 101 | -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | locals { 2 | enabled = module.this.enabled 3 | account_id = local.enabled ? data.aws_caller_identity.this[0].account_id : null 4 | partition = local.enabled ? data.aws_partition.this[0].partition : null 5 | region_name = local.enabled ? data.aws_region.this[0].name : null 6 | } 7 | 8 | module "cloudwatch_log_group" { 9 | source = "cloudposse/cloudwatch-logs/aws" 10 | version = "0.6.6" 11 | 12 | enabled = module.this.enabled 13 | 14 | iam_role_enabled = false 15 | kms_key_arn = var.cloudwatch_logs_kms_key_arn 16 | retention_in_days = var.cloudwatch_logs_retention_in_days 17 | name = "/aws/lambda/${var.function_name}" 18 | 19 | tags = module.this.tags 20 | } 21 | 22 | resource "aws_lambda_function" "this" { 23 | count = module.this.enabled ? 1 : 0 24 | 25 | architectures = var.architectures 26 | description = var.description 27 | filename = var.filename 28 | function_name = var.function_name 29 | handler = var.handler 30 | image_uri = var.image_uri 31 | kms_key_arn = var.kms_key_arn 32 | layers = var.layers 33 | memory_size = var.memory_size 34 | package_type = var.package_type 35 | publish = var.publish 36 | reserved_concurrent_executions = var.reserved_concurrent_executions 37 | role = aws_iam_role.this[0].arn 38 | runtime = var.runtime 39 | s3_bucket = var.s3_bucket 40 | s3_key = var.s3_key 41 | s3_object_version = var.s3_object_version 42 | source_code_hash = var.source_code_hash 43 | tags = module.this.tags 44 | timeout = var.timeout 45 | 46 | dynamic "dead_letter_config" { 47 | for_each = try(length(var.dead_letter_config_target_arn), 0) > 0 ? [true] : [] 48 | 49 | content { 50 | target_arn = var.dead_letter_config_target_arn 51 | } 52 | } 53 | 54 | dynamic "environment" { 55 | for_each = var.lambda_environment != null ? [var.lambda_environment] : [] 56 | content { 57 | variables = environment.value.variables 58 | } 59 | } 60 | 61 | dynamic "image_config" { 62 | for_each = length(var.image_config) > 0 ? [true] : [] 63 | content { 64 | command = lookup(var.image_config, "command", null) 65 | entry_point = lookup(var.image_config, "entry_point", null) 66 | working_directory = lookup(var.image_config, "working_directory", null) 67 | } 68 | } 69 | 70 | dynamic "tracing_config" { 71 | for_each = var.tracing_config_mode != null ? [true] : [] 72 | content { 73 | mode = var.tracing_config_mode 74 | } 75 | } 76 | 77 | dynamic "vpc_config" { 78 | for_each = var.vpc_config != null ? [var.vpc_config] : [] 79 | content { 80 | security_group_ids = vpc_config.value.security_group_ids 81 | subnet_ids = vpc_config.value.subnet_ids 82 | } 83 | } 84 | 85 | dynamic "ephemeral_storage" { 86 | for_each = var.ephemeral_storage_size != null ? [var.ephemeral_storage_size] : [] 87 | content { 88 | size = var.ephemeral_storage_size 89 | } 90 | } 91 | 92 | depends_on = [module.cloudwatch_log_group] 93 | 94 | lifecycle { 95 | ignore_changes = [last_modified] 96 | } 97 | } 98 | 99 | data "aws_partition" "this" { 100 | count = local.enabled ? 1 : 0 101 | } 102 | 103 | data "aws_region" "this" { 104 | count = local.enabled ? 1 : 0 105 | } 106 | 107 | data "aws_caller_identity" "this" { 108 | count = local.enabled ? 1 : 0 109 | } 110 | -------------------------------------------------------------------------------- /examples/complete/main.tf: -------------------------------------------------------------------------------- 1 | locals { 2 | enabled = module.this.enabled 3 | 4 | # The policy name has to be at least 20 characters 5 | policy_name_inside = "${module.label.id}-inside" 6 | policy_name_outside = "${module.label.id}-outside" 7 | 8 | policy_arn_prefix = format( 9 | "arn:%s:iam::%s:policy", 10 | join("", data.aws_partition.current[*].partition), 11 | join("", data.aws_caller_identity.current[*].account_id), 12 | ) 13 | 14 | policy_arn_inside = format("%s/%s", local.policy_arn_prefix, local.policy_name_inside) 15 | 16 | policy_json = jsonencode({ 17 | Version = "2012-10-17" 18 | Statement = [ 19 | { 20 | Action = [ 21 | "ec2:Describe*", 22 | ] 23 | Effect = "Allow" 24 | Resource = "*" 25 | }, 26 | ] 27 | }) 28 | } 29 | 30 | module "label" { 31 | source = "cloudposse/label/null" 32 | version = "0.25.0" 33 | attributes = [var.function_name] 34 | 35 | context = module.this.context 36 | } 37 | 38 | data "aws_partition" "current" { 39 | count = local.enabled ? 1 : 0 40 | } 41 | 42 | data "aws_caller_identity" "current" { 43 | count = local.enabled ? 1 : 0 44 | } 45 | 46 | data "archive_file" "lambda_zip" { 47 | count = local.enabled ? 1 : 0 48 | type = "zip" 49 | source_file = "handler.js" 50 | output_path = "lambda_function.zip" 51 | } 52 | 53 | resource "aws_iam_policy" "inside" { 54 | count = local.enabled ? 1 : 0 55 | name = local.policy_name_inside 56 | path = "/" 57 | description = "The policy attached inside the Lambda module" 58 | 59 | policy = local.policy_json 60 | } 61 | 62 | resource "aws_iam_policy" "outside" { 63 | count = local.enabled ? 1 : 0 64 | name = local.policy_name_outside 65 | path = "/" 66 | description = "The policy attached outside the Lambda module" 67 | 68 | policy = local.policy_json 69 | } 70 | 71 | resource "aws_iam_role_policy_attachment" "outside" { 72 | count = local.enabled ? 1 : 0 73 | role = module.lambda.role_name 74 | policy_arn = aws_iam_policy.outside[0].arn 75 | } 76 | 77 | module "lambda" { 78 | source = "../.." 79 | 80 | filename = join("", data.archive_file.lambda_zip[*].output_path) 81 | function_name = module.label.id 82 | handler = var.handler 83 | runtime = var.runtime 84 | iam_policy_description = var.iam_policy_description 85 | ephemeral_storage_size = var.ephemeral_storage_size 86 | 87 | custom_iam_policy_arns = [ 88 | "arn:aws:iam::aws:policy/job-function/ViewOnlyAccess", 89 | local.policy_arn_inside, 90 | # aws_iam_policy.inside[0].id, # This will result in an error message and is why we use local.policy_name_inside 91 | ] 92 | 93 | inline_iam_policy = <<-JSON 94 | { 95 | "Version": "2012-10-17", 96 | "Statement": [ 97 | { 98 | "Effect": "Deny", 99 | "Action": "ec2:DescribeInstanceTypes", 100 | "Resource": "*" 101 | } 102 | ] 103 | } 104 | JSON 105 | 106 | invoke_function_permissions = [ 107 | { 108 | principal = "s3.amazonaws.com" 109 | source_arn = join("", aws_s3_bucket.example[*].arn) 110 | } 111 | ] 112 | 113 | context = module.this.context 114 | 115 | depends_on = [aws_iam_policy.inside] 116 | } 117 | 118 | resource "aws_s3_bucket" "example" { 119 | count = local.enabled ? 1 : 0 120 | } 121 | 122 | resource "aws_s3_bucket_notification" "example" { 123 | count = local.enabled ? 1 : 0 124 | 125 | bucket = aws_s3_bucket.example[0].id 126 | lambda_function { 127 | lambda_function_arn = module.lambda.arn 128 | events = ["s3:ObjectCreated:*"] 129 | } 130 | # Lambda permissions must be created prior to setting up the notification 131 | depends_on = [ 132 | module.lambda 133 | ] 134 | } 135 | -------------------------------------------------------------------------------- /README.yaml: -------------------------------------------------------------------------------- 1 | # Name of this project 2 | name: terraform-aws-lambda-function 3 | 4 | # License of this project 5 | license: "APACHE2" 6 | 7 | # Copyrights 8 | copyrights: 9 | - name: "Cloud Posse, LLC" 10 | url: "https://cloudposse.com" 11 | year: "2022" 12 | 13 | # Canonical GitHub repo 14 | github_repo: cloudposse/terraform-aws-lambda-function 15 | 16 | badges: 17 | - name: Latest Release 18 | image: https://img.shields.io/github/release/cloudposse/terraform-aws-lambda-function.svg?style=for-the-badge 19 | url: https://github.com/cloudposse/terraform-aws-lambda-function/releases/latest 20 | - name: Last Updated 21 | image: https://img.shields.io/github/last-commit/cloudposse/terraform-aws-lambda-function.svg?style=for-the-badge 22 | url: https://github.com/cloudposse/terraform-aws-lambda-function/commits 23 | - name: Slack Community 24 | image: https://slack.cloudposse.com/for-the-badge.svg 25 | url: https://cloudposse.com/slack 26 | references: 27 | - name: "Cloud Posse Documentation" 28 | url: "https://docs.cloudposse.com" 29 | description: "The Cloud Posse Developer Hub (documentation)" 30 | - name: "Terraform Standard Module Structure" 31 | description: "HashiCorp's standard module structure is a file and directory layout we recommend for reusable modules distributed in separate repositories." 32 | url: "https://www.terraform.io/docs/language/modules/develop/structure.html" 33 | - name: "Terraform Module Requirements" 34 | description: "HashiCorp's guidance on all the requirements for publishing a module. Meeting the requirements for publishing a module is extremely easy." 35 | url: "https://www.terraform.io/docs/registry/modules/publish.html#requirements" 36 | - name: "Terraform Version Pinning" 37 | description: "The required_version setting can be used to constrain which versions of the Terraform CLI can be used with your configuration" 38 | url: "https://www.terraform.io/docs/language/settings/index.html#specifying-a-required-terraform-version" 39 | 40 | 41 | # List any related terraform modules that this module may be used with or that this module depends on. 42 | 43 | # List any related terraform modules that this module may be used with or that this module depends on. 44 | related: 45 | - name: "terraform-null-label" 46 | description: "Terraform module designed to generate consistent names and tags for resources. Use terraform-null-label to implement a strict naming convention." 47 | url: "https://github.com/cloudposse/terraform-null-label" 48 | 49 | description: |- 50 | This module deploys an AWS Lambda function from a Zip file or from a Docker image. Additionally, it creates an IAM 51 | role for the Lambda function, which optionally attaches policies to allow for CloudWatch Logs, Cloudwatch Insights, 52 | VPC Access and X-Ray tracing. 53 | 54 | # How to use this module. Should be an easy example to copy and paste. 55 | usage: |- 56 | For a complete example, see [examples/complete](examples/complete). 57 | For automated tests of the complete example using [bats](https://github.com/bats-core/bats-core) and [Terratest](https://github.com/gruntwork-io/terratest) 58 | (which tests and deploys the example on AWS), see [test](test). 59 | 60 | ```hcl 61 | module "lambda" { 62 | source = "cloudposse/lambda-function/aws" 63 | version = "xxxx" 64 | 65 | filename = "lambda.zip" 66 | function_name = "my-function" 67 | handler = "handler.handler" 68 | runtime = "nodejs14.x" 69 | } 70 | ``` 71 | 72 | examples: |- 73 | - [`examples/complete`](https://github.com/cloudposse/terraform-aws-lambda-function/blob/main/examples/complete) - complete example of using this module 74 | - [`examples/docker-image`](https://github.com/cloudposse/terraform-aws-lambda-function/blob/main/examples/docker-image) - example of using Lambda with Docker images 75 | 76 | # Other files to include in this README from the project folder 77 | include: [] 78 | contributors: [] 79 | -------------------------------------------------------------------------------- /variables.tf: -------------------------------------------------------------------------------- 1 | variable "architectures" { 2 | type = list(string) 3 | description = <`, 17 | # with final values filled in. 18 | # 19 | # For example, when using defaults, `module.this.context.delimiter` 20 | # will be null, and `module.this.delimiter` will be `-` (hyphen). 21 | # 22 | 23 | module "this" { 24 | source = "cloudposse/label/null" 25 | version = "0.25.0" # requires Terraform >= 0.13.0 26 | 27 | enabled = var.enabled 28 | namespace = var.namespace 29 | tenant = var.tenant 30 | environment = var.environment 31 | stage = var.stage 32 | name = var.name 33 | delimiter = var.delimiter 34 | attributes = var.attributes 35 | tags = var.tags 36 | additional_tag_map = var.additional_tag_map 37 | label_order = var.label_order 38 | regex_replace_chars = var.regex_replace_chars 39 | id_length_limit = var.id_length_limit 40 | label_key_case = var.label_key_case 41 | label_value_case = var.label_value_case 42 | descriptor_formats = var.descriptor_formats 43 | labels_as_tags = var.labels_as_tags 44 | 45 | context = var.context 46 | } 47 | 48 | # Copy contents of cloudposse/terraform-null-label/variables.tf here 49 | 50 | variable "context" { 51 | type = any 52 | default = { 53 | enabled = true 54 | namespace = null 55 | tenant = null 56 | environment = null 57 | stage = null 58 | name = null 59 | delimiter = null 60 | attributes = [] 61 | tags = {} 62 | additional_tag_map = {} 63 | regex_replace_chars = null 64 | label_order = [] 65 | id_length_limit = null 66 | label_key_case = null 67 | label_value_case = null 68 | descriptor_formats = {} 69 | # Note: we have to use [] instead of null for unset lists due to 70 | # https://github.com/hashicorp/terraform/issues/28137 71 | # which was not fixed until Terraform 1.0.0, 72 | # but we want the default to be all the labels in `label_order` 73 | # and we want users to be able to prevent all tag generation 74 | # by setting `labels_as_tags` to `[]`, so we need 75 | # a different sentinel to indicate "default" 76 | labels_as_tags = ["unset"] 77 | } 78 | description = <<-EOT 79 | Single object for setting entire context at once. 80 | See description of individual variables for details. 81 | Leave string and numeric variables as `null` to use default value. 82 | Individual variable settings (non-null) override settings in context object, 83 | except for attributes, tags, and additional_tag_map, which are merged. 84 | EOT 85 | 86 | validation { 87 | condition = lookup(var.context, "label_key_case", null) == null ? true : contains(["lower", "title", "upper"], var.context["label_key_case"]) 88 | error_message = "Allowed values: `lower`, `title`, `upper`." 89 | } 90 | 91 | validation { 92 | condition = lookup(var.context, "label_value_case", null) == null ? true : contains(["lower", "title", "upper", "none"], var.context["label_value_case"]) 93 | error_message = "Allowed values: `lower`, `title`, `upper`, `none`." 94 | } 95 | } 96 | 97 | variable "enabled" { 98 | type = bool 99 | default = null 100 | description = "Set to false to prevent the module from creating any resources" 101 | } 102 | 103 | variable "namespace" { 104 | type = string 105 | default = null 106 | description = "ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique" 107 | } 108 | 109 | variable "tenant" { 110 | type = string 111 | default = null 112 | description = "ID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is for" 113 | } 114 | 115 | variable "environment" { 116 | type = string 117 | default = null 118 | description = "ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'" 119 | } 120 | 121 | variable "stage" { 122 | type = string 123 | default = null 124 | description = "ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'" 125 | } 126 | 127 | variable "name" { 128 | type = string 129 | default = null 130 | description = <<-EOT 131 | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'. 132 | This is the only ID element not also included as a `tag`. 133 | The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input. 134 | EOT 135 | } 136 | 137 | variable "delimiter" { 138 | type = string 139 | default = null 140 | description = <<-EOT 141 | Delimiter to be used between ID elements. 142 | Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. 143 | EOT 144 | } 145 | 146 | variable "attributes" { 147 | type = list(string) 148 | default = [] 149 | description = <<-EOT 150 | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`, 151 | in the order they appear in the list. New attributes are appended to the 152 | end of the list. The elements of the list are joined by the `delimiter` 153 | and treated as a single ID element. 154 | EOT 155 | } 156 | 157 | variable "labels_as_tags" { 158 | type = set(string) 159 | default = ["default"] 160 | description = <<-EOT 161 | Set of labels (ID elements) to include as tags in the `tags` output. 162 | Default is to include all labels. 163 | Tags with empty values will not be included in the `tags` output. 164 | Set to `[]` to suppress all generated tags. 165 | **Notes:** 166 | The value of the `name` tag, if included, will be the `id`, not the `name`. 167 | Unlike other `null-label` inputs, the initial setting of `labels_as_tags` cannot be 168 | changed in later chained modules. Attempts to change it will be silently ignored. 169 | EOT 170 | } 171 | 172 | variable "tags" { 173 | type = map(string) 174 | default = {} 175 | description = <<-EOT 176 | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`). 177 | Neither the tag keys nor the tag values will be modified by this module. 178 | EOT 179 | } 180 | 181 | variable "additional_tag_map" { 182 | type = map(string) 183 | default = {} 184 | description = <<-EOT 185 | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`. 186 | This is for some rare cases where resources want additional configuration of tags 187 | and therefore take a list of maps with tag key, value, and additional configuration. 188 | EOT 189 | } 190 | 191 | variable "label_order" { 192 | type = list(string) 193 | default = null 194 | description = <<-EOT 195 | The order in which the labels (ID elements) appear in the `id`. 196 | Defaults to ["namespace", "environment", "stage", "name", "attributes"]. 197 | You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. 198 | EOT 199 | } 200 | 201 | variable "regex_replace_chars" { 202 | type = string 203 | default = null 204 | description = <<-EOT 205 | Terraform regular expression (regex) string. 206 | Characters matching the regex will be removed from the ID elements. 207 | If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. 208 | EOT 209 | } 210 | 211 | variable "id_length_limit" { 212 | type = number 213 | default = null 214 | description = <<-EOT 215 | Limit `id` to this many characters (minimum 6). 216 | Set to `0` for unlimited length. 217 | Set to `null` for keep the existing setting, which defaults to `0`. 218 | Does not affect `id_full`. 219 | EOT 220 | validation { 221 | condition = var.id_length_limit == null ? true : var.id_length_limit >= 6 || var.id_length_limit == 0 222 | error_message = "The id_length_limit must be >= 6 if supplied (not null), or 0 for unlimited length." 223 | } 224 | } 225 | 226 | variable "label_key_case" { 227 | type = string 228 | default = null 229 | description = <<-EOT 230 | Controls the letter case of the `tags` keys (label names) for tags generated by this module. 231 | Does not affect keys of tags passed in via the `tags` input. 232 | Possible values: `lower`, `title`, `upper`. 233 | Default value: `title`. 234 | EOT 235 | 236 | validation { 237 | condition = var.label_key_case == null ? true : contains(["lower", "title", "upper"], var.label_key_case) 238 | error_message = "Allowed values: `lower`, `title`, `upper`." 239 | } 240 | } 241 | 242 | variable "label_value_case" { 243 | type = string 244 | default = null 245 | description = <<-EOT 246 | Controls the letter case of ID elements (labels) as included in `id`, 247 | set as tag values, and output by this module individually. 248 | Does not affect values of tags passed in via the `tags` input. 249 | Possible values: `lower`, `title`, `upper` and `none` (no transformation). 250 | Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs. 251 | Default value: `lower`. 252 | EOT 253 | 254 | validation { 255 | condition = var.label_value_case == null ? true : contains(["lower", "title", "upper", "none"], var.label_value_case) 256 | error_message = "Allowed values: `lower`, `title`, `upper`, `none`." 257 | } 258 | } 259 | 260 | variable "descriptor_formats" { 261 | type = any 262 | default = {} 263 | description = <<-EOT 264 | Describe additional descriptors to be output in the `descriptors` output map. 265 | Map of maps. Keys are names of descriptors. Values are maps of the form 266 | `{ 267 | format = string 268 | labels = list(string) 269 | }` 270 | (Type is `any` so the map values can later be enhanced to provide additional options.) 271 | `format` is a Terraform format string to be passed to the `format()` function. 272 | `labels` is a list of labels, in order, to pass to `format()` function. 273 | Label values will be normalized before being passed to `format()` so they will be 274 | identical to how they appear in `id`. 275 | Default is `{}` (`descriptors` output will be empty). 276 | EOT 277 | } 278 | 279 | #### End of copy of cloudposse/terraform-null-label/variables.tf 280 | -------------------------------------------------------------------------------- /examples/complete/context.tf: -------------------------------------------------------------------------------- 1 | # 2 | # ONLY EDIT THIS FILE IN github.com/cloudposse/terraform-null-label 3 | # All other instances of this file should be a copy of that one 4 | # 5 | # 6 | # Copy this file from https://github.com/cloudposse/terraform-null-label/blob/master/exports/context.tf 7 | # and then place it in your Terraform module to automatically get 8 | # Cloud Posse's standard configuration inputs suitable for passing 9 | # to Cloud Posse modules. 10 | # 11 | # curl -sL https://raw.githubusercontent.com/cloudposse/terraform-null-label/master/exports/context.tf -o context.tf 12 | # 13 | # Modules should access the whole context as `module.this.context` 14 | # to get the input variables with nulls for defaults, 15 | # for example `context = module.this.context`, 16 | # and access individual variables as `module.this.`, 17 | # with final values filled in. 18 | # 19 | # For example, when using defaults, `module.this.context.delimiter` 20 | # will be null, and `module.this.delimiter` will be `-` (hyphen). 21 | # 22 | 23 | module "this" { 24 | source = "cloudposse/label/null" 25 | version = "0.25.0" # requires Terraform >= 0.13.0 26 | 27 | enabled = var.enabled 28 | namespace = var.namespace 29 | tenant = var.tenant 30 | environment = var.environment 31 | stage = var.stage 32 | name = var.name 33 | delimiter = var.delimiter 34 | attributes = var.attributes 35 | tags = var.tags 36 | additional_tag_map = var.additional_tag_map 37 | label_order = var.label_order 38 | regex_replace_chars = var.regex_replace_chars 39 | id_length_limit = var.id_length_limit 40 | label_key_case = var.label_key_case 41 | label_value_case = var.label_value_case 42 | descriptor_formats = var.descriptor_formats 43 | labels_as_tags = var.labels_as_tags 44 | 45 | context = var.context 46 | } 47 | 48 | # Copy contents of cloudposse/terraform-null-label/variables.tf here 49 | 50 | variable "context" { 51 | type = any 52 | default = { 53 | enabled = true 54 | namespace = null 55 | tenant = null 56 | environment = null 57 | stage = null 58 | name = null 59 | delimiter = null 60 | attributes = [] 61 | tags = {} 62 | additional_tag_map = {} 63 | regex_replace_chars = null 64 | label_order = [] 65 | id_length_limit = null 66 | label_key_case = null 67 | label_value_case = null 68 | descriptor_formats = {} 69 | # Note: we have to use [] instead of null for unset lists due to 70 | # https://github.com/hashicorp/terraform/issues/28137 71 | # which was not fixed until Terraform 1.0.0, 72 | # but we want the default to be all the labels in `label_order` 73 | # and we want users to be able to prevent all tag generation 74 | # by setting `labels_as_tags` to `[]`, so we need 75 | # a different sentinel to indicate "default" 76 | labels_as_tags = ["unset"] 77 | } 78 | description = <<-EOT 79 | Single object for setting entire context at once. 80 | See description of individual variables for details. 81 | Leave string and numeric variables as `null` to use default value. 82 | Individual variable settings (non-null) override settings in context object, 83 | except for attributes, tags, and additional_tag_map, which are merged. 84 | EOT 85 | 86 | validation { 87 | condition = lookup(var.context, "label_key_case", null) == null ? true : contains(["lower", "title", "upper"], var.context["label_key_case"]) 88 | error_message = "Allowed values: `lower`, `title`, `upper`." 89 | } 90 | 91 | validation { 92 | condition = lookup(var.context, "label_value_case", null) == null ? true : contains(["lower", "title", "upper", "none"], var.context["label_value_case"]) 93 | error_message = "Allowed values: `lower`, `title`, `upper`, `none`." 94 | } 95 | } 96 | 97 | variable "enabled" { 98 | type = bool 99 | default = null 100 | description = "Set to false to prevent the module from creating any resources" 101 | } 102 | 103 | variable "namespace" { 104 | type = string 105 | default = null 106 | description = "ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique" 107 | } 108 | 109 | variable "tenant" { 110 | type = string 111 | default = null 112 | description = "ID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is for" 113 | } 114 | 115 | variable "environment" { 116 | type = string 117 | default = null 118 | description = "ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'" 119 | } 120 | 121 | variable "stage" { 122 | type = string 123 | default = null 124 | description = "ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'" 125 | } 126 | 127 | variable "name" { 128 | type = string 129 | default = null 130 | description = <<-EOT 131 | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'. 132 | This is the only ID element not also included as a `tag`. 133 | The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input. 134 | EOT 135 | } 136 | 137 | variable "delimiter" { 138 | type = string 139 | default = null 140 | description = <<-EOT 141 | Delimiter to be used between ID elements. 142 | Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. 143 | EOT 144 | } 145 | 146 | variable "attributes" { 147 | type = list(string) 148 | default = [] 149 | description = <<-EOT 150 | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`, 151 | in the order they appear in the list. New attributes are appended to the 152 | end of the list. The elements of the list are joined by the `delimiter` 153 | and treated as a single ID element. 154 | EOT 155 | } 156 | 157 | variable "labels_as_tags" { 158 | type = set(string) 159 | default = ["default"] 160 | description = <<-EOT 161 | Set of labels (ID elements) to include as tags in the `tags` output. 162 | Default is to include all labels. 163 | Tags with empty values will not be included in the `tags` output. 164 | Set to `[]` to suppress all generated tags. 165 | **Notes:** 166 | The value of the `name` tag, if included, will be the `id`, not the `name`. 167 | Unlike other `null-label` inputs, the initial setting of `labels_as_tags` cannot be 168 | changed in later chained modules. Attempts to change it will be silently ignored. 169 | EOT 170 | } 171 | 172 | variable "tags" { 173 | type = map(string) 174 | default = {} 175 | description = <<-EOT 176 | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`). 177 | Neither the tag keys nor the tag values will be modified by this module. 178 | EOT 179 | } 180 | 181 | variable "additional_tag_map" { 182 | type = map(string) 183 | default = {} 184 | description = <<-EOT 185 | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`. 186 | This is for some rare cases where resources want additional configuration of tags 187 | and therefore take a list of maps with tag key, value, and additional configuration. 188 | EOT 189 | } 190 | 191 | variable "label_order" { 192 | type = list(string) 193 | default = null 194 | description = <<-EOT 195 | The order in which the labels (ID elements) appear in the `id`. 196 | Defaults to ["namespace", "environment", "stage", "name", "attributes"]. 197 | You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. 198 | EOT 199 | } 200 | 201 | variable "regex_replace_chars" { 202 | type = string 203 | default = null 204 | description = <<-EOT 205 | Terraform regular expression (regex) string. 206 | Characters matching the regex will be removed from the ID elements. 207 | If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. 208 | EOT 209 | } 210 | 211 | variable "id_length_limit" { 212 | type = number 213 | default = null 214 | description = <<-EOT 215 | Limit `id` to this many characters (minimum 6). 216 | Set to `0` for unlimited length. 217 | Set to `null` for keep the existing setting, which defaults to `0`. 218 | Does not affect `id_full`. 219 | EOT 220 | validation { 221 | condition = var.id_length_limit == null ? true : var.id_length_limit >= 6 || var.id_length_limit == 0 222 | error_message = "The id_length_limit must be >= 6 if supplied (not null), or 0 for unlimited length." 223 | } 224 | } 225 | 226 | variable "label_key_case" { 227 | type = string 228 | default = null 229 | description = <<-EOT 230 | Controls the letter case of the `tags` keys (label names) for tags generated by this module. 231 | Does not affect keys of tags passed in via the `tags` input. 232 | Possible values: `lower`, `title`, `upper`. 233 | Default value: `title`. 234 | EOT 235 | 236 | validation { 237 | condition = var.label_key_case == null ? true : contains(["lower", "title", "upper"], var.label_key_case) 238 | error_message = "Allowed values: `lower`, `title`, `upper`." 239 | } 240 | } 241 | 242 | variable "label_value_case" { 243 | type = string 244 | default = null 245 | description = <<-EOT 246 | Controls the letter case of ID elements (labels) as included in `id`, 247 | set as tag values, and output by this module individually. 248 | Does not affect values of tags passed in via the `tags` input. 249 | Possible values: `lower`, `title`, `upper` and `none` (no transformation). 250 | Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs. 251 | Default value: `lower`. 252 | EOT 253 | 254 | validation { 255 | condition = var.label_value_case == null ? true : contains(["lower", "title", "upper", "none"], var.label_value_case) 256 | error_message = "Allowed values: `lower`, `title`, `upper`, `none`." 257 | } 258 | } 259 | 260 | variable "descriptor_formats" { 261 | type = any 262 | default = {} 263 | description = <<-EOT 264 | Describe additional descriptors to be output in the `descriptors` output map. 265 | Map of maps. Keys are names of descriptors. Values are maps of the form 266 | `{ 267 | format = string 268 | labels = list(string) 269 | }` 270 | (Type is `any` so the map values can later be enhanced to provide additional options.) 271 | `format` is a Terraform format string to be passed to the `format()` function. 272 | `labels` is a list of labels, in order, to pass to `format()` function. 273 | Label values will be normalized before being passed to `format()` so they will be 274 | identical to how they appear in `id`. 275 | Default is `{}` (`descriptors` output will be empty). 276 | EOT 277 | } 278 | 279 | #### End of copy of cloudposse/terraform-null-label/variables.tf 280 | -------------------------------------------------------------------------------- /examples/docker-image/context.tf: -------------------------------------------------------------------------------- 1 | # 2 | # ONLY EDIT THIS FILE IN github.com/cloudposse/terraform-null-label 3 | # All other instances of this file should be a copy of that one 4 | # 5 | # 6 | # Copy this file from https://github.com/cloudposse/terraform-null-label/blob/master/exports/context.tf 7 | # and then place it in your Terraform module to automatically get 8 | # Cloud Posse's standard configuration inputs suitable for passing 9 | # to Cloud Posse modules. 10 | # 11 | # curl -sL https://raw.githubusercontent.com/cloudposse/terraform-null-label/master/exports/context.tf -o context.tf 12 | # 13 | # Modules should access the whole context as `module.this.context` 14 | # to get the input variables with nulls for defaults, 15 | # for example `context = module.this.context`, 16 | # and access individual variables as `module.this.`, 17 | # with final values filled in. 18 | # 19 | # For example, when using defaults, `module.this.context.delimiter` 20 | # will be null, and `module.this.delimiter` will be `-` (hyphen). 21 | # 22 | 23 | module "this" { 24 | source = "cloudposse/label/null" 25 | version = "0.25.0" # requires Terraform >= 0.13.0 26 | 27 | enabled = var.enabled 28 | namespace = var.namespace 29 | tenant = var.tenant 30 | environment = var.environment 31 | stage = var.stage 32 | name = var.name 33 | delimiter = var.delimiter 34 | attributes = var.attributes 35 | tags = var.tags 36 | additional_tag_map = var.additional_tag_map 37 | label_order = var.label_order 38 | regex_replace_chars = var.regex_replace_chars 39 | id_length_limit = var.id_length_limit 40 | label_key_case = var.label_key_case 41 | label_value_case = var.label_value_case 42 | descriptor_formats = var.descriptor_formats 43 | labels_as_tags = var.labels_as_tags 44 | 45 | context = var.context 46 | } 47 | 48 | # Copy contents of cloudposse/terraform-null-label/variables.tf here 49 | 50 | variable "context" { 51 | type = any 52 | default = { 53 | enabled = true 54 | namespace = null 55 | tenant = null 56 | environment = null 57 | stage = null 58 | name = null 59 | delimiter = null 60 | attributes = [] 61 | tags = {} 62 | additional_tag_map = {} 63 | regex_replace_chars = null 64 | label_order = [] 65 | id_length_limit = null 66 | label_key_case = null 67 | label_value_case = null 68 | descriptor_formats = {} 69 | # Note: we have to use [] instead of null for unset lists due to 70 | # https://github.com/hashicorp/terraform/issues/28137 71 | # which was not fixed until Terraform 1.0.0, 72 | # but we want the default to be all the labels in `label_order` 73 | # and we want users to be able to prevent all tag generation 74 | # by setting `labels_as_tags` to `[]`, so we need 75 | # a different sentinel to indicate "default" 76 | labels_as_tags = ["unset"] 77 | } 78 | description = <<-EOT 79 | Single object for setting entire context at once. 80 | See description of individual variables for details. 81 | Leave string and numeric variables as `null` to use default value. 82 | Individual variable settings (non-null) override settings in context object, 83 | except for attributes, tags, and additional_tag_map, which are merged. 84 | EOT 85 | 86 | validation { 87 | condition = lookup(var.context, "label_key_case", null) == null ? true : contains(["lower", "title", "upper"], var.context["label_key_case"]) 88 | error_message = "Allowed values: `lower`, `title`, `upper`." 89 | } 90 | 91 | validation { 92 | condition = lookup(var.context, "label_value_case", null) == null ? true : contains(["lower", "title", "upper", "none"], var.context["label_value_case"]) 93 | error_message = "Allowed values: `lower`, `title`, `upper`, `none`." 94 | } 95 | } 96 | 97 | variable "enabled" { 98 | type = bool 99 | default = null 100 | description = "Set to false to prevent the module from creating any resources" 101 | } 102 | 103 | variable "namespace" { 104 | type = string 105 | default = null 106 | description = "ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique" 107 | } 108 | 109 | variable "tenant" { 110 | type = string 111 | default = null 112 | description = "ID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is for" 113 | } 114 | 115 | variable "environment" { 116 | type = string 117 | default = null 118 | description = "ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'" 119 | } 120 | 121 | variable "stage" { 122 | type = string 123 | default = null 124 | description = "ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'" 125 | } 126 | 127 | variable "name" { 128 | type = string 129 | default = null 130 | description = <<-EOT 131 | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'. 132 | This is the only ID element not also included as a `tag`. 133 | The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input. 134 | EOT 135 | } 136 | 137 | variable "delimiter" { 138 | type = string 139 | default = null 140 | description = <<-EOT 141 | Delimiter to be used between ID elements. 142 | Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. 143 | EOT 144 | } 145 | 146 | variable "attributes" { 147 | type = list(string) 148 | default = [] 149 | description = <<-EOT 150 | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`, 151 | in the order they appear in the list. New attributes are appended to the 152 | end of the list. The elements of the list are joined by the `delimiter` 153 | and treated as a single ID element. 154 | EOT 155 | } 156 | 157 | variable "labels_as_tags" { 158 | type = set(string) 159 | default = ["default"] 160 | description = <<-EOT 161 | Set of labels (ID elements) to include as tags in the `tags` output. 162 | Default is to include all labels. 163 | Tags with empty values will not be included in the `tags` output. 164 | Set to `[]` to suppress all generated tags. 165 | **Notes:** 166 | The value of the `name` tag, if included, will be the `id`, not the `name`. 167 | Unlike other `null-label` inputs, the initial setting of `labels_as_tags` cannot be 168 | changed in later chained modules. Attempts to change it will be silently ignored. 169 | EOT 170 | } 171 | 172 | variable "tags" { 173 | type = map(string) 174 | default = {} 175 | description = <<-EOT 176 | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`). 177 | Neither the tag keys nor the tag values will be modified by this module. 178 | EOT 179 | } 180 | 181 | variable "additional_tag_map" { 182 | type = map(string) 183 | default = {} 184 | description = <<-EOT 185 | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`. 186 | This is for some rare cases where resources want additional configuration of tags 187 | and therefore take a list of maps with tag key, value, and additional configuration. 188 | EOT 189 | } 190 | 191 | variable "label_order" { 192 | type = list(string) 193 | default = null 194 | description = <<-EOT 195 | The order in which the labels (ID elements) appear in the `id`. 196 | Defaults to ["namespace", "environment", "stage", "name", "attributes"]. 197 | You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. 198 | EOT 199 | } 200 | 201 | variable "regex_replace_chars" { 202 | type = string 203 | default = null 204 | description = <<-EOT 205 | Terraform regular expression (regex) string. 206 | Characters matching the regex will be removed from the ID elements. 207 | If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. 208 | EOT 209 | } 210 | 211 | variable "id_length_limit" { 212 | type = number 213 | default = null 214 | description = <<-EOT 215 | Limit `id` to this many characters (minimum 6). 216 | Set to `0` for unlimited length. 217 | Set to `null` for keep the existing setting, which defaults to `0`. 218 | Does not affect `id_full`. 219 | EOT 220 | validation { 221 | condition = var.id_length_limit == null ? true : var.id_length_limit >= 6 || var.id_length_limit == 0 222 | error_message = "The id_length_limit must be >= 6 if supplied (not null), or 0 for unlimited length." 223 | } 224 | } 225 | 226 | variable "label_key_case" { 227 | type = string 228 | default = null 229 | description = <<-EOT 230 | Controls the letter case of the `tags` keys (label names) for tags generated by this module. 231 | Does not affect keys of tags passed in via the `tags` input. 232 | Possible values: `lower`, `title`, `upper`. 233 | Default value: `title`. 234 | EOT 235 | 236 | validation { 237 | condition = var.label_key_case == null ? true : contains(["lower", "title", "upper"], var.label_key_case) 238 | error_message = "Allowed values: `lower`, `title`, `upper`." 239 | } 240 | } 241 | 242 | variable "label_value_case" { 243 | type = string 244 | default = null 245 | description = <<-EOT 246 | Controls the letter case of ID elements (labels) as included in `id`, 247 | set as tag values, and output by this module individually. 248 | Does not affect values of tags passed in via the `tags` input. 249 | Possible values: `lower`, `title`, `upper` and `none` (no transformation). 250 | Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs. 251 | Default value: `lower`. 252 | EOT 253 | 254 | validation { 255 | condition = var.label_value_case == null ? true : contains(["lower", "title", "upper", "none"], var.label_value_case) 256 | error_message = "Allowed values: `lower`, `title`, `upper`, `none`." 257 | } 258 | } 259 | 260 | variable "descriptor_formats" { 261 | type = any 262 | default = {} 263 | description = <<-EOT 264 | Describe additional descriptors to be output in the `descriptors` output map. 265 | Map of maps. Keys are names of descriptors. Values are maps of the form 266 | `{ 267 | format = string 268 | labels = list(string) 269 | }` 270 | (Type is `any` so the map values can later be enhanced to provide additional options.) 271 | `format` is a Terraform format string to be passed to the `format()` function. 272 | `labels` is a list of labels, in order, to pass to `format()` function. 273 | Label values will be normalized before being passed to `format()` so they will be 274 | identical to how they appear in `id`. 275 | Default is `{}` (`descriptors` output will be empty). 276 | EOT 277 | } 278 | 279 | #### End of copy of cloudposse/terraform-null-label/variables.tf 280 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2021-2022 Cloud Posse, LLC 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Project Banner
5 | 6 | 7 |

Latest ReleaseLast UpdatedSlack CommunityGet Support 8 | 9 |

10 | 11 | 12 | 32 | 33 | This module deploys an AWS Lambda function from a Zip file or from a Docker image. Additionally, it creates an IAM 34 | role for the Lambda function, which optionally attaches policies to allow for CloudWatch Logs, Cloudwatch Insights, 35 | VPC Access and X-Ray tracing. 36 | 37 | 38 | > [!TIP] 39 | > #### 👽 Use Atmos with Terraform 40 | > Cloud Posse uses [`atmos`](https://atmos.tools) to easily orchestrate multiple environments using Terraform.
41 | > Works with [Github Actions](https://atmos.tools/integrations/github-actions/), [Atlantis](https://atmos.tools/integrations/atlantis), or [Spacelift](https://atmos.tools/integrations/spacelift). 42 | > 43 | >
44 | > Watch demo of using Atmos with Terraform 45 | >
46 | > Example of running atmos to manage infrastructure from our Quick Start tutorial. 47 | > 48 | 49 | 50 | 51 | 52 | 53 | ## Usage 54 | 55 | For a complete example, see [examples/complete](examples/complete). 56 | For automated tests of the complete example using [bats](https://github.com/bats-core/bats-core) and [Terratest](https://github.com/gruntwork-io/terratest) 57 | (which tests and deploys the example on AWS), see [test](test). 58 | 59 | ```hcl 60 | module "lambda" { 61 | source = "cloudposse/lambda-function/aws" 62 | version = "xxxx" 63 | 64 | filename = "lambda.zip" 65 | function_name = "my-function" 66 | handler = "handler.handler" 67 | runtime = "nodejs14.x" 68 | } 69 | ``` 70 | 71 | > [!IMPORTANT] 72 | > In Cloud Posse's examples, we avoid pinning modules to specific versions to prevent discrepancies between the documentation 73 | > and the latest released versions. However, for your own projects, we strongly advise pinning each module to the exact version 74 | > you're using. This practice ensures the stability of your infrastructure. Additionally, we recommend implementing a systematic 75 | > approach for updating versions to avoid unexpected changes. 76 | 77 | 78 | 79 | 80 | 81 | ## Examples 82 | 83 | - [`examples/complete`](https://github.com/cloudposse/terraform-aws-lambda-function/blob/main/examples/complete) - complete example of using this module 84 | - [`examples/docker-image`](https://github.com/cloudposse/terraform-aws-lambda-function/blob/main/examples/docker-image) - example of using Lambda with Docker images 85 | 86 | 87 | 88 | 89 | 90 | ## Requirements 91 | 92 | | Name | Version | 93 | |------|---------| 94 | | [terraform](#requirement\_terraform) | >= 0.14 | 95 | | [aws](#requirement\_aws) | >= 3.0 | 96 | 97 | ## Providers 98 | 99 | | Name | Version | 100 | |------|---------| 101 | | [aws](#provider\_aws) | >= 3.0 | 102 | 103 | ## Modules 104 | 105 | | Name | Source | Version | 106 | |------|--------|---------| 107 | | [cloudwatch\_log\_group](#module\_cloudwatch\_log\_group) | cloudposse/cloudwatch-logs/aws | 0.6.6 | 108 | | [this](#module\_this) | cloudposse/label/null | 0.25.0 | 109 | 110 | ## Resources 111 | 112 | | Name | Type | 113 | |------|------| 114 | | [aws_iam_policy.ssm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource | 115 | | [aws_iam_role.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource | 116 | | [aws_iam_role_policy.inline](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource | 117 | | [aws_iam_role_policy_attachment.cloudwatch_insights](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource | 118 | | [aws_iam_role_policy_attachment.cloudwatch_logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource | 119 | | [aws_iam_role_policy_attachment.custom](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource | 120 | | [aws_iam_role_policy_attachment.ssm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource | 121 | | [aws_iam_role_policy_attachment.vpc_access](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource | 122 | | [aws_iam_role_policy_attachment.xray](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource | 123 | | [aws_lambda_function.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function) | resource | 124 | | [aws_lambda_permission.invoke_function](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) | resource | 125 | | [aws_caller_identity.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source | 126 | | [aws_iam_policy_document.assume_role_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | 127 | | [aws_iam_policy_document.ssm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | 128 | | [aws_partition.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source | 129 | | [aws_region.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source | 130 | 131 | ## Inputs 132 | 133 | | Name | Description | Type | Default | Required | 134 | |------|-------------|------|---------|:--------:| 135 | | [additional\_tag\_map](#input\_additional\_tag\_map) | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.
This is for some rare cases where resources want additional configuration of tags
and therefore take a list of maps with tag key, value, and additional configuration. | `map(string)` | `{}` | no | 136 | | [architectures](#input\_architectures) | Instruction set architecture for your Lambda function. Valid values are ["x86\_64"] and ["arm64"].
Default is ["x86\_64"]. Removing this attribute, function's architecture stay the same. | `list(string)` | `null` | no | 137 | | [attributes](#input\_attributes) | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,
in the order they appear in the list. New attributes are appended to the
end of the list. The elements of the list are joined by the `delimiter`
and treated as a single ID element. | `list(string)` | `[]` | no | 138 | | [cloudwatch\_lambda\_insights\_enabled](#input\_cloudwatch\_lambda\_insights\_enabled) | Enable CloudWatch Lambda Insights for the Lambda Function. | `bool` | `false` | no | 139 | | [cloudwatch\_logs\_kms\_key\_arn](#input\_cloudwatch\_logs\_kms\_key\_arn) | The ARN of the KMS Key to use when encrypting log data. | `string` | `null` | no | 140 | | [cloudwatch\_logs\_retention\_in\_days](#input\_cloudwatch\_logs\_retention\_in\_days) | Specifies the number of days you want to retain log events in the specified log group. Possible values are:
1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, 3653, and 0. If you select 0, the events in the
log group are always retained and never expire. | `number` | `null` | no | 141 | | [context](#input\_context) | Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as `null` to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional\_tag\_map, which are merged. | `any` |
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"descriptor_formats": {},
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"labels_as_tags": [
"unset"
],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {},
"tenant": null
}
| no | 142 | | [custom\_iam\_policy\_arns](#input\_custom\_iam\_policy\_arns) | ARNs of custom policies to be attached to the lambda role | `set(string)` | `[]` | no | 143 | | [dead\_letter\_config\_target\_arn](#input\_dead\_letter\_config\_target\_arn) | ARN of an SNS topic or SQS queue to notify when an invocation fails. If this option is used, the function's IAM role
must be granted suitable access to write to the target object, which means allowing either the sns:Publish or
sqs:SendMessage action on this ARN, depending on which service is targeted." | `string` | `null` | no | 144 | | [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.
Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no | 145 | | [description](#input\_description) | Description of what the Lambda Function does. | `string` | `null` | no | 146 | | [descriptor\_formats](#input\_descriptor\_formats) | Describe additional descriptors to be output in the `descriptors` output map.
Map of maps. Keys are names of descriptors. Values are maps of the form
`{
format = string
labels = list(string)
}`
(Type is `any` so the map values can later be enhanced to provide additional options.)
`format` is a Terraform format string to be passed to the `format()` function.
`labels` is a list of labels, in order, to pass to `format()` function.
Label values will be normalized before being passed to `format()` so they will be
identical to how they appear in `id`.
Default is `{}` (`descriptors` output will be empty). | `any` | `{}` | no | 147 | | [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no | 148 | | [environment](#input\_environment) | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no | 149 | | [ephemeral\_storage\_size](#input\_ephemeral\_storage\_size) | The size of the Lambda function Ephemeral storage (/tmp) represented in MB.
The minimum supported ephemeral\_storage value defaults to 512MB and the maximum supported value is 10240MB. | `number` | `null` | no | 150 | | [filename](#input\_filename) | The path to the function's deployment package within the local filesystem. If defined, The s3\_-prefixed options and image\_uri cannot be used. | `string` | `null` | no | 151 | | [function\_name](#input\_function\_name) | Unique name for the Lambda Function. | `string` | n/a | yes | 152 | | [handler](#input\_handler) | The function entrypoint in your code. | `string` | `null` | no | 153 | | [iam\_policy\_description](#input\_iam\_policy\_description) | Description of the IAM policy for the Lambda IAM role | `string` | `"Provides minimum SSM read permissions."` | no | 154 | | [id\_length\_limit](#input\_id\_length\_limit) | Limit `id` to this many characters (minimum 6).
Set to `0` for unlimited length.
Set to `null` for keep the existing setting, which defaults to `0`.
Does not affect `id_full`. | `number` | `null` | no | 155 | | [image\_config](#input\_image\_config) | The Lambda OCI [image configurations](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function#image_config)
block with three (optional) arguments:
- *entry\_point* - The ENTRYPOINT for the docker image (type `list(string)`).
- *command* - The CMD for the docker image (type `list(string)`).
- *working\_directory* - The working directory for the docker image (type `string`). | `any` | `{}` | no | 156 | | [image\_uri](#input\_image\_uri) | The ECR image URI containing the function's deployment package. Conflicts with filename, s3\_bucket, s3\_key, and s3\_object\_version. | `string` | `null` | no | 157 | | [inline\_iam\_policy](#input\_inline\_iam\_policy) | Inline policy document (JSON) to attach to the lambda role | `string` | `null` | no | 158 | | [invoke\_function\_permissions](#input\_invoke\_function\_permissions) | Defines which external source(s) can invoke this function (action 'lambda:InvokeFunction'). Attributes map to those of https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission. NOTE: to keep things simple, we only expose a subset of said attributes. If a more complex configuration is needed, declare the necessary lambda permissions outside of this module |
list(object({
principal = string
source_arn = string
}))
| `[]` | no | 159 | | [kms\_key\_arn](#input\_kms\_key\_arn) | Amazon Resource Name (ARN) of the AWS Key Management Service (KMS) key that is used to encrypt environment variables.
If this configuration is not provided when environment variables are in use, AWS Lambda uses a default service key.
If this configuration is provided when environment variables are not in use, the AWS Lambda API does not save this
configuration and Terraform will show a perpetual difference of adding the key. To fix the perpetual difference,
remove this configuration. | `string` | `""` | no | 160 | | [label\_key\_case](#input\_label\_key\_case) | Controls the letter case of the `tags` keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper`.
Default value: `title`. | `string` | `null` | no | 161 | | [label\_order](#input\_label\_order) | The order in which the labels (ID elements) appear in the `id`.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. | `list(string)` | `null` | no | 162 | | [label\_value\_case](#input\_label\_value\_case) | Controls the letter case of ID elements (labels) as included in `id`,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper` and `none` (no transformation).
Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.
Default value: `lower`. | `string` | `null` | no | 163 | | [labels\_as\_tags](#input\_labels\_as\_tags) | Set of labels (ID elements) to include as tags in the `tags` output.
Default is to include all labels.
Tags with empty values will not be included in the `tags` output.
Set to `[]` to suppress all generated tags.
**Notes:**
The value of the `name` tag, if included, will be the `id`, not the `name`.
Unlike other `null-label` inputs, the initial setting of `labels_as_tags` cannot be
changed in later chained modules. Attempts to change it will be silently ignored. | `set(string)` |
[
"default"
]
| no | 164 | | [lambda\_at\_edge\_enabled](#input\_lambda\_at\_edge\_enabled) | Enable Lambda@Edge for your Node.js or Python functions. The required trust relationship and publishing of function versions will be configured in this module. | `bool` | `false` | no | 165 | | [lambda\_environment](#input\_lambda\_environment) | Environment (e.g. env variables) configuration for the Lambda function enable you to dynamically pass settings to your function code and libraries. |
object({
variables = map(string)
})
| `null` | no | 166 | | [layers](#input\_layers) | List of Lambda Layer Version ARNs (maximum of 5) to attach to the Lambda Function. | `list(string)` | `[]` | no | 167 | | [memory\_size](#input\_memory\_size) | Amount of memory in MB the Lambda Function can use at runtime. | `number` | `128` | no | 168 | | [name](#input\_name) | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as a `tag`.
The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input. | `string` | `null` | no | 169 | | [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no | 170 | | [package\_type](#input\_package\_type) | The Lambda deployment package type. Valid values are Zip and Image. | `string` | `"Zip"` | no | 171 | | [permissions\_boundary](#input\_permissions\_boundary) | ARN of the policy that is used to set the permissions boundary for the role | `string` | `""` | no | 172 | | [publish](#input\_publish) | Whether to publish creation/change as new Lambda Function Version. | `bool` | `false` | no | 173 | | [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no | 174 | | [reserved\_concurrent\_executions](#input\_reserved\_concurrent\_executions) | The amount of reserved concurrent executions for this lambda function. A value of 0 disables lambda from being triggered and -1 removes any concurrency limitations. | `number` | `-1` | no | 175 | | [role\_name](#input\_role\_name) | The rolename used for the Lambda Function. If not provided, a default role name will be used. | `string` | `null` | no | 176 | | [runtime](#input\_runtime) | The runtime environment for the Lambda function you are uploading. | `string` | `null` | no | 177 | | [s3\_bucket](#input\_s3\_bucket) | The S3 bucket location containing the function's deployment package. Conflicts with filename and image\_uri.
This bucket must reside in the same AWS region where you are creating the Lambda function. | `string` | `null` | no | 178 | | [s3\_key](#input\_s3\_key) | The S3 key of an object containing the function's deployment package. Conflicts with filename and image\_uri. | `string` | `null` | no | 179 | | [s3\_object\_version](#input\_s3\_object\_version) | The object version containing the function's deployment package. Conflicts with filename and image\_uri. | `string` | `null` | no | 180 | | [source\_code\_hash](#input\_source\_code\_hash) | Used to trigger updates. Must be set to a base64-encoded SHA256 hash of the package file specified with either
filename or s3\_key. The usual way to set this is filebase64sha256('file.zip') where 'file.zip' is the local filename
of the lambda function source archive. | `string` | `""` | no | 181 | | [ssm\_parameter\_names](#input\_ssm\_parameter\_names) | List of AWS Systems Manager Parameter Store parameter names. The IAM role of this Lambda function will be enhanced
with read permissions for those parameters. Parameters must start with a forward slash and can be encrypted with the
default KMS key. | `list(string)` | `null` | no | 182 | | [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no | 183 | | [tags](#input\_tags) | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).
Neither the tag keys nor the tag values will be modified by this module. | `map(string)` | `{}` | no | 184 | | [tenant](#input\_tenant) | ID element \_(Rarely used, not included by default)\_. A customer identifier, indicating who this instance of a resource is for | `string` | `null` | no | 185 | | [timeout](#input\_timeout) | The amount of time the Lambda Function has to run in seconds. | `number` | `3` | no | 186 | | [tracing\_config\_mode](#input\_tracing\_config\_mode) | Tracing config mode of the Lambda function. Can be either PassThrough or Active. | `string` | `null` | no | 187 | | [vpc\_config](#input\_vpc\_config) | Provide this to allow your function to access your VPC (if both 'subnet\_ids' and 'security\_group\_ids' are empty then
vpc\_config is considered to be empty or unset, see https://docs.aws.amazon.com/lambda/latest/dg/vpc.html for details). |
object({
security_group_ids = list(string)
subnet_ids = list(string)
})
| `null` | no | 188 | 189 | ## Outputs 190 | 191 | | Name | Description | 192 | |------|-------------| 193 | | [arn](#output\_arn) | ARN of the lambda function | 194 | | [cloudwatch\_log\_group\_name](#output\_cloudwatch\_log\_group\_name) | Name of Cloudwatch log group | 195 | | [function\_name](#output\_function\_name) | Lambda function name | 196 | | [invoke\_arn](#output\_invoke\_arn) | Invoke ARN of the lambda function | 197 | | [qualified\_arn](#output\_qualified\_arn) | ARN identifying your Lambda Function Version (if versioning is enabled via publish = true) | 198 | | [role\_arn](#output\_role\_arn) | Lambda IAM role ARN | 199 | | [role\_name](#output\_role\_name) | Lambda IAM role name | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | ## Related Projects 209 | 210 | Check out these related projects. 211 | 212 | - [terraform-null-label](https://github.com/cloudposse/terraform-null-label) - Terraform module designed to generate consistent names and tags for resources. Use terraform-null-label to implement a strict naming convention. 213 | 214 | 215 | ## References 216 | 217 | For additional context, refer to some of these links. 218 | 219 | - [Cloud Posse Documentation](https://docs.cloudposse.com) - The Cloud Posse Developer Hub (documentation) 220 | - [Terraform Standard Module Structure](https://www.terraform.io/docs/language/modules/develop/structure.html) - HashiCorp's standard module structure is a file and directory layout we recommend for reusable modules distributed in separate repositories. 221 | - [Terraform Module Requirements](https://www.terraform.io/docs/registry/modules/publish.html#requirements) - HashiCorp's guidance on all the requirements for publishing a module. Meeting the requirements for publishing a module is extremely easy. 222 | - [Terraform Version Pinning](https://www.terraform.io/docs/language/settings/index.html#specifying-a-required-terraform-version) - The required_version setting can be used to constrain which versions of the Terraform CLI can be used with your configuration 223 | 224 | 225 | 226 | > [!TIP] 227 | > #### Use Terraform Reference Architectures for AWS 228 | > 229 | > Use Cloud Posse's ready-to-go [terraform architecture blueprints](https://cloudposse.com/reference-architecture/) for AWS to get up and running quickly. 230 | > 231 | > ✅ We build it together with your team.
232 | > ✅ Your team owns everything.
233 | > ✅ 100% Open Source and backed by fanatical support.
234 | > 235 | > Request Quote 236 | >
📚 Learn More 237 | > 238 | >
239 | > 240 | > Cloud Posse is the leading [**DevOps Accelerator**](https://cpco.io/commercial-support?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-lambda-function&utm_content=commercial_support) for funded startups and enterprises. 241 | > 242 | > *Your team can operate like a pro today.* 243 | > 244 | > Ensure that your team succeeds by using Cloud Posse's proven process and turnkey blueprints. Plus, we stick around until you succeed. 245 | > #### Day-0: Your Foundation for Success 246 | > - **Reference Architecture.** You'll get everything you need from the ground up built using 100% infrastructure as code. 247 | > - **Deployment Strategy.** Adopt a proven deployment strategy with GitHub Actions, enabling automated, repeatable, and reliable software releases. 248 | > - **Site Reliability Engineering.** Gain total visibility into your applications and services with Datadog, ensuring high availability and performance. 249 | > - **Security Baseline.** Establish a secure environment from the start, with built-in governance, accountability, and comprehensive audit logs, safeguarding your operations. 250 | > - **GitOps.** Empower your team to manage infrastructure changes confidently and efficiently through Pull Requests, leveraging the full power of GitHub Actions. 251 | > 252 | > Request Quote 253 | > 254 | > #### Day-2: Your Operational Mastery 255 | > - **Training.** Equip your team with the knowledge and skills to confidently manage the infrastructure, ensuring long-term success and self-sufficiency. 256 | > - **Support.** Benefit from a seamless communication over Slack with our experts, ensuring you have the support you need, whenever you need it. 257 | > - **Troubleshooting.** Access expert assistance to quickly resolve any operational challenges, minimizing downtime and maintaining business continuity. 258 | > - **Code Reviews.** Enhance your team’s code quality with our expert feedback, fostering continuous improvement and collaboration. 259 | > - **Bug Fixes.** Rely on our team to troubleshoot and resolve any issues, ensuring your systems run smoothly. 260 | > - **Migration Assistance.** Accelerate your migration process with our dedicated support, minimizing disruption and speeding up time-to-value. 261 | > - **Customer Workshops.** Engage with our team in weekly workshops, gaining insights and strategies to continuously improve and innovate. 262 | > 263 | > Request Quote 264 | > 265 |
266 | 267 | ## ✨ Contributing 268 | 269 | This project is under active development, and we encourage contributions from our community. 270 | 271 | 272 | 273 | Many thanks to our outstanding contributors: 274 | 275 | 276 | 277 | 278 | 279 | For 🐛 bug reports & feature requests, please use the [issue tracker](https://github.com/cloudposse/terraform-aws-lambda-function/issues). 280 | 281 | In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow. 282 | 1. Review our [Code of Conduct](https://github.com/cloudposse/terraform-aws-lambda-function/?tab=coc-ov-file#code-of-conduct) and [Contributor Guidelines](https://github.com/cloudposse/.github/blob/main/CONTRIBUTING.md). 283 | 2. **Fork** the repo on GitHub 284 | 3. **Clone** the project to your own machine 285 | 4. **Commit** changes to your own branch 286 | 5. **Push** your work back up to your fork 287 | 6. Submit a **Pull Request** so that we can review your changes 288 | 289 | **NOTE:** Be sure to merge the latest changes from "upstream" before making a pull request! 290 | 291 | 292 | ## Running Terraform Tests 293 | 294 | We use [Atmos](https://atmos.tools) to streamline how Terraform tests are run. It centralizes configuration and wraps common test workflows with easy-to-use commands. 295 | 296 | All tests are located in the [`test/`](test) folder. 297 | 298 | Under the hood, tests are powered by Terratest together with our internal [Test Helpers](https://github.com/cloudposse/test-helpers) library, providing robust infrastructure validation. 299 | 300 | Setup dependencies: 301 | - Install Atmos ([installation guide](https://atmos.tools/install/)) 302 | - Install Go [1.24+ or newer](https://go.dev/doc/install) 303 | - Install Terraform or OpenTofu 304 | 305 | To run tests: 306 | 307 | - Run all tests: 308 | ```sh 309 | atmos test run 310 | ``` 311 | - Clean up test artifacts: 312 | ```sh 313 | atmos test clean 314 | ``` 315 | - Explore additional test options: 316 | ```sh 317 | atmos test --help 318 | ``` 319 | The configuration for test commands is centrally managed. To review what's being imported, see the [`atmos.yaml`](https://raw.githubusercontent.com/cloudposse/.github/refs/heads/main/.github/atmos/terraform-module.yaml) file. 320 | 321 | Learn more about our [automated testing in our documentation](https://docs.cloudposse.com/community/contribute/automated-testing/) or implementing [custom commands](https://atmos.tools/core-concepts/custom-commands/) with atmos. 322 | 323 | ### 🌎 Slack Community 324 | 325 | Join our [Open Source Community](https://cpco.io/slack?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-lambda-function&utm_content=slack) on Slack. It's **FREE** for everyone! Our "SweetOps" community is where you get to talk with others who share a similar vision for how to rollout and manage infrastructure. This is the best place to talk shop, ask questions, solicit feedback, and work together as a community to build totally *sweet* infrastructure. 326 | 327 | ### 📰 Newsletter 328 | 329 | Sign up for [our newsletter](https://cpco.io/newsletter?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-lambda-function&utm_content=newsletter) and join 3,000+ DevOps engineers, CTOs, and founders who get insider access to the latest DevOps trends, so you can always stay in the know. 330 | Dropped straight into your Inbox every week — and usually a 5-minute read. 331 | 332 | ### 📆 Office Hours 333 | 334 | [Join us every Wednesday via Zoom](https://cloudposse.com/office-hours?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-lambda-function&utm_content=office_hours) for your weekly dose of insider DevOps trends, AWS news and Terraform insights, all sourced from our SweetOps community, plus a _live Q&A_ that you can’t find anywhere else. 335 | It's **FREE** for everyone! 336 | ## License 337 | 338 | License 339 | 340 |
341 | Preamble to the Apache License, Version 2.0 342 |
343 |
344 | 345 | Complete license is available in the [`LICENSE`](LICENSE) file. 346 | 347 | ```text 348 | Licensed to the Apache Software Foundation (ASF) under one 349 | or more contributor license agreements. See the NOTICE file 350 | distributed with this work for additional information 351 | regarding copyright ownership. The ASF licenses this file 352 | to you under the Apache License, Version 2.0 (the 353 | "License"); you may not use this file except in compliance 354 | with the License. You may obtain a copy of the License at 355 | 356 | https://www.apache.org/licenses/LICENSE-2.0 357 | 358 | Unless required by applicable law or agreed to in writing, 359 | software distributed under the License is distributed on an 360 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 361 | KIND, either express or implied. See the License for the 362 | specific language governing permissions and limitations 363 | under the License. 364 | ``` 365 |
366 | 367 | ## Trademarks 368 | 369 | All other trademarks referenced herein are the property of their respective owners. 370 | 371 | 372 | ## Copyrights 373 | 374 | Copyright © 2022-2025 [Cloud Posse, LLC](https://cloudposse.com) 375 | 376 | 377 | 378 | README footer 379 | 380 | Beacon 381 | --------------------------------------------------------------------------------