Stack Exchange
CDK ECRDeployment Source image does not exist. Provide a valid source image
I have a relatively simple CDK code
import * as cdk from 'aws-cdk-lib';
import { aws_s3 as s3, 
  aws_lambda as lambda, 
  aws_apigateway as apigateway, 
  aws_ecr_assets as ecr_assets, 
  aws_ecr as ecr,
  aws_ec2 as ec2,
  aws_efs as efs,
  aws_iam as iam,
  aws_elasticbeanstalk as ebs,
  aws_codecommit as cc,
  aws_codebuild as cb,
  aws_codestarconnections as csc,
  aws_codepipeline as cpl,
  aws_codepipeline_actions as cpla,
  aws_s3_assets as s3assets,
  aws_dynamodb as db
 } from 'aws-cdk-lib';
import * as ecrdeploy from 'cdk-ecr-deployment';

function docker_img_asset(scope: Construct, id: string, props: ecr_assets.DockerImageAssetProps):ecr_assets.DockerImageAsset{
  const img_dir = 'docker_images/' + id + '/';
  const p:ecr_assets.DockerImageAssetProps = {
    ...props,
    exclude:excludes_for(id).concat((props.exclude??[]).map(x=>img_dir+x)),
    file: img_dir + (props.file ?? 'Dockerfile'),
  };
  return new ecr_assets.DockerImageAsset(scope, id+'_img', p);
}


const xxx_uber_ecr_repo = new ecr.Repository(this, 'xxx-uber-docker-img-repo', {
  repositoryName: 'xxx-uber-docker-img-repo',
  removalPolicy: cdk.RemovalPolicy.DESTROY,
  lifecycleRules: [{
      description: "Keeps a maximum number of images to minimize storage",
      maxImageCount: 1
  }],
});

const xxx_ecr_img = docker_img_asset(this, 'xxx', {
  directory: ROOT_DIR,
  exclude:[],
  invalidation: {
    buildArgs: false,
    buildSecrets: false
  },
});
const xxx_uber_ecr_deployment = new ecrdeploy.ECRDeployment(this, 'xxxUberEcrDeployment', {
    src: new ecrdeploy.DockerImageName(xxx_ecr_img.imageUri),
    dest: new ecrdeploy.DockerImageName(`${xxx_uber_ecr_repo.repositoryUri}:${xxx_ecr_img.assetHash}`),
});

const xxx_lambda = new lambda.DockerImageFunction(this, "xxx_lambda", {
  code: lambda.DockerImageCode.fromEcr(xxx_uber_ecr_repo, {
    tagOrDigest: xxx_ecr_img.imageTag
  }),
  environment: {
    // 'BUCKET': packaged_blender_files.bucketName
  },
  memorySize: 2048,
  // ephemeralStorageSize: cdk.Size.mebibytes(10240),
  timeout: cdk.Duration.minutes(15)
});

It builds docker and pushes it to ECR successfully. But then it fails
AppStack | 8:39:53 PM | CREATE_FAILED        | AWS::Lambda::Function                     | xxx_lambda (xxxlambda9C8872F5) Resource handler returned message: "Source image xxxxxxxxxxxxx.dkr.ecr.eu-west-1.amazonaws.com/xxx-uber-docker-img-repo:5e0b247c90c24e68403f249749b74ca94828ccf405300d7984438fd10f1ea7ca does not exist. Provide a valid source image. (Service: Lambda, Status Code: 400, Request ID: xxxxxxxxxx)" (RequestToken: xxxxxxxxx, HandlerErrorCode: InvalidRequest)

If I run cdk deploy a second time, then everything works just fine. But as soon as I introduce any changes in my docker image causing it to rebuild and push to ECR again, then the problem shows up again.

Add a dependsOn between the lambda and the ecr deployment. CDK might not be inferring the dependency and is trying to deploy both simultaneously.
