Import existing Step Function into a AWS CDK project

0

We have an existing step function that has multiple lambda functions (Python). All of this was developed using the console. We would like to package all the code into an AWS CDK project for configuration management.

Is there a resource that will help us import the existing artifacts into a CDK project using Python? Is there an import or conversion utility available?

Incidentally, this is something I found on the web which does not seem to be accurate:

*There are a few ways to import an existing AWS Step Function into AWS CDK. One way is to use the aws-cdk-stepfunctions-import library. This library provides a command-line tool that can be used to import an existing Step Function into a CDK project. To use the library, you will need to install it with the following command:

Code snippet npm install aws-cdk-stepfunctions-import

Once the library is installed, you can use the following command to import an existing Step Function: Code snippet

cdk import-step-function <step-function-arn>*

Thanks.

2 Answers
1

Hi there,

First of all, top choice to move to CDK!

This is a great question and something I have wished for myself in the past when trying to shift infrastructure into cdk code that was deployed manually in the console.

I am not sure where you found that information around a aws-cdk-stepfunctions-import package that you can use. I tried to Google it and found nothing.

As far as I am aware, this is not possible but there is a great way to use the Step functions ASL definition that you can pull out of the console.

It requires using this construct: https://constructs.dev/packages/@matthewbonig/state-machine/v/0.0.26?lang=python

You of course need to also think around getting your Lambdas set up too but there are many helpful blogs and tutorials on that.

It should hopefully not be an issue if you deploy new versions of the Lambdas and Step function, created from CDK. You can then delete the old ones. Just be careful around naming!

If you are interested in how to import existing resources into a cdk stack, this is a great article guiding you through that: (I am just guessing at some point you might want to move more things over)

https://medium.com/@visya/how-to-import-existing-aws-resources-into-cdk-stack-f1cea491e9

I know this is not the easiest solution but sadly for the moment this is the best we can do!

profile picture
answered a year ago
  • Hi Emma,

    Thanks for your response, will review the links you provided.

    I'd just like to point out that the example provided in this documentation below does not work: https://docs.aws.amazon.com/step-functions/latest/dg/tutorial-lambda-state-machine-cdk.html

    It would also be great if there was a fully worked out example or tutorial for a python project for a multi-step step function in the cdk documentation.

    Thanks!

  • I will take a look at this link around to see if there are any good tutorials! I will get back to you asap!

1

Hi, on top of what Emma has mentioned above, you can even just reference existing Lambda functions by leveraging CDK native methods starting with "from", as they typically indicate that they can import an pre-existent resource into CDK.

For Lambda an example is fromFunctionArn, available here: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Function.html

Hope it helps ;)

profile picture
EXPERT
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions