Skip to main content

Triggers

---

End-of-Support

AWS CDK v1 has reached End-of-Support on 2023-06-01. This package is no longer being updated, and users should migrate to AWS CDK v2.

For more information on how to migrate, see the Migrating to AWS CDK v2 guide.


Triggers allows you to execute code during deployments. This can be used for a variety of use cases such as:

  • Self tests: validate something after a resource/construct been provisioned
  • Data priming: add initial data to resources after they are created
  • Preconditions: check things such as account limits or external dependencies before deployment.

Usage

The TriggerFunction construct will define an AWS Lambda function which is triggered during deployment:

import aws_cdk.aws_lambda as lambda_
import aws_cdk.triggers as triggers
from aws_cdk.core import Stack

# stack: Stack

triggers.TriggerFunction(stack, "MyTrigger",
    runtime=lambda_.Runtime.NODEJS_14_X,
    handler="index.handler",
    code=lambda_.Code.from_asset(__dirname + "/my-trigger")
)

In the above example, the AWS Lambda function defined in myLambdaFunction will be invoked when the stack is deployed.

Trigger Failures

If the trigger handler fails (e.g. an exception is raised), the CloudFormation deployment will fail, as if a resource failed to provision. This makes it easy to implement "self tests" via triggers by simply making a set of assertions on some provisioned infrastructure.

Order of Execution

By default, a trigger will be executed by CloudFormation after the associated handler is provisioned. This means that if the handler takes an implicit dependency on other resources (e.g. via environment variables), those resources will be provisioned before the trigger is executed.

In most cases, implicit ordering should be sufficient, but you can also use executeAfter and executeBefore to control the order of execution.

The following example defines the following order: (hello, world) => myTrigger => goodbye. The resources under hello and world will be provisioned in parallel, and then the trigger myTrigger will be executed. Only then the resources under goodbye will be provisioned:

from constructs import Construct, Node
import aws_cdk.triggers as triggers

# my_trigger: triggers.Trigger
# hello: Construct
# world: Construct
# goodbye: Construct

my_trigger.execute_after(hello, world)
my_trigger.execute_before(goodbye)

Note that hello and world are construct scopes. This means that they can be specific resources (such as an s3.Bucket object) or groups of resources composed together into constructs.

Re-execution of Triggers

By default, executeOnHandlerChange is enabled. This implies that the trigger is re-executed every time the handler function code or configuration changes. If this option is disabled, the trigger will be executed only once upon first deployment.

In the future we will consider adding support for additional re-execution modes:

  • executeOnEveryDeployment: boolean - re-executes every time the stack is deployed (add random "salt" during synthesis).
  • executeOnResourceChange: Construct[] - re-executes when one of the resources under the specified scopes has changed (add the hash the CloudFormation resource specs).

Release files for aws-cdk.triggers 1.204.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aws-cdk.triggers 1.204.0
File Size Uploaded
aws-cdk.triggers-1.204.0.tar.gz 53.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aws-cdk.triggers 1.204.0
File Interpreter ABI Platform
aws_cdk.triggers-1.204.0-py3-none-any.whl Python 3 none any Details

Total release size: 106.3 kB

Release files / aws-cdk.triggers-1.204.0.tar.gz

Download URL aws-cdk.triggers-1.204.0.tar.gz
Size 53.7 kB
Tags Source
SHA-256 checksum
How to use checksums
929d6aef6767711b05dfa5e07d33bac172a9fbb032bbae411f7e8f5b61bbfce1
BLAKE2b-256 checksum
How to use checksums
bfe8a83f039673f4f0a2b7c6ef2e484a63d9568e7fc5d9d7284e67313d9cf54b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release files / aws_cdk.triggers-1.204.0-py3-none-any.whl

Download URL aws_cdk.triggers-1.204.0-py3-none-any.whl
Size 52.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
811b299bea208e33a5a728fd8be2f1dce8214aac62f8aad0bf1377da2f6f2f3c
BLAKE2b-256 checksum
How to use checksums
50e5c69ec07c4f248e83a16026707045414b86ef9a55e58e82feae1d87631bde
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release history Release notifications | RSS feed

This release

1.204.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page