Łukasz
Łukasz

Reputation: 2162

AWS CDK - post deployment actions

is anyone aware of a method to execute post-deploy functionality. Follwing is a sample of a casual CDK app.

app = core.App()
Stack(app, ...)
app.synth()

What I am looking for is a way to apply some logic after template is deployed. The thing is the app completes before cdk tool starts deploying template.

thanks

Upvotes: 12

Views: 12369

Answers (4)

bgdnlp
bgdnlp

Reputation: 1145

Edit: CDK now has https://github.com/cdklabs/cdk-triggers, which allows calling Lambda functions before/after resource/stack creation


You can't do that from CDK at the moment. See https://github.com/awslabs/aws-cdk/issues/2849. Maybe add your +1 there, let them know you'd like to see this feature.

What you can do is wrap cdk deploy in a shell script that will run whatever you need after the CDK is done. Something like:

#!/bin/sh

cdk deploy "$@"
success=$?
if [ $success != 0 ]; then
    exit $success
fi

run_post_deploy_with_arguments.sh "$@"

will run deploy with the given arguments, then call a shell scripts passing it the same arguments if deployment was successful. This is a very crude example.

Upvotes: 8

marcuse
marcuse

Reputation: 3999

Instead of wrapping the cdk deploy command in a bash script I find it more convenient to add a pre and post deployment script under a cdk_hooks.sh file and call it before and after the CDK deployment command via the cdk.json file. In this way you can keep using the cdk deploy command without calling custom scripts manually.

cdk.json

{
    "app": "sh cdk_hooks.sh pre && npx ts-node bin/stacks.ts && sh cdk_hooks.sh post"
    ,
    "context": {
      "@aws-cdk/core:enableStackNameDuplicates": "true",
      "aws-cdk:enableDiffNoFail": "true"
    }
  }

and cdk_hooks.sh

#!/bin/bash
PHASE=$1
case "$PHASE" in
    pre)
            # Do something
            ;;
    post)   
            # Do something
            ;;
    *)
            echo "Please provide a valid cdk_hooks phase"
            exit 64
esac

Upvotes: 14

Luca T
Luca T

Reputation: 668

Short answer: you can't. I've been waiting for this feature as well.

What you can do is wrap your deployment in a custom script that performs all your other logic, which also makes sense given that what you want to do is probably not strictly a "deploy thing" but more like "configure this and that now that the deploy is finished".

Another solution would be to rely on codebuild to perform your deploys and define there all your steps and which custom scripts to run after a deploy (I personally use this solution, with a specific stack to deploy this particular codedeploy project).

Upvotes: 1

vladvel
vladvel

Reputation: 173

You can use CustomResource to run some code in a lambda (which you will also need to deploy unfortunately). The lambda will get the event of the custom resource (create, update delete), so you will be able to handle different scenarios (let's say you want to seed some table after deploy, this way you will be able to clean the data at a destroy for instance).

Here is a pretty good post about it.

Personally I couldn't find a more elegant way to do this.

Upvotes: 3

Related Questions