Search code examples
pythonboto3amazon-ecr

Getting InvalidARN, How to get ARN of ECR image?


I am trying to create a new tag or an image, for which I am using boto3.client('ecr').tag_resource

response = ecr.tag_resource(
            resourceArn=f'arn:aws:ecr:{region_name}:{account_id}:image/{repository_name}@{image_digest}',
            tags=[{'Key': 'tag-key', 'Value': image_tag}]
        )

Resulting output InvalidParameterException:

File ~/Documents/NuTest_Prac/nu_py3/nutest-py3/env/lib/python3.9/site-packages/botocore/client.py:530, in ClientCreator._create_api_method.<locals>._api_call(self, *args, **kwargs)
    526     raise TypeError(
    527         f"{py_operation_name}() only accepts keyword arguments."
    528     )
    529 # The "self" in this scope is referring to the BaseClient.
--> 530 return self._make_api_call(operation_name, kwargs)

File ~/Documents/NuTest_Prac/nu_py3/nutest-py3/env/lib/python3.9/site-packages/botocore/client.py:964, in BaseClient._make_api_call(self, operation_name, api_params)
    962     error_code = parsed_response.get("Error", {}).get("Code")
    963     error_class = self.exceptions.from_code(error_code)
--> 964     raise error_class(parsed_response, operation_name)
    965 else:
    966     return parsed_response

InvalidParameterException: An error occurred (InvalidParameterException) when calling the TagResource operation: Invalid parameter at 'resourceArn' failed to satisfy constraint: 'Invalid ARN'

I explored much but could not find the ARN information which could help me create or get the ARN which is valid. Can someone help me fix this?


Solution

  • The tag_resource method is used for tagging AWS resources and does not have anything to do with image tags. If you want to tag an image you need to use the put_image method:

    # Get the image's details to get its manifest
    get_image_response = ecr.batch_get_image(
        registryId=account_id,
        repositoryName=repository_name,
        imageIds=[{"imageDigest": image_digest}]
    )
    if get_image_response["images"]:
        manifest  = get_image_response["images"][0]["imageManifest"]
        manifest_media_type = get_image_response["images"][0]["imageManifestMediaType"]
        # Tag the image
        response = ecr.put_image(
            registryId=account_id,
            repositoryName=repository_name,
            imageManifest=manifest,
            imageManifestMediaType=manifest_media_type,
            imageTag=image_tag,
            imageDigest=image_digest
        )
    else:
        print("Image not found")