I installed the AWS command line interface on my Windows 7 box, and it worked immediately when I called commands from a DOS shell.
But DOS, the worst language ever invented, is hideous for any serious scripting. So, I would like to to use the AWS CLI from bash via cygwin.
In my case, the installed AWS CLI is the Windows version. In principle, that should not be a problem because Windows commands are executable from cygwin. (cygwin includes your Windows environmental variables, such as PATH, in its own environment.)
Unfortunately, when I first tried to execute an AWS CLI command from cygwin/bash, I got an error:
$ aws s3 cp code.tgz s3://xyz/
upload failed: .\code.tgz to s3://xyz/code.tgz
Unable to locate credentials
This error is likely because the AWS CLI is looking in the wrong directory for the credentials
file. On Windows, it expects that file to be in %UserProfile%.aws
and in unix in ~/.aws
.
One hack work around is that in my home directory I created a new file named config_credentials
which contains a union of the contents of that directory's files config
and credentials
. I then made a new Windows System env var named AWS_CONFIG_FILE
whose value is the path to config_credentials
. Success: AWS CLI commands issued from cygwin now work.
I am wondering if there is a better solution?
I am curious why AWS CLI initially failed to search in the correct home directory for the config
and credentials
files. I also wonder if there is a way to correct that (this would eliminate the need for the AWS_CONFIG_FILE
env var).
Here's a way that jumps through all the Microsoft hoops; it's tedious to do for setup, but works very cleanly once you're actually using the AWS CLI. You don't need to do anything weird in Cygwin until the very end, but you may need Windows admin privs depending on your computer policies.
Install the Windows version of the AWS CLI. (It's currently distributed as an
.msi
installer, so likely needs admin privs to install to%PROGRAMFILES%
.)Launch a good ol' CMD.EXE window. Do the
aws configure
step to make sure the Windows AWS CLI is installed and working, and that your Windows.aws
directory is created and populated with files.cd
around into your Cygwin home directory, but still in the CMD.EXE window.If you have a
.aws
in your Cygwin $HOME, delete it or move it out of the way.mklink /j .aws %USERPROFILE%\.aws
This creates an NTFS directory junction, which is halfway between a Unix hard link and a Unix symbolic link, works only for directories, and -- unfortunately -- is a built-in command to CMD.EXE, not an external binary of its own. This makes a sort-of-directory named.aws
in your Cygwin $HOME, linked to your Windows "home". (This command is why you had to run CMD.EXE. There's probably a way to do it inside of PowerShell, and I don't care.)Type
dir
to see that the.aws
junction exists and how CMD.EXE displays it.Exit CMD.EXE and wash your hands thoroughly with soap. You won't need to touch that again.
Start a Cygwin shell, of whatever combination you prefer. (mintty+bash, or whatever) Run
ls -lFd .aws
to see the newly-created junction -- note how the Cygwin DLL presents directory junctions as symlinks.You may need to adjust your PATH inside Cygwin to pick up the AWS CLI installation.
With the junction in place, using
aws
inside my Cygwin shell performs normally, and all tools are using the same config/credentials files, right where the (Windows) AWS CLI v2 installation expects to find them.The one minor hitch is that AWS CLI under Windows uses CRLF newlines in all its output, so if doing any scripting with
aws [...] --output text
, you need to pipe that throughtr -d '\r'
before doing anything with the results. Else you get the hidden carriage return stuck on the end of your lines, fouling things up at random times. (You can pipe AWS output throughcat -A
to make them visible for debugging.)