I'm defining a Python 3.12 Lambda with AWS CDK and creating a CodePipeline that uses CodeBuild to upload the function's actual ZIP package. The Lambda is initially created with inline placeholder code, and a later pipeline action runs `aws lambda update-function-code` against the function name.
The deployment action sometimes fails with a "function does not exist" error. Retrying the same action manually succeeds, which suggests the Lambda is being created after the pipeline has already started. I tried adding a dependency to the pipeline using an imported function reference, but it did not change the behavior. What is the correct way to make the pipeline wait until the stack containing the Lambda has been created?
3 Answers
The fact that a retry works is a strong clue: the pipeline is starting before the Lambda’s stack has finished deploying. A `dependsOn` relationship inside one stack only orders resources in that stack; it does not automatically coordinate separate stacks or a pipeline execution.
A more CDK-native design would package the Lambda code as a CDK asset and let the Lambda resource deploy it directly. If CodeBuild must perform the update because of the required workflow, make the pipeline stack explicitly depend on the stack that owns the Lambda.
A dependency on an imported function reference won’t create the relationship you need. `Function.fromFunctionName()` is only a lookup/reference, so CDK doesn’t treat it as the resource that creates the Lambda. Also, CDK dependencies only control CloudFormation stack/resource deployment; they don’t directly pause a CodeBuild command once the pipeline is running.
The pipeline stack should depend on the stack that creates the Lambda. For example: `stackThatCreatesThePipeline.node.addDependency(stackThatCreatesTheLambda);` This makes CloudFormation create the Lambda stack first, so the pipeline won’t begin until the function exists. After that, the CodeBuild action can update the function code by name.
Be careful not to confuse “the Lambda resource has been created” with “a pipeline action has finished.” CloudFormation dependencies can guarantee the creation order, but they don’t make a later CodeBuild command wait for some arbitrary Lambda state. In this case, the missing function is caused by the pipeline stack being deployed before the stack that creates the function. Add the dependency between those stacks rather than between the pipeline and an imported function.

The Lambda asset-building portion was moved to another CodeBuild project because the code needs to be refreshed whenever the source repository changes. For now, the important fix is making the pipeline stack depend on the Lambda stack before the pipeline can run.