Skip to content

Commit

Permalink
feat: Added wrapper module (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpdenny authored Nov 8, 2022
1 parent ec81ef5 commit 1b3915a
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,4 @@ jobs:
with:
terraform-version: ${{ steps.minMax.outputs.maxVersion }}
terraform-docs-version: ${{ env.TERRAFORM_DOCS_VERSION }}
install-hcledit: true
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ repos:
rev: v1.76.0
hooks:
- id: terraform_fmt
- id: terraform_wrapper_module_for_each
- id: terraform_validate
- id: terraform_docs
args:
Expand Down
100 changes: 100 additions & 0 deletions wrappers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# 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/sqs/aws//wrappers"
# Alternative source:
# source = "git::git@github.com:terraform-aws-modules/terraform-aws-sqs.git//wrappers?ref=master"
}
inputs = {
defaults = { # Default values
create = 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/sqs/aws//wrappers"
defaults = { # Default values
create = 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 S3 buckets in one Terragrunt layer

`eu-west-1/s3-buckets/terragrunt.hcl`:

```hcl
terraform {
source = "tfr:///terraform-aws-modules/s3-bucket/aws//wrappers"
# Alternative source:
# source = "git::git@github.com:terraform-aws-modules/terraform-aws-s3-bucket.git//wrappers?ref=master"
}
inputs = {
defaults = {
force_destroy = true
attach_elb_log_delivery_policy = true
attach_lb_log_delivery_policy = true
attach_deny_insecure_transport_policy = true
attach_require_latest_tls_policy = true
}
items = {
bucket1 = {
bucket = "my-random-bucket-1"
}
bucket2 = {
bucket = "my-random-bucket-2"
tags = {
Secure = "probably"
}
}
}
}
```
25 changes: 25 additions & 0 deletions wrappers/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module "wrapper" {
source = "../"

for_each = var.items

create = try(each.value.create, var.defaults.create, true)
name = try(each.value.name, var.defaults.name, null)
name_prefix = try(each.value.name_prefix, var.defaults.name_prefix, null)
visibility_timeout_seconds = try(each.value.visibility_timeout_seconds, var.defaults.visibility_timeout_seconds, 30)
message_retention_seconds = try(each.value.message_retention_seconds, var.defaults.message_retention_seconds, 345600)
max_message_size = try(each.value.max_message_size, var.defaults.max_message_size, 262144)
delay_seconds = try(each.value.delay_seconds, var.defaults.delay_seconds, 0)
receive_wait_time_seconds = try(each.value.receive_wait_time_seconds, var.defaults.receive_wait_time_seconds, 0)
policy = try(each.value.policy, var.defaults.policy, "")
redrive_policy = try(each.value.redrive_policy, var.defaults.redrive_policy, "")
redrive_allow_policy = try(each.value.redrive_allow_policy, var.defaults.redrive_allow_policy, "")
fifo_queue = try(each.value.fifo_queue, var.defaults.fifo_queue, false)
content_based_deduplication = try(each.value.content_based_deduplication, var.defaults.content_based_deduplication, false)
kms_master_key_id = try(each.value.kms_master_key_id, var.defaults.kms_master_key_id, null)
sqs_managed_sse_enabled = try(each.value.sqs_managed_sse_enabled, var.defaults.sqs_managed_sse_enabled, false)
kms_data_key_reuse_period_seconds = try(each.value.kms_data_key_reuse_period_seconds, var.defaults.kms_data_key_reuse_period_seconds, 300)
deduplication_scope = try(each.value.deduplication_scope, var.defaults.deduplication_scope, null)
fifo_throughput_limit = try(each.value.fifo_throughput_limit, var.defaults.fifo_throughput_limit, null)
tags = try(each.value.tags, var.defaults.tags, {})
}
5 changes: 5 additions & 0 deletions wrappers/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/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/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
required_version = ">= 0.13.1"
}

0 comments on commit 1b3915a

Please sign in to comment.