getmoto / moto

A library that allows you to easily mock out tests based on AWS infrastructure.
http://docs.getmoto.org/en/latest/
Apache License 2.0
7.65k stars 2.05k forks source link

Cannot create ELBV2 listener default rule with ForwardConfig #4695

Closed rickbowden closed 2 years ago

rickbowden commented 2 years ago

Python version: 3.6 Moto version: 2.2.18

I'm trying to create an application load balancer with a single listener. That lister should have a default rule with weighted target groups. This is how the default rule config format looks.

"DefaultActions": [
          {
            "Type": "forward",
            "ForwardConfig": {
              "TargetGroups": [
                {
                  "TargetGroupArn": "myapp-blue-tg",
                  "Weight": 100
                },
                {
                  "TargetGroupArn": "myapp-green-tg",
                  "Weight": 0
                }
              ],
              "TargetGroupStickinessConfig": {
                "Enabled": false,
                "DurationSeconds": 300
              }
            }
          }
        ]

The error I get is File "C:\Users**\AppData\Roaming\Python\Python36\site-packages\moto\elbv2\models.py", line 1001, in create_listener target_group = self.target_groups[action.data["TargetGroupArn"]] KeyError: 'TargetGroupArn'

Looking at the models.py create_listener code, it fails in this loop, where it attempts to get the TargetGroupArn attribute from action.data. However when I step through and look at action.data it does not contain a TargetGroupArn key it contains the entire default actions list.

for action in default_actions:
    if action.type == "forward":
        target_group = self.target_groups[action.data["TargetGroupArn"]]
        target_group.load_balancer_arns.append(load_balancer_arn)
bblommers commented 2 years ago

Hi @rickbowden, thanks for raising this! Looks like we only support sending a single TargetGroupArn - will mark it as an enhancement to also support the ForwardConfig-parameter.

rickbowden commented 2 years ago

Many thanks @bblommers