IAM Roles are highly recommended because they are easy to manage and have limited usage durations. In this tutorial, we will create 2 IAM Roles for each EC2 instance to grant access to desired services: S3 and ECR.
For the EC2 instance in the development environment, it has the permission to:
For the EC2 instance in the production environment, it has the permission to:
Thus, we will need the following policies to:
Since Docker will push and pull Docker images to a private registry, and in this case, we are using ECR as a registry with a private repository, Docker needs credentials to push and pull images to and from ECR. When configuring IAM Policies, we will add the permission to retrieve authentication tokens.
On the home page:
IAM
On the IAM home page:
In Select a service, we will:
Elastic Container Registry
Next, we will select a few permissions in List and Read as shown in the image. Check Any in this account under Resources (Specific).
After that, enter some details for the policy:
ReadECRRepositoryContent
Allow pull images, describe repositories
Similarly, we will now create another policy for the ECR service with Write permissions.
Details:
WriteECRRepositoryContent
Allow push and delete images
Next, in the policy creation interface:
S3
Next, we will add List, Read, and Write permissions as follows:
In Resources, select Specific and check Any for both bucket and object.
Next, we will configure the details for this policy:
RWDS3Objects
Allow read, write and delete Objects in S3 Bucket
In the policy list, under Filter by Type, select Customer managed to check the policies.
After creating the policies, we will now assign them to IAM Roles, starting with the EC2 instance in the development environment. In the IAM console:
Next, in Select trusted entity:
In Add permissions:
Name the Role DevEC2
and create the Role.
Similarly, follow the same steps as creating the Role for EC2 in the development environment, but the permissions for this role will differ slightly.
ReadECRRepositoryContent
and RWDS3Object
Name the Role ProdEC2
and create the Role.
Finally, review the 2 IAM Roles we just created.