In this tutorial I am going to show you how to create an AWS IAM role so you can create access keys to use to copy files to and from AWS S3 buckets via the AWS Command Line Interface (CLI).
This is useful if you need to transfer files from S3 to an EC2 instance using the CLI.
- You have an AWS account
- You have an EC2 instance created
- You have access to IAM within the AWS Console
Creating an IAM Role
Step 1 – Open the IAM Console
Step 2 – Click on Users
Step 3 – Click ‘Add user’
Step 4 – Enter a unique user name
Step 5 – Check Programmatic Access
Step 6 – Click Next: Permissions
Step 7 – Click ‘Attach existing policies directly’
Step 8 – Enter S3 in the’ Filter policies’ field
Step 9 – Check ‘AmazonS3FullAccess’ or ‘AmazonS3ReadOnlyAccess’ depending on your requirements
Step 10 – Click Next: Review
Step 11 – Review and Click ‘Create user’
Step 12 – Download the key pair and store securely – don’t email, share them outside your organization – keep the keys confidential. You can only download them once.
In this tutorial I will show you how you can copy files from S3 to an Amazon Web Services EC2 instance using the AWS Command Line Interface (CLI).
The basic concept of the AWS CLI S3 commands is that you have two types of path arguments: LocalPath and S3Uri.
The LocalPath is the local file or directory. The S3Uri is the location of a S3 object or bucket written in the form S3://mybucket/myobjectkey.
In each command, one path argument represents the source and the other the destination.
- You have an EC2 instance running and can get access to it using SSH
- You have configured the AWS CLI on your instance using keys with read/write access to S3 – see How to Configure the AWS CLI
- You have a bucket or buckets set up containing some test files or objects
Copying a file to S3
aws s3 cp MyFile.txt s3://my-bucket/path/
Moving all files from S3
aws s3 mv s3://my-bucket/path ./MyDirectory --recursive
Removing a file from S3
aws s3 rm s3://my-bucket/path/MyFile.txt
Syncing current directory with a S3 bucket
aws s3 sync . s3://my-bucket/path
Delete all contents of a bucket
aws s3 rm s3://my-bucket/path --recursive
List all buckets
aws s3 ls
List contents of a bucket
aws s3 ls s3://bucket-name