Skip to content

Instantly share code, notes, and snippets.

@lucjross
Last active May 20, 2020 02:54
Show Gist options
  • Save lucjross/e1a369a68103d198cb576af866c81b50 to your computer and use it in GitHub Desktop.
Save lucjross/e1a369a68103d198cb576af866c81b50 to your computer and use it in GitHub Desktop.
ALB Listener Rule, weighted Target Groups
resource "aws_alb_listener_rule" "http_80" {
count = local.lb ? 1 : 0
listener_arn = data.aws_alb_listener.http_80.arn
action {
type = "forward"
target_group_arn = aws_alb_target_group.http_80[0].arn
}
condition {
host_header {
values = [local.host_name]
}
}
// todo: terraform does not yet support weighted target groups
provisioner "local-exec" {
command = local.blue_green ? "${path.module}/add_target_groups_to_listener_rule.sh ${data.aws_region.current.name} ${data.aws_alb_listener.http_80.arn} ${self.arn} ${aws_alb_target_group.http_80[1].arn}" : "echo"
}
lifecycle {
ignore_changes = [action.0.target_group_arn]
}
}
#!/bin/bash
set -eo pipefail
aws_region=$1
listener_arn=$2
rule_arn=$3
tg_http_80_1_arn=$4
rules=$(aws elbv2 describe-rules --region $aws_region --listener-arn $listener_arn)
rule=$(echo $rules | jq --arg rule_arn "$rule_arn" '.Rules[] | select(.RuleArn == $rule_arn)')
expr_fwdcfg='.Actions[.Actions | map(.Type=="forward") | index(true)].ForwardConfig'
rule=$(echo $rule | jq --arg tg_arn "$tg_http_80_1_arn" "$expr_fwdcfg"'
.TargetGroups += [{
"TargetGroupArn": $tg_arn,
"Weight": 0
}]')
rule=$(echo $rule | jq "$expr_fwdcfg"'
.TargetGroupStickinessConfig = {
"Enabled": true,
"DurationSeconds": 86400
}')
rule=$(echo $rule | jq 'del(.Priority)
| del(.IsDefault)
| (.Conditions |= map(del(.Values)))
| (.Actions |= map(del(.TargetGroupArn)))')
echo "rule=$rule"
aws elbv2 modify-rule --region $aws_region --rule-arn $rule_arn --cli-input-json "$rule"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment