0

I am trying to PUT files from EC2 to S3 using bash/curl and instance profile. I am using the following code:

instance_profile=`curl http://169.254.169.254/latest/meta-data/iam/security-credentials/`
aws_access_key_id=`curl http://169.254.169.254/latest/meta-data/iam/security-credentials/${instance_profile} | grep AccessKeyId | cut -d':' -f2 | sed 's/[^0-9A-Z]*//g'`
aws_secret_access_key=`curl http://169.254.169.254/latest/meta-data/iam/security-credentials/${instance_profile} | grep SecretAccessKey | cut -d':' -f2 | sed 's/[^0-9A-Za-z/+=]*//g'
token=`curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/${instance_profile} | sed -n '/Token/{p;}' | cut -f4 -d'"'
file="test_file.txt"
bucket="MM-test-s3-bucket"
filepath="/${bucket}/${path}/${file}"
contentType="application/x-compressed-tar"
dateValue=`date -R`
signature_string="PUT\n\n${contentType}\n${dateValue}\n${filepath}"
signature_hash=`echo -en ${signature_string} | openssl sha1 -hmac ${aws_secret_access_key} -binary | base64`

curl -X PUT -T "${file}" -H "Host: ${bucket}.s3.amazonaws.com" -H "Date: ${dateValue}" -H "Content-Type: ${contentType}" -H "Authorization: AWS ${aws_access_key_id}:${signature_hash}" https://${bucket}.s3.amazonaws.com/${file}

I am getting an error "InvalidAccessKeyIdThe AWS Access Key Id you provided does not exist in our records."

6
  • What's the output of echo $aws_access_key_id? Commented Jul 12, 2022 at 12:22
  • It shows the access key starting with ASIA******** Commented Jul 12, 2022 at 12:32
  • 1
    Since the access key starts with ASIA, it's a temporary credentials. You also need to include the security token in the request. Commented Jul 12, 2022 at 12:48
  • Thanks! That issue is gone. Now getting the error "The request signature we calculated does not match the signature you provided. Check your key and signing method." Commented Jul 12, 2022 at 13:39
  • This is probably related to this: stackoverflow.com/questions/30518899/… Commented Jul 13, 2022 at 3:25

1 Answer 1

2

As mentioned in the questions' comment, we need to use token when using temporary access keys and secret key.

Also, we need to add this token in our stringToSign and headers in curl request.

stringToSign="PUT\n\n${contentType}\n${dateValue}\n${CanonicalizedAmzHeaders}\n${filepath}".

Where this CanonicalizedAmzHeaders is - CanonicalizedAmzHeaders="x-amz-security-token:${token}"

and in header in curl request in below format- -H "x-amz-security-token: ${token}"

else you will face error "The request signature we calculated does not match the signature you provided"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.