Skip to main content

Execute AWS Lambda handlers during deployments of AWS CDK stacks

Project description

CDK Triggers

The APIs of higher level constructs in this module are experimental and under active development. They are subject to non-backward compatible changes or removal in any future version. These are not subject to the Semantic Versioning model and breaking changes will be announced in the release notes. This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.

Overview

Allow specifying arbitrary handlers which execute as part of the deployment process and trigger them before/after resources or stacks.

Usage

You can trigger the execution of arbitrary AWS Lambda functions before or after resources or groups of resources are provisioned using the Triggers API.

The library includes constructs that represent different triggers. The BeforeCreate and AfterCreate constructs can be used to trigger a handler before/after a set of resources have been created.

new triggers.AfterCreate(this, 'InvokeAfter', {
  resources: [resource1, resource2, stack, ...],
  handler: myLambdaFunction,
});

Where resources is a list of construct scopes which determine when handler is invoked. Scopes can be either specific resources or composite constructs (in which case all the resources in the construct will be used as a group). The scope can also be a Stack, in which case the trigger will apply to all the resources within the stack (same as any composite construct). All scopes must roll up to the same stack.

Let's look at an example. Say we want to publish a notification to an SNS topic that says "hello, topic!" after the topic is created.

// define a topic
const topic = new sns.Topic(this, 'MyTopic');

// define a lambda function which publishes a message to the topic
const publisher = new NodeJsFunction(this, 'PublishToTopic');
publisher.addEnvironment('TOPIC_ARN', topic.topicArn);
publisher.addEnvironment('MESSAGE', 'Hello, topic!');
topic.grantPublish(publisher);

// trigger the lambda function after the topic is created
new triggers.AfterCreate(this, 'SayHello', {
  scopes: [topic],
  handler: publisher
});

Additional Notes

  • If the trigger fails, deployment fails.
  • If the handler changes (configuration or code), the trigger gets re-executed (trigger is bound to lambda.currentVersion which gets recreated when the function changes).

Roadmap

  • Additional periodic execution after deployment (repeatOnSchedule).
  • Async checks (retryWithTimeout)
  • Execute shell command inside a Docker image

Use Cases

Here are some examples of use cases for triggers:

  • Intrinsic validations: execute a check to verify that a resource or set of resources have been deployed correctly

    • Test connections to external systems (e.g. security tokens are valid)
    • Verify integration between resources is working as expected
    • Execute as one-off and also periodically after deployment
    • Wait for data to start flowing (e.g. wait for a metric) before deployment is successful
  • Data priming: add data to resources after they are created

    • CodeCommit repo + initial commit
    • Database + test data for development
  • Check prerequisites before depoyment

    • Account limits
    • Availability of external services
  • Connect to other accounts

Security

See CONTRIBUTING for more information.

License

This project is licensed under the Apache-2.0 License.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cdk-triggers-0.1.11.tar.gz (32.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cdk_triggers-0.1.11-py3-none-any.whl (31.2 kB view details)

Uploaded Python 3

File details

Details for the file cdk-triggers-0.1.11.tar.gz.

File metadata

  • Download URL: cdk-triggers-0.1.11.tar.gz
  • Upload date:
  • Size: 32.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2

File hashes

Hashes for cdk-triggers-0.1.11.tar.gz
Algorithm Hash digest
SHA256 b4cee3efc4b4664f7a1e0947a9a8c32978e531c6649d587ff2f5d10fbb2fbc04
MD5 1a2914d3f9a55c0d2d7157218b33086c
BLAKE2b-256 2653338d48a527ddcfe83253a715e45595d28afd4f7e62d85dd0254bcb17f21e

See more details on using hashes here.

File details

Details for the file cdk_triggers-0.1.11-py3-none-any.whl.

File metadata

  • Download URL: cdk_triggers-0.1.11-py3-none-any.whl
  • Upload date:
  • Size: 31.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2

File hashes

Hashes for cdk_triggers-0.1.11-py3-none-any.whl
Algorithm Hash digest
SHA256 50858f899bad256422a2f47755e10b6221b2de870dcc1b5f1085b1708f4db9e4
MD5 1c43838111f6774be2c2efe1cea9631d
BLAKE2b-256 7a8f7545fcabba77e56c5d7c5bf61be7fba2d78385e6c30e7bcb443452c95ba4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page