I would like to upload a docker image from local disc to a repository that I created with the AWS CDK.
When i use aws_ecr_assets.DockerImageAsset to add a Docker image to a repository (which works fine except that I am not able to set permissions on its repository via CDK), I get the following deprecation warning:
DockerImageAsset.repositoryName is deprecated. Override "core.Stack.addDockerImageAsset" to control asset locations
When looking into core.Stack.addDockerImageAsset, I get a hint that I should override stack.synthesizer.addDockerImageAsset().
My simplified stack with a custom synthesizer looks like this:
class CustomSynthesizer(core.LegacyStackSynthesizer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def add_docker_image_asset(
self,
*,
directory_name,
source_hash,
docker_build_args=None,
docker_build_target=None,
docker_file=None,
repository_name=None,
):
# What should I put in this function to upload the docker image into my repository?
class ContainerStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, synthesizer=CustomSynthesizer(), **kwargs)
repo = ecr.Repository(self, "TestRepo", repository_name="test_repo")
repo.grant_pull(iam.AccountPrincipal("123456789123"))
image_location = self.synthesizer.add_docker_image_asset(
directory_name="path/to/dir/with/Dockerfile",
source_hash="latest",
repository_name=repo.repository_name,
)
Another thing that I tried is to use the standard stack synthesizer and calling add_docker_image_asset on it. Unfortunately, I get the following error message and the stack fails to deploy:
test-stack: deploying...
[0%] start: Publishing latest:current
[25%] fail: Unable to execute 'docker' in order to build a container asset. Please install 'docker' and try again.
...
❌ test-stack failed: Error: Failed to publish one or more assets. See the error messages above for more information.
at Object.publishAssets (/home/user/.npm-global/lib/node_modules/aws-cdk/lib/util/asset-publishing.ts:25:11)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at Object.deployStack (/home/user/.npm-global/lib/node_modules/aws-cdk/lib/api/deploy-stack.ts:216:3)
at CdkToolkit.deploy (/home/user/.npm-global/lib/node_modules/aws-cdk/lib/cdk-toolkit.ts:181:24)
at main (/home/user/.npm-global/lib/node_modules/aws-cdk/bin/cdk.ts:268:16)
at initCommandLine (/home/user/.npm-global/lib/node_modules/aws-cdk/bin/cdk.ts:188:9)
Failed to publish one or more assets. See the error messages above for more information.
I'm flat on my back as to how to solve this problem and any help is much appreciated!