Day 43: S3 Programmatic Access with AWS CLI

Day 43: S3 Programmatic Access with AWS CLI


4 min read

Hi, I hope you had a great day yesterday. Today as part of the #90DaysofDevOps Challenge we will be exploring the most commonly used service in AWS i.e. S3.

What is S3?

Amazon Simple Storage Service (Amazon S3) is an object storage service provided by Amazon Web Services (AWS). It offers scalable, durable, and secure storage for a wide range of data types, including documents, images, videos, backups, and more.

Key Features

  • Amazon S3 can store an unlimited amount of data, and you can increase or decrease your storage as needed.

  • Data stored in S3 is redundantly stored across multiple facilities and is designed to provide 99.999999999% durability.

  • S3 provides various security features, including access control lists (ACLs), bucket policies, and integration with AWS Identity and Access Management (IAM).

  • You can enable versioning for your buckets, which allows you to preserve, retrieve, and restore every version of every object stored in a bucket.

  • S3 supports both server-side encryption and client-side encryption to protect data at rest and during transit.


Launch an EC2 instance using the AWS Management Console and connect to it using Secure Shell (SSH).

Create an S3 bucket and upload a file to it using the AWS Management Console.

Access the file from the EC2 instance using the AWS Command Line Interface (AWS CLI).

Step 1: Launch an EC2 Instance and Connect Using SSH:

  • Sign in to the AWS Management Console. Navigate to the EC2 service.

  • Click Launch Instance to create a new EC2 instance. Choose an Amazon Machine Image (AMI), such as Amazon Linux, and select an instance type.

  • Configure the instance details, add storage, and configure any additional settings as needed.

  • Add tags and configure security groups (ensure that SSH is allowed).

  • Create or select an existing key pair, which will be used for SSH access.

  • Launch the instance.

Step 2: Create an S3 Bucket and Upload a File:

  • Navigate to the S3 service in the AWS Management Console. Click Create bucket.

  • Choose a unique bucket name, select a region, and configure additional options if needed. Create the bucket.

  • Within the bucket, click Upload to add a file to the bucket.

  • Select the file from your local machine and configure any additional options.

  • Upload the file to the bucket.

Step 3: Access the File from the EC2 Instance Using AWS CLI:

  • Connect to your EC2 instance via SSH using the key pair you created during the instance launch.

      ssh -i your-key.pem ec2-user@your-instance-ip
  • Install the AWS CLI on your EC2 instance if it's not already installed:

      sudo yum install aws-cli
  • Configure the AWS CLI with your access key and secret key (you can find them in your AWS IAM account):

      aws configure
  • Provide your access key ID, secret access key, default region name, and default output format.

  • Use the AWS CLI to copy the file from the S3 bucket to your EC2 instance:

      aws s3 cp s3://your-bucket-name/your-file.txt /path-to-destination/your-file.txt
  • You can now access the file on your EC2 instance.

Make sure you replace your-key.pem, your-instance-ip, your-bucket-name, and other placeholders with your actual values.


Create a snapshot of the EC2 instance and use it to launch a new EC2 instance.

Download a file from the S3 bucket using the AWS CLI.

Verify that the contents of the file are the same on both EC2 instances.

Step 1: Create a Snapshot of the EC2 Instance and Launch a New Instance:

  • Navigate to the AWS Management Console. Go to the EC2 service.

  • Select "Instances" from the left navigation pane. Locate the EC2 instance for which you want to create a snapshot.

  • Right-click on the instance and choose Create Image. Provide a name and description for the image, and click Create Image. This will initiate the snapshot creation process.

  • Once the snapshot is created, navigate to the AMIs section in the EC2 service.

  • Find the newly created Amazon Machine Image (AMI) and use it to launch a new EC2 instance.

  • Configure the new instance as needed.

Step 2: Download a File from the S3 Bucket Using the AWS CLI:

  • Connect to your EC2 instance via SSH.

  • Ensure the AWS CLI is installed and configured on the new EC2 instance (you should have configured it in a previous task).

  • Use the AWS CLI to download the file from the S3 bucket. For example:

      aws s3 cp s3://your-bucket-name/your-file.txt /path-to-destination/your-file.txt

    Replace your-bucket-name and the file paths with your actual values.

Step 3: Verify the Contents of the File on Both EC2 Instances:

  • Once the file is downloaded to the new EC2 instance, you can compare its contents to the original EC2 instance. You can use commands like diff or md5sum to compare files for equality.

      diff /path-to-destination/your-file.txt /path-to-original/your-file.txt
  • If the files are identical, there will be no output from the diff command.

<That's all for today. Hope you like it. FOLLOW to join me in the journey of DevOps>