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

Problem with auto-naming of singleton lambda functions #5616

Closed
reidca opened this issue Jan 2, 2020 · 8 comments
Closed

Problem with auto-naming of singleton lambda functions #5616

reidca opened this issue Jan 2, 2020 · 8 comments
Assignees
Labels
@aws-cdk/aws-lambda Related to AWS Lambda guidance Question that needs advice or information. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.

Comments

@reidca
Copy link

reidca commented Jan 2, 2020

The Question

When creating a Singleton Lambda function (https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.SingletonFunction.html) and not specifying the FunctionName property specifically, CDK creates a name in the format "StackName-SingletonLambdaGUID"

e.g. S3FtpStack-SingletonLambda0d11343f709a4e728c0caf32-XRCK830I30HY

If you have multiple singleton lambda functions this makes it impossible to work out what the function does since they look identical apart from the guid at the end.

I would have expected CDK to use the resource name provided to stem the function name just like it does when creating a lambda.Function.

e.g. S3FtpStack-ftpUploaderHandler6825122D-1LK6V6JG68LPJ

I do not want to name the function specifically because this then makes it impossible for cloudformation to replace the function.

Is this expected behaviour? If so why and could it be changed?

Environment

  • CDK CLI Version: 1.15.0 (build bdbe3aa)
  • Module Version: 1.11.0",
  • OS: All
  • Language: TypeScript
@reidca reidca added the needs-triage This issue or PR still needs to be triaged. label Jan 2, 2020
@SomayaB SomayaB added guidance Question that needs advice or information. @aws-cdk/aws-lambda Related to AWS Lambda feature-request A feature should be added or improved. labels Jan 2, 2020
@cmckni3
Copy link
Contributor

cmckni3 commented Jan 2, 2020

I have the same issue with logical id generation in general. I set a description on singleton lambda to help me identify them.

I cleaned up logical ids by overriding allocateLogicalId.

I understand the reasoning behind the unique id generation and logical id stability. It makes it difficult to identity resources (especially in nested stacks). It seems to have been discussed before in #1424 and #1594.

I would appreciate guidance from the CDK team as well.

@reidca
Copy link
Author

reidca commented Jan 3, 2020

Reading the docs it seems you should be able to override the logical id generation however this doesn't work in my tests.

let baseCfnFunction = myFunction.node.defaultChild as lambda.CfnFunction
baseCfnFunction .overrideLogicalId("mylambdaname")

When I use this code on a lambda.SingletonFunction I get a runtime undefined error:

Cannot read property 'overrideLogicalId' of undefined
Subprocess exited with error 1

@cmckni3
Copy link
Contributor

cmckni3 commented Jan 3, 2020

I ran into the same issue. The method definitely exists on CfnElement still.

renameLogicalId may work.

EDIT Looks like defaultChild is not set for some reason.

@cmckni3
Copy link
Contributor

cmckni3 commented Jan 3, 2020

#207 (comment)

@nija-at
Copy link
Contributor

nija-at commented Jan 27, 2020

@reidca & @cmckni3 - we have an option for this called lambdaPurpose as part of SingletonFunction.

Have you tried using that?

@nija-at nija-at added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Jan 27, 2020
@cmckni3
Copy link
Contributor

cmckni3 commented Jan 27, 2020

@nija-at yes, I figured that out.

Is it possible to override purpose on CDK provided custom resources?

@reidca
Copy link
Author

reidca commented Jan 28, 2020

@reidca & @cmckni3 - we have an option for this called lambdaPurpose as part of SingletonFunction.

Have you tried using that?

Can I understand why the Lambda.SingletonFunction is different from the Lambda.Function in the way they are named by CDK?

@nija-at
Copy link
Contributor

nija-at commented Jan 28, 2020

The SingletonFunction lets you add this function into your stack any number of times and guarantees that it will be added only once, as long as the uuid and the lambdaPurpose remain the same.

This allows for another construct to define a lambda function as part of its construction (using SingletonFunction) such that this other construct can be added to the stack any number of times while guaranteeing that the lambda function is added only once. For example, the GlobalTableCoordinator construct can be added to your stack any number of times but there will be one and only one of the underlying lambda function in the stack.

On the other hand, the regular Function construct will fail with the error that the function already exists in the stack.

Hope that makes sense?

Closing this issue for now. Please reopen if there's something I've missed.

@nija-at nija-at closed this as completed Jan 28, 2020
mergify bot added a commit that referenced this issue Jan 29, 2020
relates #5616

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-lambda Related to AWS Lambda guidance Question that needs advice or information. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Projects
None yet
Development

No branches or pull requests

4 participants