0

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). enter image description here

What could be the problem? Thank you.

Yuval Podoksik
  • 508
  • 2
  • 7
  • 23
  • Where are you running the command? Windows (PowerShell, cmd), Mac, Linux (which shell)? – John Rotenstein Jul 04 '20 at 23:45
  • It's a shell script running from Windows. – Yuval Podoksik Jul 05 '20 at 05:50
  • Within the JSON, you would need to put the ARNs within quote marks, and possibly the numbers too. Worst case, you can provide the JSON in a file. For an example, see: [aws-cli: awscli/examples/elbv2/create-rule.rst | Fossies](https://fossies.org/linux/aws-cli/awscli/examples/elbv2/create-rule.rst) – John Rotenstein Jul 05 '20 at 06:50

1 Answers1

1

Your issue is about Linux variable expansion, not exactly about AWS CLI usage.
On your command that is failing you are using single quote ', which will not expand variables to its value.
To expand variables you need to use double quote " or no quote at all, as you did in your second example.

Azize
  • 4,006
  • 2
  • 22
  • 38
  • Not worked, without quotes at all I'm getting the error: Error parsing parameter '--default-actions': Invalid JSON: [{Type: With double quotes, I'm getting the error: Expecting property name enclosed in double quotes: line 1 column 3 (char 2) – Yuval Podoksik Jul 04 '20 at 16:25
  • Try double quotes outside and single quotes inside. – Azize Jul 04 '20 at 23:54