Created
May 17, 2019 17:47
-
-
Save tsprlng/50ffbec22c882762c85415ec0652f668 to your computer and use it in GitHub Desktop.
Correct aws_lb_listener_rule priorities to avoid Terraform recreating them unnecessarily
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
#!/usr/bin/ruby | |
# Terraform can't adjust priorities of `aws_lb_listener_rule`s without recreating them. | |
# If you have external dependencies on the ARNs this can be quite annoying. | |
# This script will fetch the ARNs out of terraform state and produce JSON to apply the defined priorities using `aws elbv2 set-rule-priorities`. | |
# Usage: | |
# $ terraform workspace select <environment-or-whatever> | |
# $ aws --region eu-west-1 elbv2 set-rule-priorities --rule-priorities $(./fix-aws-lb-listener-rule-priorities.rb) | |
require 'json' | |
Expecting = { | |
'aws_lb_listener_rule.my_rule' => 101, | |
'aws_lb_listener_rule.my_other_rule' => 301, | |
} | |
State = JSON.parse(ARGV[0] ? File.read(ARGV[0]) : `terraform state pull`) | |
Ids = {} | |
State['modules'].each do |m| | |
next unless m['path'] == ['root'] | |
m['resources'].each do |name, details| | |
Ids[name] = details['primary']['id'] if Expecting.keys.include?(name) | |
end | |
end | |
results = [] | |
Ids.each do |name, id| | |
results << {RuleArn: id, Priority: Expecting[name] || raise} | |
end | |
puts JSON.dump(results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment