Skip to main content

Command Palette

Search for a command to run...

Day 43: S3 Programmatic access with AWS-CLI

Updated
5 min read

Today we have two tasks to perform.Let's get started

Task-01

  • 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).

we'll launch an EC2 instance using the AWS Management Console. Here's how:

  1. Log in to the AWS Management Console.

  2. Navigate to the EC2 service.

  3. Click on "Launch Instance" to start the instance creation wizard.

  4. Select the desired Amazon Machine Image (AMI) for your instance. Choose one that aligns with your requirements, such as a Linux or Windows image.

  5. Choose an instance type based on your workload needs.

  6. Configure the instance details, including the number of instances, networking settings, and storage options.

  7. Set up security groups to control inbound and outbound traffic to your instance.

  8. Review your instance settings and launch the instance.

Step 2: Connecting to the EC2 Instance

Once your EC2 instance is up and running, you need to establish an SSH connection to access it securely. Follow these steps:

  1. Retrieve the public IP or DNS of your EC2 instance from the EC2 dashboard.

  2. Open your preferred SSH client

  3. Use the SSH command along with the key pair associated with your EC2 instance to establish a secure connection. For example: ssh -i <key-pair>.pem ec2-user@<public-IP-or-DNS>

Congratulations! You are now connected to your EC2 instance.

Step 3: Creating an S3 Bucket

We'll create an S3 bucket to store and manage files. Here's how:

  1. Go to the AWS Management Console.

  2. Navigate to the S3 service.

  3. Click on "Create bucket" to start the bucket creation process.

  4. Give your bucket a unique name and choose the region where you want it to be located.

  5. Configure optional settings like versioning, logging, and encryption.

  6. Set up permissions to control access to your bucket.

  7. Review your settings and create the bucket.

Step 4: Uploading a File to S3

With your S3 bucket ready, you can now upload a file to it using the AWS Management Console. Follow these steps:

  1. Open your S3 bucket in the AWS Management Console.

  2. Click on the "Upload" button to choose a file from your local machine.

  3. Select the file you want to upload and click "Open" to initiate the upload process.

  4. Monitor the progress as the file is uploaded to your S3 bucket.

Step 5: Accessing the S3 File from the EC2 Instance

To access the file we uploaded to S3 from our EC2 instance, we'll use the AWS Command Line Interface (AWS CLI). Here's how:

  1. Install the AWS CLI on your EC2 instance if it's not already installed.

    sudo apt install awscli

  2. Configure the AWS CLI with your AWS credentials using the command: aws configure

  3. Enter your access key ID, secret access key, region, and output format when prompted.

  4. Use the AWS CLI command syntax to access and download the file from your S3 bucket.

aws s3 cp s3://<bucket-name>/<file-name> .

And there you have it! We have successfully completed Task 1

Task-02

  • 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: Creating an EC2 Instance Snapshot

Follow these steps:

  1. Log in to the AWS Management Console.

  2. Navigate to the EC2 service.

  3. Select the running instance for which you want to create a snapshot.

  4. Right-click on the instance and choose "Create Image ".

  5. Provide a suitable name and description for the snapshot.

  6. Review the snapshot details and click on "Create Image" to initiate the snapshot creation process.

  7. Monitor the progress as the snapshot is being created.

Step 2: Launching a New EC2 Instance from the Snapshot

Now that we have an EC2 instance snapshot, let's use it to launch a new instance. Follow these steps:

  1. Go to the EC2 dashboard in the AWS Management Console.

  2. Click on "Launch Instance" to start the instance creation wizard.

  3. Choose the "My AMIs" tab and select the snapshot you created in the previous step.

  4. Configure the instance details, including the instance type, networking settings, and storage options.

  5. Set up security groups and other advanced settings as required.

  6. Review your instance settings and launch the new instance.

Step 3: Downloading a File from S3 using the AWS CLI

Next, let's explore how to download a file from our S3 bucket using the AWS CLI. Here's how:

  1. Open your EC2 instance's terminal or SSH into the instance.

  2. Install the AWS CLI on the instance if it's not already installed.

  3. Configure the AWS CLI with your AWS credentials using the command: aws configure

  4. Enter your access key ID, secret access key, region, and output format when prompted.

  5. Use the AWS CLI command syntax to download the file from your S3 bucket. For example: aws s3 cp s3://<bucket-name>/<file-name> .

Now verify both file on both EC2

That was it for today. If you have any queries/suggestions please comment below.

See you another day with another challenge.

More from this blog

Ajay Patel

116 posts