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

SingletonFunction ignores explicit dependencies via node.add_dependency (or node.addDependency) #7568

Closed
mcalello opened this issue Apr 23, 2020 · 1 comment · Fixed by #7997
Assignees
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. p1

Comments

@mcalello
Copy link

mcalello commented Apr 23, 2020

Explicitly declaring dependencies with SingletonFunction constructs are silently ignored. This is true in both directions:

  • singletonFunction.node.add_dependency(another_construct)
  • another_construct.node.add_dependency(singletonFunction)

Reproduction Steps

cdk synth (or deploy)
        lambda_name_1 = f'{id}-lambda-1'
        singleton_lambda_1 = _lambda.SingletonFunction(
            self, lambda_name_1,
            uuid="B34394B8-3CCA-47FA-B1D5-DCDFCC7ED9CD",  # from uuidgen
            description="An Example Lambda 1",
            runtime=_lambda.Runtime.NODEJS_10_X,
            code=_lambda.InlineCode("exports.handler = (event, context) => context.succeed('hello world1');"),
            handler='index.handler',
            function_name=lambda_name_1
        )

        lambda_name_2 = f'{id}-lambda-2'
        singleton_lambda_2 = _lambda.SingletonFunction(
            self, lambda_name_2,
            uuid="9D7113E8-B954-4B7F-B67E-3C17F641F69A",  # from uuidgen
            description="An Example Lambda 2 - which should explicitly depend on Lambda 1.",
            runtime=_lambda.Runtime.NODEJS_10_X,
            code=_lambda.InlineCode("exports.handler = (event, context) => context.succeed('hello world2');"),
            handler='index.handler',
            function_name=lambda_name_2
        )               
        singleton_lambda_2.node.add_dependency(singleton_lambda_1) # silently ignored dependency on lamda-1
        singleton_lambda_2.node.add_dependency(example_role) # silently ignore dependency on example-role

Error Log

    "SingletonLambda9D7113E8B9544B7FB67E3C17F641F69A4A2DA42D": {
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "Code": {
          "ZipFile": "exports.handler = (event, context) => context.succeed('hello world2');"
        },
        "Handler": "index.handler",
        "Role": {
          "Fn::GetAtt": [
            "SingletonLambda9D7113E8B9544B7FB67E3C17F641F69AServiceRole58F57FC5",
            "Arn"
          ]
        },
        "Runtime": "nodejs10.x",
        "Description": "An Example Lambda 2 - which should explicitly depend on Lambda 1.",
        "FunctionName": "test-depends-lambda-2"
      },
      "DependsOn": [
        "SingletonLambda9D7113E8B9544B7FB67E3C17F641F69AServiceRole58F57FC5"
      ],
      "Metadata": {
        "aws:cdk:path": "test-depends/SingletonLambda9D7113E8B9544B7FB67E3C17F641F69A/Resource"
      }
    }

when I switch to vanilla Function, rather than SingletonFunction for both Lambda1 and Lambda2 I see the expected dependencies present from both Lambda 1, and my Example Role.

Expected Result:

      "DependsOn": [
        "testdependsexamplerole9DFB4232",
        "testdependslambda1CE722EFB",
        "testdependslambda1ServiceRoleBA30F7B6",
        "testdependslambda2ServiceRole1FA22585"
      ],

Furthermore, if I leave lambda1 as a SingletonFunction - and make lambda2 a vanilla Function - it will ignore my explicit dependency with lambda1, but recognize my explicit dependency with Example Role:

      "DependsOn": [
        "testdependsexamplerole9DFB4232",
        "testdependslambda2ServiceRole1FA22585"
      ],

Environment

  • **CLI Version : aws-cli/1.16.223 Python/3.7.4 Darwin/18.7.0 botocore/1.12.213
  • **Framework Version: 1.31.0 (build 8f3ac79), NOTE: Same behavior after upgrading to 1.34.1 (build 7b21aa0)
  • **OS : macOS 10.14.6
  • **Language : python3

Other

I'm not sure this issue is limited to only SingletonFunction constructs.


This is 🐛 Bug Report

@mcalello mcalello added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Apr 23, 2020
@mcalello mcalello changed the title SingletonFunction ignore explicit dependencies via node.add_dependency (or node.addDependency) SingletonFunction ignores explicit dependencies via node.add_dependency (or node.addDependency) Apr 23, 2020
@SomayaB SomayaB added the @aws-cdk/aws-lambda Related to AWS Lambda label Apr 29, 2020
@nija-at
Copy link
Contributor

nija-at commented May 15, 2020

Thanks for filing this issue. This is indeed a problem with SingletonFunction. This is because the underlying Lambda function is attached to the Stack rather than the SingeltonFunction node.

I don't think there's a very good workaround for this. You will need to find the Lambda function node from the stack node based on its slug-ified id.

@nija-at nija-at added p1 and removed needs-triage This issue or PR still needs to be triaged. labels May 15, 2020
nija-at pushed a commit that referenced this issue May 15, 2020
The problem here is that the Function construct underlying the
SingletonFunction construct is attached directly to the Stack to enforce
the Stack-wide singleton behaviour.

This commit adds additional methods `addDependencies()` and `dependOn()`
methods to let customers declare these.

fixes #7568
nija-at pushed a commit that referenced this issue May 15, 2020
The problem here is that the Function construct underlying the
SingletonFunction construct is attached directly to the Stack to enforce
the Stack-wide singleton behaviour.

This commit adds additional methods `addDependencies()` and `dependOn()`
methods to let customers declare these.

fixes #7568
@mergify mergify bot closed this as completed in #7997 May 15, 2020
mergify bot pushed a commit that referenced this issue May 15, 2020
…#7997)

The problem here is that the Function construct underlying the
SingletonFunction construct is attached directly to the Stack to enforce
the Stack-wide singleton behaviour.

This commit adds additional methods `addDependencies()` and `dependOn()`
methods to let customers declare these.

fixes #7568
karupanerura pushed a commit to karupanerura/aws-cdk that referenced this issue May 22, 2020
…aws#7997)

The problem here is that the Function construct underlying the
SingletonFunction construct is attached directly to the Stack to enforce
the Stack-wide singleton behaviour.

This commit adds additional methods `addDependencies()` and `dependOn()`
methods to let customers declare these.

fixes aws#7568
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 bug This issue is a bug. p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants