Aws-cdk: Implement custom resource provider framework as singleton

Created on 14 Nov 2019  路  2Comments  路  Source: aws/aws-cdk


Hi,
i have been trying out the new provider framework for custom resources and am very pleased with it. However currently, for every custom resource a new provider lambda function is generated in the stack.

Use Case


I want to call a custom resource around 50 times. This results in my custom resource function deployed once (singleton) and 50 provider functions being deployed, 1 for each custom resource call.

Proposed Solution


Looking at the code only 3 things change in each of these provider functions.

  • The entry point of the function
  • The onEvent handler function (as env var)
  • The onComplete handler function (as env var)

It would be great if the provider function could be implemented as a singleton, passing these parameters in the payload instead. This would cut down on the lambda functions created drastically.

Other

  • [ ] :wave: I may be able to implement this feature request
  • [ ] :warning: This feature might incur a breaking change

This is a :rocket: Feature Request

@aws-cdcustom-resources feature-request needs-triage

Most helpful comment

We can definitely add some sugar to make it easier to implement a provider as a singleton, but it is fairly easy to implement yourself. Here's the pattern:

class MyProvider extends Construct {
  public static getOrCreate(scope: Construct) {
    const stack = Stack.of(scope);
    const id = 'unique-id-of-provider-so-it-doesnt-conflict-with-others';
    return stack.node.tryGetChild(id) as MyProvider || new MyProvider(stack, id);
  }

  public readonly provider: cr.Provider;
  constructor(scope: Construct, id: string) {
    super(scope, id);
    this.provider = new cr.Provider(this, 'MyProvider', { ... });
  }
}

Let me know if this is helpful

All 2 comments

We can definitely add some sugar to make it easier to implement a provider as a singleton, but it is fairly easy to implement yourself. Here's the pattern:

class MyProvider extends Construct {
  public static getOrCreate(scope: Construct) {
    const stack = Stack.of(scope);
    const id = 'unique-id-of-provider-so-it-doesnt-conflict-with-others';
    return stack.node.tryGetChild(id) as MyProvider || new MyProvider(stack, id);
  }

  public readonly provider: cr.Provider;
  constructor(scope: Construct, id: string) {
    super(scope, id);
    this.provider = new cr.Provider(this, 'MyProvider', { ... });
  }
}

Let me know if this is helpful

Thank you, yes it was. I was able to implement a singleton provider with this.

Was this page helpful?
0 / 5 - 0 ratings