How to upload/download to S3 from a EC2 instance

How to upload/download to S3 from a EC2 instance

1. Create a separate AWS user

I recommend creating a separated user for uploading to your S3 bucket.

  1. Select the AWS Service IAM -> Users -> Add user
  2. User name: s3_user
  3. Permission options: Attach policies directly
  4. Select AmazonS3FullAccess
  5. Click Next -> Create user

2. Configure AWS user

We need to create an access_key and secret_key in order to use our s3_user within the aws command.

  1. Select your s3_user and go to Security credentials
  2. Under Access keys click Create access key
  3. Choose Application running on an AWS compute service
  4. Click Create access key
  5. Store the Access key and Secret access key

3. Configure AWS CLI

aws configure --profile s3_user
AWS Access Key ID [None]: <your_access_key>
AWS Secret Access Key [None]: <your_secret_access_key>
Default region name [None]: eu-central-1
Default output format [None]: json

4. Upload file to S3

Now you can upload a file to your S3 bucket using your new profile

aws s3 cp /opt/test.txt s3://mybucketname/test.txt --profile s3_user

5. Download file from S3

aws s3 cp s3://mybucketname/test.txt /opt/test.txt --profile s3_user

6. Conclusion

In this article we have learned how to configure an user and the aws cli to conveniently upload and download files from S3 to your EC2 instance.