Introduction
Amazon Simple Storage Service (S3) is a highly scalable and durable object storage service provided by Amazon Web Services (AWS). In this blog post, we will explore how to leverage AWS Command Line Interface (AWS CLI) for programmatic access to S3. We'll cover two tasks that involve launching an EC2 instance, creating an S3 bucket, uploading and accessing files, creating EC2 snapshots, and downloading files from S3 using the AWS CLI.
Task-01: Setting Up EC2 Instance and S3 Bucket
Step 1: Launch an EC2 Instance
Log in to the AWS Management Console.
Navigate to the EC2 service.
Click on "Launch Instance" and follow the wizard to configure your EC2 instance.
Download the key pair and launch the instance.
Step 2: Connect to the EC2 Instance
Open a terminal window.
Use the downloaded key pair to SSH into the EC2 instance:
ssh -i path/to/keypair.pem ec2-user@<your-instance-ip>
Step 3: Create an S3 Bucket and Upload a File
In the AWS Management Console, go to the S3 service.
Click "Create Bucket" and follow the prompts to create a bucket.
Upload a file to the bucket.
Step 4: Access the File from the EC2 Instance using AWS CLI
- Install AWS CLI on the EC2 instance:
sudo yum install aws-cli -y
- Configure AWS CLI with your credentials:
aws configure
- Use the following command to copy the file from S3 to the EC2 instance:
aws s3 cp s3://your-bucket-name/your-file.txt /path/on/ec2/instance/
Task-02: Creating EC2 Snapshots and Downloading from S3
Step 1: Create a Snapshot of the EC2 Instance
In the AWS Management Console, navigate to the EC2 service.
Right-click on your running instance, select "Create Image," and follow the prompts to create a snapshot.
Step 2: Launch a New EC2 Instance from the Snapshot
- In the AMIs section of the EC2 service, launch a new instance using the created snapshot.
Step 3: Download a File from S3 Using AWS CLI
On the new EC2 instance, install and configure AWS CLI as in Task-01.
Use the following command to download the file from S3:
aws s3 cp s3://your-bucket-name/your-file.txt /path/on/ec2/instance/
Step 4: Verify Contents on Both EC2 Instances
- Compare the contents of the file on both EC2 instances:
diff /path/on/first/ec2/instance/your-file.txt /path/on/second/ec2/instance/your-file.txt
This command will show no output if the files are identical.
Conclusion
By following the detailed steps outlined in this blog post, you should now have a solid understanding of how to perform S3 programmatic access with AWS CLI. Whether you are managing EC2 instances, creating snapshots, or interacting with S3 buckets, the AWS CLI provides a powerful and flexible interface for automating your AWS workflows.
Follow me on LinkedIn.
Check out my GitHub profile.