To ensure our CI/CD pipeline can securely interact with Amazon ECR, we need to configure the appropriate access permissions in AWS IAM. This step involves creating an ECR Repository if one doesn’t already exist, then creating an IAM User and assigning the necessary policies.
fcj-workshop-app ECR Repository if it doesn’t already exist.We need an ECR Repository to store our container images. If you haven’t created it in the “Preparation” section, follow these steps. Afterward, we will retrieve this repository’s URI.
Log in to the AWS Console: Go to https://aws.amazon.com/console/ and sign in.
Navigate to the ECR service: In the search bar, type “ECR” and select Elastic Container Registry.

Check and Create Repository:
fcj-workshop-app yet, follow these steps to create one:fcj-workshop-app.

fcj-workshop-app repository from the preparation section, proceed to the next step.Select your Repository and Copy the URI:
fcj-workshop-app repository name.123456789012.dkr.ecr.ap-southeast-1.amazonaws.com/fcj-workshop-app (where 123456789012 is your AWS account ID and ap-southeast-1 is your Region).
Keep this URI handy. We will need to use it in later GitHub Actions configuration steps to specify the repository where the image will be pushed.
Instead of using a pre-existing policy, we will create a custom IAM Policy via the console to gain more granular control over the permissions the CI/CD user will have on ECR.
Navigate to the IAM service: In the AWS Console search bar, type “IAM” and select IAM.

Create a new Policy:

Choose service and actions:

CompleteLayerUploadInitiateLayerUploadPutImageUploadLayerPartBatchCheckLayerAvailabilityBatchGetImageDescribeImagesGetDownloadUrlForLayerGetAuthorizationToken action.
ap-southeast-1).fcj-workshop-app.GetAuthorizationToken resource (if this option is separate), leave it as Any or add a separate line for this action with Resource: * if necessary. (Typically, GetAuthorizationToken is not restricted to a specific repository resource but rather by account/region).
The selected permissions (`ecr:*` for specific actions) allow the user to perform image push and pull operations.
`ecr:GetAuthorizationToken`: Allows the user to obtain an authentication token to log Docker into ECR.
`Resource`: We specify that this policy applies only to your `fcj-workshop-app` repository, adhering to the principle of least privilege.
Add Tags (Optional): You can add tags for easier management. Click Next: Review policy.
Review and Create Policy:
fcj-ecr-ci-cd-policy.policy for CI-CD.
Creating a custom policy like this allows you more precise control over the permissions granted to a user or role. This is a better practice than assigning broader managed policies for CI/CD purposes.
Now we will create an IAM User and attach the custom policy fcj-ecr-ci-cd-policy we just created to this user.
Navigate to the IAM service: Ensure you are in the IAM dashboard.
Create a new User:

fcj-ecr-ci-cd-user.
Set permissions:
fcj-ecr-ci-cd-policy (the name of the policy you just created) and select this policy.
Review and Create User:

Save Access Key:
fcj-ecr-ci-cd-user you just created.
Choose Third-party service as the use case.
(Optional) Enter a description for the Access Key (e.g., for-github-actions).

The Secret Access Key is displayed ONLY ONCE immediately after creation. Make sure you copy both the Access Key ID and Secret Access Key and store them in a secure place. You will not be able to view this Secret Access Key again after closing this window. We will need to use them to configure GitHub Actions in later sections.
In a real production environment, using an IAM Role with OpenID Connect (OIDC) is the more recommended method for granting permissions to CI/CD. This approach avoids the need to store long-lived Access Keys, enhancing security. However, for simplicity in this workshop, we will use Access Keys.