Deploying infrastructure with AWS CDK is powerful — but sometimes, you want something more than just resources in place. Maybe you want to:
- Seed a database after deployment
- Populate an S3 bucket with default data
- Kick off a workflow
- Or simply validate that everything deployed successfully
That's where the cdk-triggers
module comes in. In this post, we'll walk through how to use cdk.triggers.Trigger
to automatically invoke a Lambda function after a successful CDK deployment.
🔧 What is cdk.triggers.Trigger
?
The Trigger
construct is part of the aws-cdk-lib.triggers
module. It allows you to run a Lambda automatically after your stack is deployed — great for bootstrapping and post-deploy configuration tasks.
🧪 Example: Run a Lambda After Resources Are Created
Let’s say you’re deploying:
- An S3 bucket
- A DynamoDB table
- A Lambda that seeds a database
You want the Lambda to run after all the resources are successfully created.
Step 1: Import Required Modules
import * as cdk from "aws-cdk-lib";
import * as lambda from "aws-cdk-lib/aws-lambda";
import * as s3 from "aws-cdk-lib/aws-s3";
import * as dynamodb from "aws-cdk-lib/aws-dynamodb";
import * as triggers from "aws-cdk-lib/triggers";
import { Construct } from "constructs";
Step 2: Define the Stack
export class MyStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// S3 bucket
const bucket = new s3.Bucket(this, "MyBucket");
// DynamoDB table
const table = new dynamodb.Table(this, "MyTable", {
partitionKey: { name: "id", type: dynamodb.AttributeType.STRING },
});
// Lambda function to run post-deploy
const seedFunction = new lambda.Function(this, "SeedFunction", {
runtime: lambda.Runtime.NODEJS_18_X,
handler: "index.handler",
code: lambda.Code.fromAsset("lambda/seed"),
timeout: cdk.Duration.minutes(5),
environment: {
BUCKET_NAME: bucket.bucketName,
TABLE_NAME: table.tableName,
},
});
bucket.grantReadWrite(seedFunction);
table.grantReadWriteData(seedFunction);
// Create the trigger
const cdkTrigger = new triggers.Trigger(this, "SeedTrigger", {
handler: seedFunction,
timeout: cdk.Duration.minutes(10),
invocationType: triggers.InvocationType.EVENT, // async invocation
});
// Define dependencies – only run trigger after these resources are created
cdkTrigger.executeAfter(bucket);
cdkTrigger.executeAfter(table);
}
}
🔁 How It Works
During deployment, CDK will:
- Deploy your stack’s resources (bucket, table, Lambda).
- Automatically invoke the
seedFunction
via the trigger. - Ensure it runs only once (unless you opt to re-run it).
🛠 Notes & Best Practices
- The
Trigger
construct deploys a custom resource under the hood. -
The
invocationType
can be:-
EVENT
: Asynchronous (default and recommended for non-critical background jobs) -
REQUEST_RESPONSE
: Wait for result (useful for critical tasks, but can cause stack rollback on failure)
-
You can use
cdkTrigger.executeAfter(...)
to control execution order and guarantee your resources are ready.To re-run the trigger every deploy, set
cdkTrigger.trigger.runOnUpdate = true
.
🧹 Optional: Clean Up After Execution
By default, the Lambda and the trigger stay in place. If you want to clean up the Lambda after running:
cdkTrigger.applyRemovalPolicy(cdk.RemovalPolicy.DESTROY);
Or consider separating your post-deployment logic into a one-time stack.
✅ Summary
cdk.triggers.Trigger
is a great way to kick off post-deployment automation in a clean, repeatable way.
Use it to:
- Seed databases
- Initialize state
- Validate configs
- Or automate any once-per-deploy logic
No need for separate scripts or manual steps — just CDK.
📁 Example Directory Structure
my-app/
├── bin/
│ └── my-app.ts
├── lib/
│ └── my-stack.ts
├── lambda/
│ └── seed/
│ └── index.ts
🧪 Want to Try It?
Make sure you have the triggers module installed:
npm install aws-cdk-lib constructs
That’s it! Happy automating with CDK! ⚙️
Top comments (0)