Skip to content

Commit

Permalink
feat: Wrapper for terragrunt (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
glavk authored Jan 19, 2024
1 parent fcca2fc commit 5c7ce58
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 0 deletions.
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.86.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/efs/aws//wrappers"
# Alternative source:
# source = "git::[email protected]:terraform-aws-modules/terraform-aws-efs.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/efs/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::[email protected]: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"
}
}
}
}
```
35 changes: 35 additions & 0 deletions wrappers/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module "wrapper" {
source = "../"

for_each = var.items

access_points = try(each.value.access_points, var.defaults.access_points, {})
attach_policy = try(each.value.attach_policy, var.defaults.attach_policy, true)
availability_zone_name = try(each.value.availability_zone_name, var.defaults.availability_zone_name, null)
bypass_policy_lockout_safety_check = try(each.value.bypass_policy_lockout_safety_check, var.defaults.bypass_policy_lockout_safety_check, null)
create = try(each.value.create, var.defaults.create, true)
create_backup_policy = try(each.value.create_backup_policy, var.defaults.create_backup_policy, true)
create_replication_configuration = try(each.value.create_replication_configuration, var.defaults.create_replication_configuration, false)
create_security_group = try(each.value.create_security_group, var.defaults.create_security_group, true)
creation_token = try(each.value.creation_token, var.defaults.creation_token, null)
deny_nonsecure_transport = try(each.value.deny_nonsecure_transport, var.defaults.deny_nonsecure_transport, true)
enable_backup_policy = try(each.value.enable_backup_policy, var.defaults.enable_backup_policy, true)
encrypted = try(each.value.encrypted, var.defaults.encrypted, true)
kms_key_arn = try(each.value.kms_key_arn, var.defaults.kms_key_arn, null)
lifecycle_policy = try(each.value.lifecycle_policy, var.defaults.lifecycle_policy, {})
mount_targets = try(each.value.mount_targets, var.defaults.mount_targets, {})
name = try(each.value.name, var.defaults.name, "")
override_policy_documents = try(each.value.override_policy_documents, var.defaults.override_policy_documents, [])
performance_mode = try(each.value.performance_mode, var.defaults.performance_mode, null)
policy_statements = try(each.value.policy_statements, var.defaults.policy_statements, [])
provisioned_throughput_in_mibps = try(each.value.provisioned_throughput_in_mibps, var.defaults.provisioned_throughput_in_mibps, null)
replication_configuration_destination = try(each.value.replication_configuration_destination, var.defaults.replication_configuration_destination, {})
security_group_description = try(each.value.security_group_description, var.defaults.security_group_description, null)
security_group_name = try(each.value.security_group_name, var.defaults.security_group_name, null)
security_group_rules = try(each.value.security_group_rules, var.defaults.security_group_rules, {})
security_group_use_name_prefix = try(each.value.security_group_use_name_prefix, var.defaults.security_group_use_name_prefix, false)
security_group_vpc_id = try(each.value.security_group_vpc_id, var.defaults.security_group_vpc_id, null)
source_policy_documents = try(each.value.source_policy_documents, var.defaults.source_policy_documents, [])
tags = try(each.value.tags, var.defaults.tags, {})
throughput_mode = try(each.value.throughput_mode, var.defaults.throughput_mode, null)
}
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 5c7ce58

Please sign in to comment.