I'm using a CloudFormation template to boot up a custom windows AMI, which needs to retrieve some code from an S3 bucket and execute it, once it boots up.
After quickly being able to do it manually using the AWS CLI (s3 sync
) I've been struggling to get this to work via CloudFormation for over 8 hours now, to no avail. In essence, this command always fails with:
"NoCredentialsError: Unable to locate credentials"
Initially I tried setting the /.aws/credentials
and /.aws/config
files, in many different ways, until I finally realized that the user identity running cfn-init.exe
and it's child processes doesn't have access to these files at all and that won't work. So instead I've opted for setting environment variables using setx
, but that doesn't seem to work either and I'm still getting the same error.
I'm getting quite frustrated with this as every test requires a change to the CF template, uploading to S3, creating a stack, waiting a good 5-10 minutes for it to finish bootstrapping only to RDP and find out it failed, again.
Here's the init > config
portion of my template:
"commands" : {
"0-Tester" : {
"command" : "echo \"I am OK.\" > \"d:\\test.txt\""
},
"1-SetAK" : {
"command" : { "Fn::Join" : ["", [
"setx AWS_ACCESS_KEY_ID ",
{ "Ref": "AutomationUserAK" }
]] }
},
"2-SetSK" : {
"command" : { "Fn::Join" : ["", [
"setx AWS_SECRET_ACCESS_KEY ",
{ "Ref": "AutomationUserSK" }
]] }
},
"3-Pullcode" : {
"command" : "aws s3 sync s3://some-s3-bucket d:/dev/ --debug"
}
}
The first 3 commands (Tester, SetAK, SetSK) work just fine. The last one (Pullcode) fails, every time.
So at this point I'm assuming I'm going at it wrong. Maybe I need to configure a specific IAM for the CF stack, maybe I should use setx ... /M
, maybe a million other options, but since this trial and error has been going for the entire length of my work day and then some I figured it won't hurt to ask.