Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add wrapper #270

Conversation

SlavaSubotskiy
Copy link

@SlavaSubotskiy SlavaSubotskiy commented Apr 26, 2022

Description

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).

Inspired by https://github.com/terraform-aws-modules/terraform-aws-s3-bucket/tree/master/wrappers

Motivation and Context

We are using Terragrunt to launch multiple EC2 instances with similar configuration. To keep DRY, we had to implement a wrapper module with for_each implementation.
The only difference between this wrapper and the https://github.com/terraform-aws-modules/terraform-aws-s3-bucket/tree/master/wrappers, is that the defaults variable was added, to support more DRY configurations in case of similar instances configuration(see example in the README)
The main.tf was generated by a dummy bash script:

#!/usr/bin/env bash

VARS=$(cat variables.tf| grep -e '^variable ' -e '  default     =' | perl -p -e 's/variable "//g;s/" {\n//;s/  default     = / /g')

# Get the longest variable
THE_LONGEST_VAR_SIZE=0
while read -r line;
do
   var="$(echo ${line} | awk '{print $1}')"
   VAR_SIZE=${#var}
   if [ ${VAR_SIZE} -gt ${THE_LONGEST_VAR_SIZE} ]; then THE_LONGEST_VAR_SIZE=${VAR_SIZE}; fi
done < <(echo "${VARS}")

# Generate wrapper content

cat << EOF
module "wrapper" {
  source = "../"

  for_each = var.items

EOF

while read -r line;
do
   var="$(echo ${line} | awk '{print $1}')"
   value="$(echo ${line} | awk '{print $2}')"
   VAR_SIZE=${#var}
   SPACES=$((${THE_LONGEST_VAR_SIZE} - ${VAR_SIZE}))

   echo "  ${var} $(perl -E "print ' ' x ${SPACES}")= try(each.value.${var}, var.defaults.${var}, ${value})"

done < <(echo "${VARS}")

echo '}'

Breaking Changes

No breaking changes introduced

How Has This Been Tested?

  • terraform fmt/terraform validate/tflint executed
  • terragrunt plan reviewed for configurations with and without defaults
  • real instances provisioned with defaults

@SlavaSubotskiy SlavaSubotskiy changed the title feat: add wrapper feat: Add wrapper Apr 26, 2022
@antonbabenko
Copy link
Member

Very good timing :) I have just started implementing this as a pre-commit-hook in antonbabenko/pre-commit-terraform#202 an hour ago.

I will extend it with the support for defaults like you propose.

Let's keep this PR open for a while.

@antonbabenko
Copy link
Member

This issue has been resolved in version 3.6.0 🎉

@github-actions
Copy link

github-actions bot commented Nov 8, 2022

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants