Skip to content
This repository was archived by the owner on May 2, 2025. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions examples/cloudwatch-trigger/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

No requirements.

## Providers

| Name | Version |
|------|---------|
| <a name="provider_archive"></a> [archive](#provider\_archive) | n/a |
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a |

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_lambda_function"></a> [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 |
|------|-------------|------|---------|:--------:|
| <a name="input_tags"></a> [tags](#input\_tags) | n/a | `map(string)` | n/a | yes |
| <a name="input_test_name"></a> [test\_name](#input\_test\_name) | n/a | `string` | n/a | yes |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_lambda_function_name"></a> [lambda\_function\_name](#output\_lambda\_function\_name) | n/a |
| <a name="output_tags"></a> [tags](#output\_tags) | n/a |
| <a name="output_test_name"></a> [test\_name](#output\_test\_name) | n/a |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
2 changes: 2 additions & 0 deletions examples/cloudwatch-trigger/handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def lambda_handler(event, context):
return "hello world"
46 changes: 46 additions & 0 deletions examples/cloudwatch-trigger/main.tf
Original file line number Diff line number Diff line change
@@ -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)"
}
19 changes: 19 additions & 0 deletions examples/cloudwatch-trigger/outputs.tf
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 18 additions & 0 deletions examples/cloudwatch-trigger/variables.tf
Original file line number Diff line number Diff line change
@@ -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
}
67 changes: 67 additions & 0 deletions test/terraform_aws_lambda_cloudwatch_test.go
Original file line number Diff line number Diff line change
@@ -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\"")

}