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..b38d917 --- /dev/null +++ b/examples/cloudwatch-trigger/main.tf @@ -0,0 +1,46 @@ +// ================================================================= +// +// 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 = "../../" + + source_directory = "./script_src/" + + 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 + + schedule_expression = "rate(1 minute)" +} 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..5bc761c --- /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 TestTerraformCloudwatchTrigger(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-cw-trigger-%s", strings.ToLower(random.UniqueId())) + + terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ + TerraformDir: "../examples/cloudwatch-trigger", + Vars: map[string]interface{}{ + "test_name": testName, + "tags": map[string]interface{}{ + "Automation": "Terraform", + "Terratest": "yes", + "Test": "TestTerraformCloudwatchTrigger", + }, + }, + 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\"") + +}