AWS Windows Powershell ECR Login - amazon-web-services

I am new to AWS and Docker. I am trying to setup AWS ECR and docker and trying to retrieve ECR Login using windows powershell. I am trying to use the command -
Invoke-Expression -Command (aws ecr get-login)
which gives me the error
My problem is it is trying to use the ccuser on its own. I don't think I have configured it to use this user. I have created a separate user with AmazonEC2ContainerRegistryFullAccess. How do I configure this as the user for AWS Powershell to execute the command?

aws ecr get-login will simply use the creds that you've already setup for the AWS CLI. If you want to change the creds for the CLI, use aws configure to do the setup again, it will ask you for:
AWS Access Key ID []:
AWS Secret Access Key []:
Default region name []:
Default output format []:
If you only want to use that user temporarily without reconfiguring your existing account, here are the docs for doing that.

simple and easy, I was debugging this for while but somehow it worked
aws ecr get-login-password --region ap-south-1 | docker login --username AWS --password-stdin ecr.amazonaws.com

Related

AWS Public Repository Push Image issue

I have created one ECR repository as public. Now, from my on-premises docker server, I build the image and I wanted to push the image in AWS ECR as public image. AWS has given option view push option but It did not work, getting below error while running the below command.
**docker login -u AWS -p $(aws ecr get-login-password --region ap-northeast-2)
public.ecr.aws/m8r0s3o9**
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Error response from daemon: login attempt to https://public.ecr.aws/v2/ failed with status: 400 Bad Request
For private repository it works fine for me.
Any suggestion would be highly appreciable, do i need to add any role/policy to my aws user?
Thanks for your feedback guidance.
I found the issue, I was referring "view push command instructions" where respective region show in the command.
But for public repository need to run below command always.
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/<your repo name>
so in short, When authenticating to a public registry, always authenticate to the us-east-1 Region when using the AWS CLI.
It resolved my issue and i was able to push the docker images in ECR. Rest command are same.

AWS ECR trying to use Get-ECRLoginCommnad fails

