I have an AWS CDK stack containing a Step Functions state machine and a lambda function. This lambda function uses the StartExecution API which requires the ARN of the state machine. I’m am unaware of how to acquire the ARN of the state machine since it is volatile and constantly changes.
I have tried creating a .env file next to the index.ts of the lambda function.
const stateMachine = new stepfunctions.StateMachine(this, 'my-state-machine', {
definition: waitState,
});
And writing the stateMachine.stateMachineArn
to that .env file using fs
from the CDK stack. The result written to the .env file is ${Token[TOKEN.1056]}
. This is the same result when logging to the console. From my understanding, the ARN is not available during the current "phase" of the CDK stack, but I don’t know how to get the ARN to the lambda function before the lambda function is also deployed.
2
Answers
Why don’t you pass
stateMachine.stateMachineArn
directly to the lambda function? You should be able to do it if it is part of the same stack, by exposing the ARN property from one of the Constructs and referencing it from the lambda.If it is in a different stack you may have to export it using
new CfnOutput
and import it in your stack usingcdk.Fn.importValue
You will have to pass it in as an environment variable to the lambda function if it is to be used inside lambda code.
I believe you are on the right track using an environment variable. Instead of creating a
.env
file on the fly during deployment as a means to pass the state machine ARN to the lambda function, I would use secrets manager or systems manager parameter store, make that a dependency and inject that into the functionsenvironment
variables.I am not sure without running code if the
<resource>.addDependency(<other resource>)
is needed but good to be aware of this if you run into any deployment order of operation issues…