pulumi / pulumi-awsx

AWS infrastructure best practices in component form!
https://www.pulumi.com/docs/guides/crosswalk/aws/
Apache License 2.0
218 stars 105 forks source link

unable to define inline listeners: `Attribute "port" must be specified` #1292

Open rpmccarter opened 3 months ago

rpmccarter commented 3 months ago

What happened?

When attempting to modify a load balancer, the load balancer started complaining that Attribute "port" must be specified, even though it already is.

$ pulumi preview
Previewing update (dev)

View in Browser (Ctrl+O): https://app.pulumi.com/rpmccarter/pulumi-awsx-targetgroup-bug-repro/dev/previews/51a7e92a-bd5f-485f-98fa-45549895d59d

     Type                                Name                                   Plan       Info
 +   pulumi:pulumi:Stack                 pulumi-awsx-targetgroup-bug-repro-dev  create     1 error
 +   ├─ awsx:ec2:DefaultVpc              default-vpc                            create     
 +   ├─ aws:ecs:Cluster                  test-cluster-dev                       create     
 +   ├─ aws:ec2:SecurityGroup            test-service-sg-dev                    create     
 +   ├─ aws:lb:TargetGroup               test-server-tg-dev                     create     
 +   └─ awsx:lb:ApplicationLoadBalancer  test-lb-dev                            create     
 +      ├─ aws:ec2:SecurityGroup         test-lb-dev                            create     
 +      └─ aws:lb:TargetGroup            test-lb-dev                            create     1 error

Diagnostics:
  aws:lb:TargetGroup (test-lb-dev):
    error: Preview failed: diffing urn:pulumi:dev::pulumi-awsx-targetgroup-bug-repro::awsx:lb:ApplicationLoadBalancer$aws:lb/targetGroup:TargetGroup::test-lb-dev: Invalid Attribute Combination

    Attribute "port" must be specified when "target_type" is "ip".
    target_type

  pulumi:pulumi:Stack (pulumi-awsx-targetgroup-bug-repro-dev):
    error: preview failed

Example

I created a github repo with the repro code: https://github.com/rpmccarter/pulumi-awsx-lb-port-bug-repro

Output of pulumi about

CLI          
Version      3.116.1
Go Version   go1.22.2
Go Compiler  gc

Plugins
KIND      NAME    VERSION
resource  aws     6.37.1
resource  awsx    2.10.0
resource  docker  4.5.4
resource  docker  3.6.1
language  nodejs  unknown

Host     
OS       darwin
Version  14.5
Arch     arm64

This project is written in nodejs: executable='/Users/rpmccarter/.nvm/versions/node/v20.10.0/bin/node' version='v20.10.0'

Backend        
Name           pulumi.com
URL            https://app.pulumi.com/rpmccarter
User           rpmccarter
Organizations  rpmccarter
Token type     personal

Dependencies:
NAME            VERSION
@pulumi/aws     6.37.1
@pulumi/awsx    v2.10.0
@pulumi/pulumi  3.117.0
@types/node     18.19.33
typescript      5.4.5

Pulumi locates its logs in /var/folders/dn/z0by0dcj1gnbkjr6_t71hp_m0000gn/T/ by default

Additional context

This bug was introduced in v2.9.0 with the bump of the @pulumi/aws package - unsure if this is a bug with this package or @pulumi/aws

Edit: After some debugging, I sense that the error is not complaining about my target group, but about the default target group. Supplying defaultTargetGroupPort does not fix the issue, but supplying defaultTargetGroup does. I believe these lines are the root of the issue.

Contributing

Vote on this issue by adding a 👍 reaction. To contribute a fix for this issue, leave a comment (and link to your pull request, if you've opened one already).

flostadler commented 3 months ago

Thanks for reaching out @rpmccarter! You are indeed correct that this is caused by the defaultTargetGroup that's created here: https://github.com/pulumi/pulumi-awsx/blob/master/awsx/lb/applicationLoadBalancer.ts#L136-L145

The ApplicationLoadBalancer component always creates a target group, which ends up failing in your case because it cannot infer port or protocol for the targets; because there's no targets.

What you can do as a workaround is defining the port & protocol for the defaultTargetGroup like so:

new awsx.lb.ApplicationLoadBalancer("alb", {
    defaultTargetGroup: {
        port: 443,
        protocol: "HTTP",
    },
    listeners: [
        {
          port: 443,
          protocol: 'HTTP',
          defaultActions: [{
            type: "fixed-response",
            fixedResponse: {
              contentType: "text/plain",
              messageBody: "Hello world",
              statusCode: "200",
            },
          }],
        }
      ]
});

Additionally I checked if this is a regression compared to v2.8.0. Previously you could run pulumi preview and it wouldn't report any error, but if you tried running pulumi up it failed. You cannot create a target group of type ip without a port. So it's not a regression, the issue is rather caught during preview time instead.

This AWS CLI command shows that behavior:

aws elbv2 create-target-group \
    --name my-targets \
    --protocol HTTP \
    --target-type ip \
    --vpc-id vpc-3ac0fb5f --region us-west-2 --profile pulumi-dev-sandbox

An error occurred (ValidationError) when calling the CreateTargetGroup operation: A port must be specified

What changed from v2.8.0 to v2.9.0 is that this is now caught during preview instead of only during the apply.

flostadler commented 3 months ago

We could add an optional flag to prevent creating the defaultTargetGroup, but that output is required right now. So this would be a breaking change: https://github.com/pulumi/pulumi-awsx/blob/master/awsx/schema-types.ts#L276