I am new to AWS and I am trying to register an image on ECR on windows. To do that I am using PowerShell to connect to AWS.
Below is my version
PS C:\> aws --version
aws-cli/2.0.0 Python/3.7.5 Windows/10 botocore/2.0.0dev4
I used aws configure command to login. I went to users -> createdUser -> Security Credentials for Access key and Secret key.
When I use Get-ECRLoginCommand
PS C:\> Get-ECRLoginCommand
Get-ECRLoginCommand : The request signature we calculated does not match the signature you provided. Check your AWS
Secret Access Key and signing method. Consult the service documentation for details.
At line:1 char:1
+ Get-ECRLoginCommand
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Amazon.PowerShe...inCommandCmdlet:GetECRLoginCommandCmdlet) [Get-ECRL
oginCommand], InvalidOperationException
+ FullyQualifiedErrorId : Amazon.ECR.AmazonECRException,Amazon.PowerShell.Cmdlets.ECR.GetECRLoginCommandCmdlet
Can someone please help me how to registry docker image on ECR?
Even on Windows,
it's easier to use push instructions on Linux and macOS.
you just need to install AWS CLI, docker, and set up AWS credentials.
Install AWS CLI version 2
msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi
To confirm the installation
C:\> aws --version
aws-cli/2.2.43 Python/3.8.8 Windows/10 exe/AMD64 prompt/off
In AWS IAM, create user with the required role.
It's not recommended. I simply created a user with AdministratorAccess.
set up your AWS credentials
aws configure
AWS Access Key ID [None]: Access Key
AWS Secret Access Key [None]: Secret Key
Default region name [None]: us-west-2
Default output format [None]: json
Now, Authenticate the docker of the Amazon ECR registry
aws ecr get-login-password | docker login --username AWS --password-stdin YOUR-REGISTRY-URL
Login Succeeded
Finally, Pushing a Docker Image
docker build -t YOUR-BUILD-NAME .
docker tag YOUR-BUILD-NAME:latest YOUR-REGISTRY-URL/YOUR-BUILD-NAME:latest
docker push YOUR-REGISTRY-URL/YOUR-BUILD-NAME:latest
Try uninstalling AWS CLI V2 and install AWS CLI V1 that fixed the problem for me.
You need to first set up you aws credentials on your Windows PC.
Separately you need to set up it for aws cli and aws sdk:
from documentation:
Credentials Store Locations
The AWS Tools for PowerShell can use either of two credentials stores:
The AWS SDK store, which encrypts your credentials and stores them in your home folder. > In Windows, this store is located at: C:\Users\username\AppData\Local\AWSToolkit\RegisteredAccounts.json.
The AWS SDK for .NET and Toolkit for Visual Studio can also use the AWS SDK store.
The shared credentials file, which is also located in your home folder, but stores credentials as plain text.
By default, the credentials file is stored here:
On Windows: C:\Users\username.aws\credentials
On Mac/Linux: ~/.aws/credentials
The AWS SDKs and the AWS Command Line Interface can also use the credentials file. If you're running a script outside of your AWS user context, be sure that the file that contains your credentials is copied to a location where all user accounts (local system and user) can access your credentials.
Example:
PS > Set-AWSCredential `
-AccessKey AKIA0123456787EXAMPLE `
-SecretKey wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY `
-StoreAs MyNewProfile
Source: https://docs.aws.amazon.com/powershell/latest/userguide/specifying-your-aws-credentials.html#specifying-your-aws-credentials-store
Try this
Get-AWSCredential -ListProfileDetail
If you get something like this(2 defaults)
The 1st default is causing the issue! you should remove it with the following command
Remove-AWSCredentialProfile -ProfileName default
Once you only have a single default left(the working one) Get-ECRLoginCommand should work as expected.

login to docker AWS registry from GCP

I would like to login to an AWS registry from my GCP instance. I have transferred the AWS ID and secret key to the instance via the GCP metadata server and can access them from my script as:
getMetadata() {
curl http://metadata/computeMetadata/v1/instance/attributes/$1 \
-H "Metadata-Flavor: Google"
}
aws_access_key_id=`getMetadata aws_access_key_id`
aws_secret_access_key=`getMetadata aws_secret_access_key`
Now usually locally I can get the AWS login as:
$(aws ecr get-login --no-include-email --region eu-central-1)
I was wondering if there is a way to login to the docker registry from the shell scrip by specifying the AWS ID and the secret key through command line as it is more complicated for me to get the credentials file setup as I want during the GCP instance creation (due to the startup script running as root, for example).
You can pass the access key and secret to AWS CLI while getting the authorization token.
$(AWS_ACCESS_KEY_ID=my_key AWS_SECRET_ACCESS_KEY=my_secret aws ecr get-login --no-include-email --region eu-central-1)

"no basic auth credentials" when trying to pull an image from a private ECR

I have the following line somewhere in the middle of my Dockerfile to retrieve an image from my private ECR.
FROM **********.dkr.ecr.ap-southeast-1.amazonaws.com/prod/*************:ff03401
This is the error that I get in AWS Codebuild when trying to build this:
Step 21/36 : FROM **********.dkr.ecr.ap-southeast-1.amazonaws.com/prod/*************:ff03401
Get https://**********.dkr.ecr.ap-southeast-1.amazonaws.com/prod/*************/manifests/ff03401: no basic auth credentials
How can one provide these credentials in the most secure way, and in a way that can also be terraformed?
There are multiple ways to do it.
Using aws access and secret key. In which you set the aws credentials on the ec2 machine and run ecr login command. aws ecr get-login --no-include-email --registry-ids <some-id> --region eu-west-1 and then docker pull should work. But this is not a recommended secure way.
What I prefer is using aws iam roles.
Assuming you want to pull this image on your ec2 machine that was brought up using terraform. Make use of iam roles.
Create an iam role manually or using terraform iam resource.
For contents of iam policy refer this.
While bringing ec2 using terraform instance resource make use of iam_instance_profile attribute, the value of this attribute should be the name of iam role you created.
This should be enough to automatically pull docker images from ECR in a secure way.
Hope this helps.

Pull images from AWS ECR on AWS EC2 without using docker login but using EC2 Instance role and ECR repository permissions

Can we pull images from AWS ECR Repository on an AWS EC2 instance running docker assigning AWS EC2 instance role/policy and AWS ECR Repository permission that provides access to ECR.
I have currently provided all permissions but the error I am getting is "unauthorized: authentication required".
Let me know if this is possible.
you can actually skip the docker login step, even aws ecr get-token which still did the docker login, using ecr credential helper.
with the helper, just config the docker:
{
"credHelpers": {
"aws_account_id.dkr.ecr.region.amazonaws.com": "ecr-login"
}
}
refer to: https://lwpro2.wordpress.com/2019/10/30/authenticating-amazon-ecr-repositories-for-docker-cli-with-credential-helper/
Run the below command in your cron and cron will refresh your login credentials.
COMMAND=`eval aws ecr get-login --region us-west-2`
echo `eval $COMMAND`
So you can avoid any login to ecr itself and access seamlessly all the time.