Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Granting access to pull a DockerImageAsset #5983

Closed
misterjoshua opened this issue Jan 27, 2020 · 6 comments
Closed

Granting access to pull a DockerImageAsset #5983

misterjoshua opened this issue Jan 27, 2020 · 6 comments
Assignees
Labels
@aws-cdk/assets Related to the @aws-cdk/assets package @aws-cdk/aws-ecr Related to Amazon Elastic Container Registry docs/generated Related to the generated API Reference documentation feature-request A feature should be added or improved. guidance Question that needs advice or information.

Comments

@misterjoshua
Copy link
Contributor

misterjoshua commented Jan 27, 2020

https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ecr-assets.DockerImageAsset.html

It doesn't seem clear how to grant services (like ECS) access to pull the docker image asset. An example would be helpful, as DockerImageAsset doesn't seem to have the same grant methods as many other constructs in the CDK.


This is a 📕 documentation issue

@misterjoshua misterjoshua added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Jan 27, 2020
@SomayaB SomayaB added @aws-cdk/assets Related to the @aws-cdk/assets package @aws-cdk/aws-ecr Related to Amazon Elastic Container Registry labels Jan 28, 2020
@SomayaB SomayaB added the guidance Question that needs advice or information. label Jan 28, 2020
@MrArnoldPalmer MrArnoldPalmer added docs/generated Related to the generated API Reference documentation p1 and removed guidance Question that needs advice or information. needs-triage This issue or PR still needs to be triaged. labels Jan 30, 2020
@MrArnoldPalmer
Copy link
Contributor

@misterjoshua an instance of DockerImageAsset has a property repository which is of type IRepository. From here you have access to the grant, grantPull, and grantPullPush methods.

Keeping this open to track adding an example to the aws-ecr-assets repository readme.

@peterjuras
Copy link

How do you grant permissions to ecs.AssetImage types? I'm using docker image assets from ecs, but they don't appear to have a repository property.

My code looks similar to this:

const image = new ecs.AssetImage(path.join(...)),
const container = taskDefinition.addContainer("container-name", {
  image: image,
  // ...
});
// How to add pull permissions to the aws-cdk/assets ecr repository without hardcoding it?

@eladb
Copy link
Contributor

eladb commented Feb 4, 2020

@misterjoshua @peterjuras can you guys please provide some more information about your use case? Why do you need to grant pull permissions to these images?

@eladb eladb added guidance Question that needs advice or information. and removed p1 labels Feb 4, 2020
@peterjuras
Copy link

Hi,

Sorry for not updating this, it was actually working automatically after some retries of starting the ecs task.

Is there a delay on when the permissions are being applied by cdk?

eladb pushed a commit that referenced this issue Feb 4, 2020
Allow using an existing `DockerImageAsset` object as a container image in order to enable direct access to `DockerImageAsset`s API such as accessing the ECR repository, the source hash or granting permissions.

The reason this could not have been exposed through the normal `fromImageAsset` is that `ContainerImage` can be used multiple times (i.e. be bound to multiple container definitions), so there is no reliable way to allow users to access the asset.

Related to #5791 and #5983
@misterjoshua
Copy link
Contributor Author

misterjoshua commented Feb 4, 2020

@eladb I was getting an access denied error when ECS was attempting to pull the image during deployment.

const nodeImage = new ecrAssets.DockerImageAsset(this, "Image", {
  directory: "../context"
});
const task = new ecs.TaskDefinition(this, 'Task', {
  compatibility: ecs.Compatibility.EC2,
  family: "MyTask",
});
nodeImage.repository.grantPull(task.obtainExecutionRole()); // This fixed the permissions error
const nodeContainer = task.addContainer("main", {
  image: ecs.ContainerImage.fromEcrRepository(nodeImage.repository, nodeImage.sourceHash),
//...
});

Your commit seems like it would solve my problem.

eladb pushed a commit that referenced this issue Feb 5, 2020
Allow using an existing `DockerImageAsset` object as a container image in order to enable direct access to `DockerImageAsset`s API such as accessing the ECR repository, the source hash or granting permissions.

The reason this could not have been exposed through the normal `fromImageAsset` is that `ContainerImage` can be used multiple times (i.e. be bound to multiple container definitions), so there is no reliable way to allow users to access the asset.

Related to #5791 and #5983
@eladb eladb closed this as completed Feb 9, 2020
@0cv
Copy link

0cv commented Jun 20, 2024

This doesn't seem necessary anymore to use grantPull(...) as it's been fixed in the referenced commit.

However for anyone coming here, I still got the same error and the reason was because the Image Repository was set with immutable tags. Changing that setting fixed my issue.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/assets Related to the @aws-cdk/assets package @aws-cdk/aws-ecr Related to Amazon Elastic Container Registry docs/generated Related to the generated API Reference documentation feature-request A feature should be added or improved. guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests

6 participants