Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws-lambda: Allow undefined values to be passed to the environment prop of a Function #27715

Closed
piotrekwitkowski opened this issue Oct 27, 2023 · 3 comments
Labels
@aws-cdk/aws-lambda Related to AWS Lambda feature-request A feature should be added or improved. p3

Comments

@piotrekwitkowski
Copy link

piotrekwitkowski commented Oct 27, 2023

Describe the bug

TypeScript doesn't allow me to pass objects with undefined props to the environment prop of a Lambda function.
This code could work but currently doesn't:

new Function(this, 'function', {
  environment: {
    myOptionalBucketName: thisBucketMayNotExist?.bucketName // <-- could work but doesn't 
  }
}

I would like to pass such object and let CDK deal with it.

Expected Behavior

TypeScript will not mark this as an error, my stack can be synthesized and deployed.

Current Behavior

I see a type error:

Type 'string | undefined' is not assignable to type 'string'.
  Type 'undefined' is not assignable to type 'string'.ts(2322)
function.d.ts(72, 14): The expected type comes from property 'environment' which is declared here on type 'FunctionProps'

Reproduction Steps

Can be reproduced with this code:

new Function(this, 'function', {
  code: Code.fromInline(''),
  handler: 'index.handler',
  runtime: Runtime.NODEJS_18_X,
  environment: {
    myOptionalBucketName: undefined
  }
}

Possible Solution

The type [key: string] : string specified in function.ts:L82 could be replaced with a Partial<[key: string] : string> (or a Partial<Record<string, string>>).

If needed, the passed values that are undefined should be filtered out before synth/deployment.

The workaround I need to have today is either conditionally append the prop in another statement or use a somewhat ugly conditional assignment with

{
  envVariable1: "aaa",
  ...(myLengthyBucketIdentifier?.bucketName && { myBucketName: myLengthyBucketIdentifier.bucketName })
}

Additional Information/Context

I'm opening this ticket to see if the maintainers see the current behavior as a bug or as a feature. I am able to implement the change/fix if approved.

CDK CLI Version

2.103.1

Node.js Version

18.12.1

OS

MacOS

Language

TypeScript

@piotrekwitkowski piotrekwitkowski added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Oct 27, 2023
@github-actions github-actions bot added the @aws-cdk/aws-lambda Related to AWS Lambda label Oct 27, 2023
@piotrekwitkowski piotrekwitkowski changed the title aws-lambda: Allow undefined values to be passed to the environment prop of a Lambda function aws-lambda: Allow undefined values to be passed to the environment prop of a Function Oct 27, 2023
@khushail khushail self-assigned this Oct 27, 2023
@khushail khushail added investigating This issue is being investigated and/or work is in progress to resolve the issue. and removed needs-triage This issue or PR still needs to be triaged. labels Oct 27, 2023
@khushail khushail added feature-request A feature should be added or improved. and removed bug This issue is a bug. labels Nov 22, 2023
@khushail
Copy link
Contributor

khushail commented Nov 22, 2023

Hi @piotrekwitkowski , thanks for reaching out. Partial types are a JSII limitation and hence can't be supported.

@khushail khushail removed the investigating This issue is being investigated and/or work is in progress to resolve the issue. label Nov 22, 2023
@khushail khushail removed their assignment Nov 22, 2023
@ashishdhingra
Copy link
Contributor

Closing due to lack of traction.

@ashishdhingra ashishdhingra closed this as not planned Won't fix, can't repro, duplicate, stale Jul 31, 2024
Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 31, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
@aws-cdk/aws-lambda Related to AWS Lambda feature-request A feature should be added or improved. p3
Projects
None yet
Development

No branches or pull requests

3 participants