question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deployment Triggers

See original GitHub issue

Description

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

Published: https://github.com/awslabs/cdk-triggers

README

Hypothetical README for this feature

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,
});

Similarly, triggers.BeforeCreate can be used to set up a “before” trigger.

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
});

Requirements

  • One-off exec before/after resource/s are created (Trigger.AfterCreate).
  • Additional periodic execution after deployment (repeatOnSchedule).
  • Async checks (retryWithTImeout)
  • Execute on updates (bind logical ID to hash of CFN properties of triggered resource)
  • 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

Implementation

At the base level, the trigger handler can be invoked through a custom resource and the timing (before/after) will be determined using CFN dependencies (“after” means the trigger CR depends on the scope, and “before” is the opposite).

This simple implementation will allow us to implement “one-off” triggers. This means that we wait for a CFN CREATE request on the custom resource and invoke the handler. Any updates to the stack will not include any changes to the properties of the custom resource and therefore the trigger won’t get invoked again (unless it’s removed).

We need to consider the following:

  • If the trigger handler itself changes, do we want it to be invoked again?
  • If the triggering resource is updated, do we want the trigger to be invoked again?
  • Do we want some kind of support for triggers that always gets invoked (for any update)?
  • Do we want triggers for “AfterDelete” or “AfterUpdate” does that make sense?

Lots to talk about!

Next Steps

  • Least-privilege IAM policy for trigger custom resource provider (currently it’s invokeFunction for * resources).
  • Invoke trigger if another resource is added to the stack (even if the trigger has already been created).
  • Consider adding support for “update” triggers (if the triggering resource has been updated).

Related Issues

See #75 for a discussion, then, use these for e.g. integration test assertions (#31)

Progress

  • Tracking Issue Created
  • RFC PR Created
  • Core Team Member Assigned
  • Initial Approval / Final Comment Period
  • Ready For Implementation
    • implementation issue 1
  • Resolved

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:56
  • Comments:38 (18 by maintainers)

github_iconTop GitHub Comments

7reactions
eladbcommented, Feb 16, 2021

Thanks for everyone who attended CDK Construction Zone. We started building this in the first episode. Code is here: https://github.com/eladb/cdk-triggers

4reactions
project0commented, Dec 8, 2020

Just ran into a situation where i need to run some integration tests in my ci pipeline, some event emitter/hooks would be awesome to bundle that within cdk.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Basic Deployment Operations - OpenShift Documentation
A deployment configuration can contain triggers, which drive the creation of new deployment processes in response to events inside the cluster. If no...
Read more >
Deployment triggers - IBM
Deployment Triggers are ways for environments to subscribe to certain components. A trigger consists of a specified component, deployment process, and user.
Read more >
Deployment triggers - HCL Product Documentation
Deployment Triggers are ways for environments to subscribe to certain components. A trigger consists of a specified component, deployment process, and user.
Read more >
How can I trigger a new deployment of an application?
When your application is deployed using a DeploymentConfig , Deployment , StatefulSet , DaemonSet , or with lower level ReplicaSet and ReplicationContoller ...
Read more >
Release triggers - Azure Pipelines - Microsoft Learn
Continuous deployment triggers; Scheduled release triggers ... When the trigger conditions are met, the pipeline will deploy your artifacts ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found