aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.38k stars 3.79k forks source link

lambda: CDK synth warning message presented as [object Object] #28936

Open enpatrik opened 5 months ago

enpatrik commented 5 months ago

Describe the bug

Warning message is expected to be shown, but the message is not correctly presented.

Expected Behavior

Warning message to be presented correctly.

`addPermission() has no effect on a Lambda Function with region=${this.env.region}, account=${this.env.account}, in a Stack with region=${Stack.of(this).region}, account=${Stack.of(this).account}. Suppress this warning if this is is intentional, or pass sameEnvironment=true to fromFunctionAttributes() if you would like to add the permissions.`

Warning message was added in: https://github.com/aws/aws-cdk/commit/2c21ea06adc629d177db0114f16800d5f5f60c48#diff-527ac1e9edceb6fd9abb75249884fa31415393bbfed87ef28a2faa8a6a39b416R347 CDK v2.112.0

Current Behavior

CDK synth warning message:

[Warning at /CdkTestStack/AuthFunction] [object Object]

Reproduction Steps

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as lambda from 'aws-cdk-lib/aws-lambda'
import * as apigateway from 'aws-cdk-lib/aws-apigateway'

export class CdkTestStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const authFunc = lambda.Function.fromFunctionArn(this,'AuthFunction', 'arn:aws:lambda:eu-west-1:000000000000:function:AuthFunction')

    const authorizer = new apigateway.TokenAuthorizer(this, 'TokenAuthorizer', {
      handler: authFunc,
    })

    const restApi = new apigateway.RestApi(this, 'RestApi', {})
    restApi.root.addResource('hello').addMethod('GET', new apigateway.MockIntegration(), {
      authorizer: authorizer
    })
  }
}

Run:

npx cdk synth -q
# Output:
# [Warning at /CdkTestStack/AuthFunction] [object Object]

By following the advice from the warning message (that was supposed to be shown) and changing the code to use:

const authFunc = lambda.Function.fromFunctionAttributes(this,'AuthFunction', {
  functionArn: 'arn:aws:lambda:eu-west-1:000000000000:function:AuthFunction',
  sameEnvironment: true,
})

The warning is no longer shown (as expected).

Possible Solution

No response

Additional Information/Context

The warning message is still shown when setting skipPermissions = true:

lambda.Function.fromFunctionAttributes(this,'AuthFunction', {
  functionArn: 'arn:aws:lambda:eu-west-1:000000000000:function:AuthFunction',
  skipPermissions: true
})

And the permissions are still added to the function, which was unexpected. Let me know if you consider this a bug and if you want me to create a separate ticket for that.

CDK CLI Version

CDK 2.124.0 (build 4b6724c)

Framework Version

No response

Node.js Version

v20.11.0

OS

MacOS

Language

TypeScript

Language Version

No response

Other information

cdk synth verbose log:

Annotations.addMessage (/cdktest/cdktest/node_modules/aws-cdk-lib/core/lib/annotations.js:1:1608)
Annotations.addWarningV2 (/cdktest/cdktest/node_modules/aws-cdk-lib/core/lib/annotations.js:1:825)
Import.addPermission (/cdktest/cdktest/node_modules/aws-cdk-lib/aws-lambda/lib/function-base.js:2:309)
TokenAuthorizer.addDefaultPermissionRole (/cdktest/cdktest/node_modules/aws-cdk-lib/aws-apigateway/lib/authorizers/lambda.js:1:2064)
TokenAuthorizer.setupPermissions (/cdktest/cdktest/node_modules/aws-cdk-lib/aws-apigateway/lib/authorizers/lambda.js:1:1997)
new TokenAuthorizer (/cdktest/cdktest/node_modules/aws-cdk-lib/aws-apigateway/lib/authorizers/lambda.js:1:3753)
new CdkTestStack (/cdktest/cdktest/lib/cdk-test-stack.ts:16:24)
...

Warning message was added in PR: https://github.com/aws/aws-cdk/pull/27861

msambol commented 5 months ago

I'll take this.

msambol commented 5 months ago

@lmammino I wanted to loop you in here as it's related to https://github.com/aws/aws-cdk/pull/28414. It looks like we're still seeing this.

jaapvanblaaderen commented 2 months ago

FYI, I created a ticket which slightly related to this one: https://github.com/aws/aws-cdk/issues/29887

alexander-dermicus commented 1 month ago

I got the same [object Object] warning on aws-cdk and aws-cdk-lib version 2.114.1 up to 2.143.0 (currently latest version). The warnings only appear since version 2.112.0 in my case. It's the exact same issue as the OP. Somehow the string interpolation turns into [object Object], because if I replace the two ` with regular " quotes I do see the message.

image
hakenmt commented 1 month ago

I get a similar output from a CloudWatch Alarm resource: [Warning at /path/to/my/cloudwatch/alarm] [object Object]. Would be great to be able to see what the actual warning is.