Authenticating against AWS ECR using CLI on Windows

Authenticating against AWS ECR using CLI on Windows

Problem

Today I was playing around with AWS ECR trying to build my own docker image and push it into ECR repository. As soon as you create a repository on ECR, there is View push commands button on the top-right side of the window. Once you press the button it shows you push commands for macOS/Linux and Windows. Since I am a Windows user, I followed the instructions for Windows. However, the command didn't work I got this error:

Get-ECRLoginCommand : The term 'Get-ECRLoginCommand' is not recognized as the name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
At line:1 char:2
+ (Get-ECRLoginCommand).Password | docker login --username AWS --passwo ...
    + CategoryInfo          : ObjectNotFound: (Get-ECRLoginCommand:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Basically, it's saying I don't have Get-ECRLoginCommand installed on my Windows PowerShell. However, there is no link to documentation about instructions on installing this command.

Solution

  1. Start your PowerShell and install modularized AWS.Tools package

    Install-Module -Name AWS.Tools.Installer
    

    If you are notified that the repository is "untrusted", it asks you if you want to install anyway. Enter y to allow PowerShell to install the module. To avoid the prompt and install the module without trusting the repository, you can run the command with the -Force parameter.

    Install-Module -Name AWS.Tools.Installer -Force
    
  2. We can now install the module for each AWS service that we want to use by using the Install-AWSToolsModule cmdlet.. In our case we need ECR module, we can install it using below command:

    Install-AWSToolsModule AWS.Tools.ECR
    

Now it should work and we should be able to push our docker images using the command from ECR console. The other AWS.Tools for PowerShell can also be installed in the same way. For example, to install AWS.Tools for S3 and EC2 we would run the following command

Install-AWSToolsModule AWS.Tools.S3, AWS.Tools.EC2

References:

Installing the AWS Tools for PowerShell on Windows