AWSCDK::Triggers

8 types

Triggers

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

Usage

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

require 'aws-cdk-lib'


AWSCDK::Triggers::TriggerFunction.new(self, "MyTrigger", {
    runtime: AWSCDK::Lambda::Runtime.NODEJS_LATEST,
    handler: "index.handler",
    code: AWSCDK::Lambda::Code.from_asset(__dirname + "/my-trigger"),
})

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

It is also possible to trigger a predefined Lambda function by using the Trigger construct:

require 'aws-cdk-lib'


func = AWSCDK::Lambda::Function.new(self, "MyFunction", {
    handler: "index.handler",
    runtime: AWSCDK::Lambda::Runtime.NODEJS_LATEST,
    code: AWSCDK::Lambda::Code.from_inline("foo"),
})

AWSCDK::Triggers::Trigger.new(self, "MyTrigger", {
    handler: func,
    timeout: AWSCDK::Duration.minutes(10),
    invocation_type: AWSCDK::Triggers::InvocationType::EVENT,
})

Addition properties can be used to fine-tune the behaviour of the trigger. The timeout property can be used to determine how long the invocation of the function should take. The invocation_type property can be used to change the invocation type of the function. This might be useful in scenarios where a fire-and-forget strategy for invoking the function is sufficient.

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.

Note that this behavior is only applied when invocationType is REQUEST_RESPONSE. When invocationType is EVENT, Lambda function is invoked asynchronously. In that case, if Lambda function is invoked successfully, the trigger will success regardless of the result for the function execution.

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 execute_after and execute_before 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 my_trigger will be executed. Only then the resources under goodbye will be provisioned:

require 'aws-cdk-lib'

my_trigger = nil # AWSCDK::Triggers::Trigger
hello = nil # Constructs::Construct
world = nil # Constructs::Construct
goodbye = nil # Constructs::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, execute_on_handler_change 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:

API Reference

Classes 2

TriggerTriggers an AWS Lambda function during deployment. TriggerFunctionInvokes an AWS Lambda function during deployment.

Interfaces 4

ITriggerInterface for triggers. TriggerFunctionPropsProps for `InvokeFunction`. TriggerOptionsOptions for `Trigger`. TriggerPropsProps for `Trigger`.

Enums 2

InvocationTypeThe invocation type to apply to a trigger. TriggerInvalidationDetermines.