Amazon S3 is a highly scalable, secure, and durable object storage service that can store and retrieve any amount of data from anywhere on the web. With AWS CLI, you can easily interact with S3 programmatically, and automate various tasks like creating buckets, uploading and downloading files, and more.
In this blog, we will explore how to access S3 programmatically with AWS CLI, and perform various tasks like creating a bucket, uploading a file, downloading a file, and more.
Task-01: Accessing S3 from EC2 instance using AWS-CLI
Step 1: Launch an EC2 instance using the AWS Management Console and connect to it using Secure Shell (SSH)
Log in to the AWS Management Console and navigate to the EC2 Dashboard.
Click on the "Launch Instance" button and select your preferred Amazon Machine Image (AMI).
Choose your instance type, configure your instance details (such as VPC, subnet, security groups, etc.), and add storage as per your requirements.
Review your instance settings and launch the instance.
Once the instance is running, connect to it using Secure Shell (SSH) and the private key file that you created during the launch process.
Step 2: Create an S3 bucket and upload a file to it using the AWS Management Console
Log in to the AWS Management Console and navigate to the S3 service.
Click on the "Create Bucket" button and provide a unique name for your bucket, choose your preferred region, and click on "Create".
Once your bucket is created, navigate to it and click on the "Upload" button.
Select the file that you want to upload to your S3 bucket and click on "Upload".
Wait for the file to be uploaded successfully.
Step 3: Access the file from the EC2 instance using the AWS Command Line Interface (AWS CLI)
Connect to your EC2 instance using SSH.
Install AWS CLI by running the following command:
sudo apt-get update sudo apt-get install awscli
Once AWS CLI is installed, configure it by running the following command:
aws configure
Enter your AWS Access Key ID, AWS Secret Access Key, default region name, and default output format when prompted.
To access the file from your S3 bucket, run the following command:
aws s3 cp s3://your-bucket-name/your-file-name /path/to/local/file
Replace "your-bucket-name" with the name of your S3 bucket and "your-file-name" with the name of the file that you uploaded to your S3 bucket.
Replace "/path/to/local/file" with the path on your EC2 instance where you want to save the file.
Task-02: Creating a snapshot of EC2 instance, launching new EC2 instance, and downloading a file from S3 bucket using AWS-CLI
Step 1: Create a snapshot of the EC2 instance
Connect to your EC2 instance using SSH.
Stop the instance by running the following command:
sudo poweroff
Once the instance is stopped, navigate to the EC2 Dashboard in the AWS Management Console.
Select the instance that you want to create a snapshot of and click on the "Create Image" button.
Provide a name and description for your image, and click on "Create Image".
Wait for the image creation process to complete.
Step 2: Use the snapshot to launch a new EC2 instance
Navigate to the "AMIs" section in the EC2 Dashboard.
Select the image that you created in Step 1 and click on "Launch".
Choose your instance type, configure your instance details (such as VPC, subnet, security groups, etc.), and add storage as per your requirements.
Review your instance settings and launch the instance.
Step 3: Download a file from the S3 bucket using the AWS CLI
Connect to your new EC2 instance using SSH.
Install AWS CLI by running the following command:
sudo apt-get update sudo apt-get install awscli
Once AWS CLI is installed, configure it by running the following command:
aws configure
Enter your AWS Access Key ID, AWS Secret Access Key, default region name, and default output format when prompted.
To download a file from your S3 bucket, run the following command:
aws s3 cp s3://your-bucket-name/your-file-name /path/to/local/file
Replace "your-bucket-name" with the name of your S3 bucket and "your-file-name" with the name of the file that you uploaded to your S3 bucket.
Replace "/path/to/local/file" with the path on your EC2 instance where you want to save the file.
Verify that the contents of the file are the same on both EC2 instances by comparing the files using the "diff" command.
Congratulations!🥳🥳🥳🥳🥳 We have successfully completed the tasks of creating an S3 bucket, uploading a file to it, accessing the file from an EC2 instance using the AWS CLI, creating a snapshot of the EC2 instance, launching a new instance from the snapshot, downloading a file from the S3 bucket using the AWS CLI, and verifying the contents of the file on both EC2 instances.