Last active
May 31, 2023 09:38
-
-
Save withakay/c11f33a8472e16f357f15c8f15948de5 to your computer and use it in GitHub Desktop.
Terraform module version pin - search and replace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# A script to search for all terraform files in a directory and replace | |
# the source attribute of any module references with a specific git ref tag/commit. | |
# This is useful for pinning a specific version of a module in a terraform project. | |
# | |
# Note: | |
# this script was tested on MacOS and may need to be modified for other platforms | |
# as the sed command may differ. | |
# | |
# Search pattern: | |
# match a specific git repository and any top level directory | |
# with alphanumeric characters, underscores or hyphens in the name, capture the directory name | |
pattern='source = "git@github\.com:acme-org/shared-terraform-modules\.git\/\/([a-zA-Z0-9_-]+)"' | |
# Replacement pattern: | |
# replace the entire line with the captured directory name | |
# and append a ref tag to the end of the line before the closing double quote | |
replacement='source = "[email protected]:acme-org/shared-terraform-modules.git\/\/\1?ref=my-ref-tag"' | |
# Search directory | |
search_directory='.' | |
# Find files and perform replacement - ignore the .terraform directory | |
# Uncomment the sed command to perform the replacement, note this is destructive and will modify files in place | |
# Perform a dry run first to ensure the correct files are being modified! | |
find "$search_directory" -type f -name "*.tf" -not -path "*/.terraform/*" \ | |
# -exec sed -i '' -E "s#$pattern#$replacement#g" {} \; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment