I'm trying to create a listener for my AWS ALB and attach my 2 target groups to that listener using the "create-listener" command (AWS CLI), but having issues creating the listener with those 2 target groups.
When creating the listener from the AWS Console and attaching both target groups it works as expected, or hen using the hardcoded arns of the target groups with the above syntax it works:
listener=$(aws elbv2 create-listener --load-balancer-arn $lb_arn --protocol HTTP --port $http_external_port --default-actions '[{"Type": "forward", "Order": 1, "ForwardConfig": {"TargetGroups": [{"TargetGroupArn": "HARDCODED_FIRST_TG_ARN", "Weight": 50}, {"TargetGroupArn": "HARDCODED_SECOND_TG_ARN", "Weight": 50}]}}]')
However, when using the ARNs as variables (which works) - the 'create-listener' command fails with the above error message:
first_tg_arn=$(aws elbv2 describe-target-groups --names $first_tg_name --query "TargetGroups[*][TargetGroupArn]" --output text)
second_tg_arn=$(aws elbv2 describe-target-groups --names $second_tg_name --query "TargetGroups[*][TargetGroupArn]" --output text)
listener=$(aws elbv2 create-listener --load-balancer-arn $lb_arn --protocol HTTP --port $http_external_port --default-actions '[{"Type": "forward", "Order": 1, "ForwardConfig": {"TargetGroups": [{"TargetGroupArn": "${first_tg_arn}", "Weight": 50}, {"TargetGroupArn": "${second_tg_arn}", "Weight": 50}]}}]')
An error occurred (ValidationError) when calling the CreateListener operation: '${first_tg_arn}' is not a valid target group ARN
Above attached both ways running the command (with hardcoded ARNs and using the ARNs variables).
What could be the problem? Thank you.