Skip to content

Commit

Permalink
feat: Add module wrappers (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusmazzoni authored Jul 5, 2023
1 parent 0236678 commit b5e9346
Show file tree
Hide file tree
Showing 20 changed files with 736 additions and 0 deletions.
106 changes: 106 additions & 0 deletions wrappers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Wrapper for the root module

The configuration in this directory contains an implementation of a single module wrapper pattern, which allows managing several copies of a module in places where using the native Terraform 0.13+ `for_each` feature is not feasible (e.g., with Terragrunt).

You may want to use a single Terragrunt configuration file to manage multiple resources without duplicating `terragrunt.hcl` files for each copy of the same module.

This wrapper does not implement any extra functionality.

## Usage with Terragrunt

`terragrunt.hcl`:

```hcl
terraform {
source = "tfr:///terraform-aws-modules/lambda/aws//wrappers"
# Alternative source:
# source = "git::[email protected]:terraform-aws-modules/terraform-aws-lambda.git//wrappers?ref=master"
}
inputs = {
defaults = { # Default values
create = true
handler = "index.lambda_handler"
runtime = "python3.8"
tags = {
Terraform = "true"
Environment = "dev"
}
}
items = {
my-item = {
# omitted... can be any argument supported by the module
}
my-second-item = {
# omitted... can be any argument supported by the module
}
# omitted...
}
}
```

## Usage with Terraform

```hcl
module "wrapper" {
source = "terraform-aws-modules/lambda/aws//wrappers"
defaults = { # Default values
create = true
handler = "index.lambda_handler"
runtime = "python3.8"
tags = {
Terraform = "true"
Environment = "dev"
}
}
items = {
my-item = {
# omitted... can be any argument supported by the module
}
my-second-item = {
# omitted... can be any argument supported by the module
}
# omitted...
}
}
```

## Example: Manage multiple Lambdas in one Terragrunt layer

`eu-west-1/lambdas/terragrunt.hcl`:

```hcl
terraform {
source = "tfr:///terraform-aws-modules/lambda/aws//wrappers"
# Alternative source:
# source = "git::[email protected]:terraform-aws-modules/terraform-aws-lambda.git//wrappers?ref=master"
}
inputs = {
defaults = {
create = true
handler = "index.lambda_handler"
runtime = "python3.8"
tags = {
Terraform = "true"
Environment = "dev"
}
}
items = {
lambda1 = {
function_name = "my-lambda1"
description = "My awesome lambda function"\
source_path = "../src/lambda-function1"
}
lambda2 = {
function_name = "my-lambda2"
description = "My second awesome lambda function"
source_path = "../src/lambda-function2"
}
}
}
```
103 changes: 103 additions & 0 deletions wrappers/alias/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Wrapper for module: `modules/alias`

The configuration in this directory contains an implementation of a single module wrapper pattern, which allows managing several copies of a module in places where using the native Terraform 0.13+ `for_each` feature is not feasible (e.g., with Terragrunt).

You may want to use a single Terragrunt configuration file to manage multiple resources without duplicating `terragrunt.hcl` files for each copy of the same module.

This wrapper does not implement any extra functionality.

## Usage with Terragrunt

`terragrunt.hcl`:

```hcl
terraform {
source = "tfr:///terraform-aws-modules/lambda/aws"
# Alternative source:
# source = "git::[email protected]:terraform-aws-modules/terraform-aws-lambda.git//wrappers/alias?ref=master"
}
inputs = {
defaults = { # Default values
create = true
refresh_alias = true
}
items = {
my-item = {
# omitted... can be any argument supported by the module
}
my-second-item = {
# omitted... can be any argument supported by the module
}
# omitted...
}
}
```

## Usage with Terraform

```hcl
module "wrapper" {
source = "terraform-aws-modules/lambda/aws//wrappers/alias"
defaults = { # Default values
create = true
refresh_alias = true
}
items = {
my-item = {
# omitted... can be any argument supported by the module
}
my-second-item = {
# omitted... can be any argument supported by the module
}
# omitted...
}
}
```

## Example: Manage multiple aliases in one Terragrunt layer

`eu-west-1/lambda-aliases/terragrunt.hcl`:

```hcl
terraform {
source = "tfr:///terraform-aws-modules/lambda/aws//wrappers/alias"
# Alternative source:
# source = "git::[email protected]:terraform-aws-modules/terraform-aws-lambda.git//wrappers/alias?ref=master"
}
dependency "lambda1" {
config_path = "../lambdas/lambda1"
}
dependency "lambda2" {
config_path = "../lambdas/lambda2"
}
inputs = {
defaults = {
refresh_alias = true
allowed_triggers = {
AnotherAPIGatewayAny = {
service = "apigateway"
source_arn = "arn:aws:execute-api:eu-west-1:135367859851:abcdedfgse/*/*/*"
}
}
}
items = {
alias1 = {
name = "my-random-alias-1"
function_name = dependency.lambda1.outputs.lambda_function_name
function_version = dependency.lambda1.outputs.lambda_function_version
}
alias2 = {
name = "my-random-alias-2"
function_name = dependency.lambda2.outputs.lambda_function_name
function_version = dependency.lambda2.outputs.lambda_function_version
}
}
}
```
26 changes: 26 additions & 0 deletions wrappers/alias/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module "wrapper" {
source = "../../modules/alias"

for_each = var.items

create = try(each.value.create, var.defaults.create, true)
use_existing_alias = try(each.value.use_existing_alias, var.defaults.use_existing_alias, false)
refresh_alias = try(each.value.refresh_alias, var.defaults.refresh_alias, true)
create_async_event_config = try(each.value.create_async_event_config, var.defaults.create_async_event_config, false)
create_version_async_event_config = try(each.value.create_version_async_event_config, var.defaults.create_version_async_event_config, true)
create_qualified_alias_async_event_config = try(each.value.create_qualified_alias_async_event_config, var.defaults.create_qualified_alias_async_event_config, true)
create_version_allowed_triggers = try(each.value.create_version_allowed_triggers, var.defaults.create_version_allowed_triggers, true)
create_qualified_alias_allowed_triggers = try(each.value.create_qualified_alias_allowed_triggers, var.defaults.create_qualified_alias_allowed_triggers, true)
name = try(each.value.name, var.defaults.name, null)
description = try(each.value.description, var.defaults.description, null)
function_name = try(each.value.function_name, var.defaults.function_name, null)
function_version = try(each.value.function_version, var.defaults.function_version, null)
routing_additional_version_weights = try(each.value.routing_additional_version_weights, var.defaults.routing_additional_version_weights, {})
maximum_event_age_in_seconds = try(each.value.maximum_event_age_in_seconds, var.defaults.maximum_event_age_in_seconds, null)
maximum_retry_attempts = try(each.value.maximum_retry_attempts, var.defaults.maximum_retry_attempts, null)
destination_on_failure = try(each.value.destination_on_failure, var.defaults.destination_on_failure, null)
destination_on_success = try(each.value.destination_on_success, var.defaults.destination_on_success, null)
allowed_triggers = try(each.value.allowed_triggers, var.defaults.allowed_triggers, {})
event_source_mapping = try(each.value.event_source_mapping, var.defaults.event_source_mapping, {})

}
5 changes: 5 additions & 0 deletions wrappers/alias/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
output "wrapper" {
description = "Map of outputs of a wrapper."
value = module.wrapper
# sensitive = false # No sensitive module output found
}
11 changes: 11 additions & 0 deletions wrappers/alias/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
variable "defaults" {
description = "Map of default values which will be used for each item."
type = any
default = {}
}

variable "items" {
description = "Maps of items to create a wrapper from. Values are passed through to the module."
type = any
default = {}
}
3 changes: 3 additions & 0 deletions wrappers/alias/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
required_version = ">= 0.13"
}
134 changes: 134 additions & 0 deletions wrappers/deploy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# Wrapper for module: `modules/deploy`

The configuration in this directory contains an implementation of a single module wrapper pattern, which allows managing several copies of a module in places where using the native Terraform 0.13+ `for_each` feature is not feasible (e.g., with Terragrunt).

You may want to use a single Terragrunt configuration file to manage multiple resources without duplicating `terragrunt.hcl` files for each copy of the same module.

This wrapper does not implement any extra functionality.

## Usage with Terragrunt

`terragrunt.hcl`:

```hcl
terraform {
source = "tfr:///terraform-aws-modules/lambda/aws//wrappers/deploy"
# Alternative source:
# source = "git::[email protected]:terraform-aws-modules/terraform-aws-lambda.git//wrappers/deploy?ref=master"
}
inputs = {
defaults = { # Default values
create_app = true
tags = {
Terraform = "true"
Environment = "dev"
}
}
items = {
my-item = {
# omitted... can be any argument supported by the module
}
my-second-item = {
# omitted... can be any argument supported by the module
}
# omitted...
}
}
```

## Usage with Terraform

```hcl
module "wrapper" {
source = "terraform-aws-modules/lambda/aws//wrappers/deploy"
defaults = { # Default values
create_app = true
tags = {
Terraform = "true"
Environment = "dev"
}
}
items = {
my-item = {
# omitted... can be any argument supported by the module
}
my-second-item = {
# omitted... can be any argument supported by the module
}
# omitted...
}
}
```

## Example: Manage multiple deployment via AWS CodeDeploy in one Terragrunt layer

`eu-west-1/lambda-deploys/terragrunt.hcl`:

```hcl
terraform {
source = "tfr:///terraform-aws-modules/lambda/aws//wrappers/deploy"
# Alternative source:
# source = "git::[email protected]:terraform-aws-modules/terraform-aws-lambda.git//wrappers/deploy?ref=master"
}
dependency "aliases" {
config_path = "../lambdas-aliases/"
}
dependency "lambda1" {
config_path = "../lambdas/lambda1"
}
dependency "lambda2" {
config_path = "../lambdas/lambda2"
}
inputs = {
defaults = {
create_app = true
reate_deployment_group = true
create_deployment = true
run_deployment = true
wait_deployment_completion = true
triggers = {
start = {
events = ["DeploymentStart"]
name = "DeploymentStart"
target_arn = "arn:aws:sns:eu-west-1:135367859851:sns1"
}
success = {
events = ["DeploymentSuccess"]
name = "DeploymentSuccess"
target_arn = "arn:aws:sns:eu-west-1:135367859851:sns2"
}
}
tags = {
Terraform = "true"
Environment = "dev"
}
}
items = {
deploy1 = {
app_name = "my-random-app-1"
deployment_group_name = "something1"
alias_name = dependency.aliases.outputs.wrapper.alias1.lambda_alias_name
function_name = dependency.lambda1.outputs.lambda_function_name
target_version = dependency.lambda1.outputs.lambda_function_version
}
deploy2 = {
app_name = "my-random-app-2"
deployment_group_name = "something2"
alias_name = dependency.aliases.outputs.wrapper.alias2.lambda_alias_name
function_name = dependency.lambda2.outputs.lambda_function_name
target_version = dependency.lambda2.outputs.lambda_function_version
}
}
}
```
Loading

0 comments on commit b5e9346

Please sign in to comment.