From 2d43ac1bdc86d0200303e0d76f47a8e2a2d98b82 Mon Sep 17 00:00:00 2001 From: Austin Lasseter Date: Tue, 22 Jun 2021 18:47:33 -0400 Subject: [PATCH 1/6] copied initial files as templates from simple terratest --- examples/cloudwatch-trigger/README.md | 40 ++++++++++++ examples/cloudwatch-trigger/handler.py | 2 + examples/cloudwatch-trigger/main.tf | 42 ++++++++++++ examples/cloudwatch-trigger/outputs.tf | 19 ++++++ examples/cloudwatch-trigger/variables.tf | 18 ++++++ test/terraform_aws_lambda_cloudwatch_test.go | 67 ++++++++++++++++++++ 6 files changed, 188 insertions(+) create mode 100644 examples/cloudwatch-trigger/README.md create mode 100644 examples/cloudwatch-trigger/handler.py create mode 100644 examples/cloudwatch-trigger/main.tf create mode 100644 examples/cloudwatch-trigger/outputs.tf create mode 100644 examples/cloudwatch-trigger/variables.tf create mode 100644 test/terraform_aws_lambda_cloudwatch_test.go diff --git a/examples/cloudwatch-trigger/README.md b/examples/cloudwatch-trigger/README.md new file mode 100644 index 0000000..8852e7e --- /dev/null +++ b/examples/cloudwatch-trigger/README.md @@ -0,0 +1,40 @@ + +## Requirements + +No requirements. + +## Providers + +| Name | Version | +|------|---------| +| [archive](#provider\_archive) | n/a | +| [aws](#provider\_aws) | n/a | + +## Modules + +| Name | Source | Version | +|------|--------|---------| +| [lambda\_function](#module\_lambda\_function) | ../../ | n/a | + +## Resources + +| Name | Type | +|------|------| +| [archive_file.lambda_simple_zip_inline](https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file) | data source | +| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source | + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| [tags](#input\_tags) | n/a | `map(string)` | n/a | yes | +| [test\_name](#input\_test\_name) | n/a | `string` | n/a | yes | + +## Outputs + +| Name | Description | +|------|-------------| +| [lambda\_function\_name](#output\_lambda\_function\_name) | n/a | +| [tags](#output\_tags) | n/a | +| [test\_name](#output\_test\_name) | n/a | + diff --git a/examples/cloudwatch-trigger/handler.py b/examples/cloudwatch-trigger/handler.py new file mode 100644 index 0000000..750459d --- /dev/null +++ b/examples/cloudwatch-trigger/handler.py @@ -0,0 +1,2 @@ +def lambda_handler(event, context): + return "hello world" diff --git a/examples/cloudwatch-trigger/main.tf b/examples/cloudwatch-trigger/main.tf new file mode 100644 index 0000000..da4b960 --- /dev/null +++ b/examples/cloudwatch-trigger/main.tf @@ -0,0 +1,42 @@ +// ================================================================= +// +// Work of the U.S. Department of Defense, Defense Digital Service. +// Released as open source under the MIT License. See LICENSE file. +// +// ================================================================= + + +data "aws_region" "current" {} + +data "archive_file" "lambda_simple_zip_inline" { + type = "zip" + source_file = "${path.module}/handler.py" + output_path = "../../temp/lambda/simple.zip" +} + +module "lambda_function" { + source = "../../" + + execution_role_name = format( + "test-func-lambda-execution-role-%s", + var.test_name + ) + + function_name = format( + "test-func-%s-%s", + var.test_name, + data.aws_region.current.name + ) + + function_description = "Function description." + + filename = data.archive_file.lambda_simple_zip_inline.output_path + + handler = "handler.lambda_handler" + + runtime = "python3.8" + + environment_variables = { Automation = "Terraform" } + + tags = var.tags +} diff --git a/examples/cloudwatch-trigger/outputs.tf b/examples/cloudwatch-trigger/outputs.tf new file mode 100644 index 0000000..cbf7c26 --- /dev/null +++ b/examples/cloudwatch-trigger/outputs.tf @@ -0,0 +1,19 @@ +// ================================================================= +// +// Work of the U.S. Department of Defense, Defense Digital Service. +// Released as open source under the MIT License. See LICENSE file. +// +// ================================================================= + + +output "tags" { + value = var.tags +} + +output "test_name" { + value = var.test_name +} + +output "lambda_function_name" { + value = module.lambda_function.lambda_function_name +} diff --git a/examples/cloudwatch-trigger/variables.tf b/examples/cloudwatch-trigger/variables.tf new file mode 100644 index 0000000..f1479b1 --- /dev/null +++ b/examples/cloudwatch-trigger/variables.tf @@ -0,0 +1,18 @@ +// ================================================================= +// +// Work of the U.S. Department of Defense, Defense Digital Service. +// Released as open source under the MIT License. See LICENSE file. +// +// ================================================================= + +// Do not provide default values for the variables in the examples. +// The variables will be set in the go tests + + +variable "tags" { + type = map(string) +} + +variable "test_name" { + type = string +} diff --git a/test/terraform_aws_lambda_cloudwatch_test.go b/test/terraform_aws_lambda_cloudwatch_test.go new file mode 100644 index 0000000..715c568 --- /dev/null +++ b/test/terraform_aws_lambda_cloudwatch_test.go @@ -0,0 +1,67 @@ +// ================================================================= +// +// Work of the U.S. Department of Defense, Defense Digital Service. +// Released as open source under the MIT License. See LICENSE file. +// +// ================================================================= + +package test + +import ( + "fmt" + "os" + "strings" + "testing" + + "github.com/aws/aws-sdk-go/service/lambda" + "github.com/stretchr/testify/require" + "github.com/gruntwork-io/terratest/modules/random" + "github.com/gruntwork-io/terratest/modules/terraform" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" +) + +func TestTerraformSimpleExample(t *testing.T) { + t.Parallel() + + region := os.Getenv("AWS_DEFAULT_REGION") + require.NotEmpty(t, region, "missing environment variable AWS_DEFAULT_REGION") + + testName := fmt.Sprintf("tt-lf-simple-%s", strings.ToLower(random.UniqueId())) + + terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ + TerraformDir: "../examples/simple", + Vars: map[string]interface{}{ + "test_name": testName, + "tags": map[string]interface{}{ + "Automation": "Terraform", + "Terratest": "yes", + "Test": "TestTerraformSimpleExample", + }, + }, + EnvVars: map[string]string{ + "AWS_DEFAULT_REGION": region, + }, + }) + + if os.Getenv("TT_SKIP_DESTROY") != "1" { + defer terraform.Destroy(t, terraformOptions) + } + + terraform.InitAndApply(t, terraformOptions) + + lambdaFunctionName := terraform.Output(t, terraformOptions, "lambda_function_name") + s := session.Must(session.NewSession()) + + c := lambda.New(s, aws.NewConfig().WithRegion(region)) + + invokeOutput, invokeError := c.Invoke(&lambda.InvokeInput{ + FunctionName: aws.String(lambdaFunctionName), + Payload: []byte("{}"), + }) + + require.NoError(t, invokeError) + payload := string(invokeOutput.Payload) + require.Equal(t, payload, "\"hello world\"") + +} From 4c2327fa2078c52654ffd8ad5f9784674d175885 Mon Sep 17 00:00:00 2001 From: Austin Lasseter Date: Tue, 22 Jun 2021 18:56:11 -0400 Subject: [PATCH 2/6] updated test.go --- test/terraform_aws_lambda_cloudwatch_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/terraform_aws_lambda_cloudwatch_test.go b/test/terraform_aws_lambda_cloudwatch_test.go index 715c568..50650bd 100644 --- a/test/terraform_aws_lambda_cloudwatch_test.go +++ b/test/terraform_aws_lambda_cloudwatch_test.go @@ -27,16 +27,16 @@ func TestTerraformSimpleExample(t *testing.T) { region := os.Getenv("AWS_DEFAULT_REGION") require.NotEmpty(t, region, "missing environment variable AWS_DEFAULT_REGION") - testName := fmt.Sprintf("tt-lf-simple-%s", strings.ToLower(random.UniqueId())) + testName := fmt.Sprintf("tt-lf-cw-trigger-%s", strings.ToLower(random.UniqueId())) terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ - TerraformDir: "../examples/simple", + TerraformDir: "../examples/cloudwatch-trigger", Vars: map[string]interface{}{ "test_name": testName, "tags": map[string]interface{}{ "Automation": "Terraform", "Terratest": "yes", - "Test": "TestTerraformSimpleExample", + "Test": "TestTerraformCloudwatchTrigger", }, }, EnvVars: map[string]string{ From 7077a7b36a9eeed2c5b5a926dca2e7a4cc6255c7 Mon Sep 17 00:00:00 2001 From: Austin Lasseter Date: Tue, 22 Jun 2021 19:08:06 -0400 Subject: [PATCH 3/6] brought over all the recent changes from dod-iac/terraform-module-template --- .circleci/config.yml | 7 ++++--- .editorconfig | 34 ++++++++++++++++++++++++++++++++++ .pre-commit-config.yaml | 2 +- LICENSE | 3 ++- Makefile | 15 +++++++++------ scripts/format-terraform | 2 +- scripts/lint-go | 21 ++++++++++++++++++--- scripts/terratest | 2 +- scripts/update-docs | 2 +- 9 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 .editorconfig diff --git a/.circleci/config.yml b/.circleci/config.yml index 83e2157..ee51f93 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -36,16 +36,17 @@ jobs: cd ~ mkdir -p ~/bin curl -sSLO https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz - [ $(sha256sum go${GO_VERSION}.linux-amd64.tar.gz | cut -f1 -d' ') = 7154e88f5a8047aad4b80ebace58a059e36e7e2e4eb3b383127a28c711b4ff59 ] + [ $(sha256sum go${GO_VERSION}.linux-amd64.tar.gz | cut -f1 -d' ') = "${GO_CHECKSUM}" ] tar -xzvf go${GO_VERSION}.linux-amd64.tar.gz sudo mv go /usr/local echo 'export PATH=$PATH:/usr/local/go/bin' >> $BASH_ENV environment: - GO_VERSION: "1.16.4" - GO_CHECKSUM: "7154e88f5a8047aad4b80ebace58a059e36e7e2e4eb3b383127a28c711b4ff59" + GO_VERSION: "1.16.5" + GO_CHECKSUM: "b12c23023b68de22f74c0524f10b753e7b08b1504cb7e417eccebdd3fae49061" - run: echo 'export PATH=~/bin:$PATH' >> $BASH_ENV - run: make update_docs - run: make format_terraform + - run: make imports - run: make tidy - run: make lint_go - run: git diff --exit-code diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..3503477 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,34 @@ +# EditorConfig helps developers define and maintain consistent +# coding styles between different editors and IDEs +# editorconfig.org + +root = true + +[*] +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true +indent_style = tab +indent_size = 4 +tab_width = 4 + +[*.sh] +indent_style = space +indent_size = 4 + +[*.go] +indent_style = tab +indent_size = 4 + +[*.tf] +indent_style = space +indent_size = 2 + +[*.yml] +indent_style = space +indent_size = 2 + +[*.yaml] +indent_style = space +indent_size = 2 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2ddd620..bbc7537 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,7 +1,7 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v3.4.0 + rev: v4.0.1 hooks: - id: check-json - id: check-merge-conflict diff --git a/LICENSE b/LICENSE index d103a39..82a9d5a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,7 @@ The MIT License (MIT) -Copyright (c) 2021 U.S. Department of Defense, Defense Digital Service +Copyright (c) 2021 U.S. Department of Defense, Defense Digital Service (in +countries where recognized) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/Makefile b/Makefile index fa4c030..8187c2d 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ fmt: ## Format Go source code .PHONY: imports imports: bin/goimports ## Update imports in Go source code - bin/goimports -w -local github.com/dod-iac,github.com/dod-iac/terraform-aws-lambda-function $$(find . -iname '*.go') + bin/goimports -w -local github.com/dod-iac $$(find . -iname '*.go') .PHONY: test_go lint_go: bin/errcheck bin/ineffassign bin/staticcheck bin/shadow ## Run Go tests @@ -54,24 +54,27 @@ terratest: ## Run terratest tests # Command line Programs # -bin/errcheck: +bin/errcheck: ## Make go binary errcheck go build -o bin/errcheck github.com/kisielk/errcheck -bin/goimports: +bin/goimports: ## Make go binary goimports go build -o bin/goimports golang.org/x/tools/cmd/goimports bin/gox: go build -o bin/gox github.com/mitchellh/gox -bin/ineffassign: +bin/ineffassign: ## Make go binary ineffassign go build -o bin/ineffassign github.com/gordonklaus/ineffassign -bin/staticcheck: +bin/staticcheck: ## Make go binary staticcheck go build -o bin/staticcheck honnef.co/go/tools/cmd/staticcheck -bin/shadow: +bin/shadow: ## Make go binary shadow go build -o bin/shadow golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow +.PHONY: tools ## Install all binary tools +tools: bin/errcheck bin/goimports bin/ineffassign bin/staticcheck bin/shadow + ## Clean clean: ## Clean artifacts diff --git a/scripts/format-terraform b/scripts/format-terraform index fa8b005..2a7f937 100755 --- a/scripts/format-terraform +++ b/scripts/format-terraform @@ -1,4 +1,4 @@ -#!/bin/bash +#! /usr/bin/env bash # ================================================================= # diff --git a/scripts/lint-go b/scripts/lint-go index 473d9b4..d95f301 100755 --- a/scripts/lint-go +++ b/scripts/lint-go @@ -1,4 +1,4 @@ -#!/bin/bash +#! /usr/bin/env bash # ================================================================= # @@ -11,16 +11,31 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" set -eu # move up a directory -cd $DIR/.. +cd "${DIR}"/.. pkgs=$(go list ./... | grep -v /vendor/ | tr "\n" " ") +check_command () { + cmd=$1 + + if ! command -v "bin/${cmd}" > /dev/null; then + echo "Missing ${cmd}, please install into bin/ directory with 'make bin/${cmd}'" + exit 1 + fi +} + +# Validate that the commands exist +check_command errcheck +check_command ineffassign +check_command shadow +check_command staticcheck + echo "******************" echo "Running go vet" go vet $pkgs echo "******************" echo "Running go vet with shadow" -go vet -vettool="bin/shadow" $pkgs +go vet -vettool="${DIR}/../bin/shadow" $pkgs echo "******************" echo "Running errcheck" bin/errcheck ${pkgs} diff --git a/scripts/terratest b/scripts/terratest index 23c629e..973fd74 100755 --- a/scripts/terratest +++ b/scripts/terratest @@ -1,4 +1,4 @@ -#!/bin/bash +#! /usr/bin/env bash # ================================================================= # diff --git a/scripts/update-docs b/scripts/update-docs index e8b185f..30418f3 100755 --- a/scripts/update-docs +++ b/scripts/update-docs @@ -1,4 +1,4 @@ -#!/bin/bash +#! /usr/bin/env bash # ================================================================= # From baf80b9f12959099779041835bf8c0c4166f21b1 Mon Sep 17 00:00:00 2001 From: Austin Lasseter Date: Tue, 22 Jun 2021 19:14:46 -0400 Subject: [PATCH 4/6] fixed bug with name TestTerraformCloudwatchTrigger --- test/terraform_aws_lambda_cloudwatch_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/terraform_aws_lambda_cloudwatch_test.go b/test/terraform_aws_lambda_cloudwatch_test.go index 50650bd..5bc761c 100644 --- a/test/terraform_aws_lambda_cloudwatch_test.go +++ b/test/terraform_aws_lambda_cloudwatch_test.go @@ -21,7 +21,7 @@ import ( "github.com/aws/aws-sdk-go/aws/session" ) -func TestTerraformSimpleExample(t *testing.T) { +func TestTerraformCloudwatchTrigger(t *testing.T) { t.Parallel() region := os.Getenv("AWS_DEFAULT_REGION") From 56b8791f0da78833e17b386403c3e0146770916d Mon Sep 17 00:00:00 2001 From: Austin Lasseter Date: Wed, 23 Jun 2021 11:11:19 -0400 Subject: [PATCH 5/6] initial attempt at updating main.tf --- examples/cloudwatch-trigger/main.tf | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/examples/cloudwatch-trigger/main.tf b/examples/cloudwatch-trigger/main.tf index da4b960..b38d917 100644 --- a/examples/cloudwatch-trigger/main.tf +++ b/examples/cloudwatch-trigger/main.tf @@ -17,6 +17,8 @@ data "archive_file" "lambda_simple_zip_inline" { module "lambda_function" { source = "../../" + source_directory = "./script_src/" + execution_role_name = format( "test-func-lambda-execution-role-%s", var.test_name @@ -39,4 +41,6 @@ module "lambda_function" { environment_variables = { Automation = "Terraform" } tags = var.tags + + schedule_expression = "rate(1 minute)" } From 95b3e21bbbd696a0f27c8c5e475fb2f0cc70085f Mon Sep 17 00:00:00 2001 From: Austin Lasseter Date: Wed, 23 Jun 2021 16:17:17 -0400 Subject: [PATCH 6/6] updated Makefile to match Master branch --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 8187c2d..d71c35c 100644 --- a/Makefile +++ b/Makefile @@ -69,7 +69,7 @@ bin/ineffassign: ## Make go binary ineffassign bin/staticcheck: ## Make go binary staticcheck go build -o bin/staticcheck honnef.co/go/tools/cmd/staticcheck -bin/shadow: ## Make go binary shadow +bin/shadow: ## Make go binary shadow go build -o bin/shadow golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow .PHONY: tools ## Install all binary tools