Accessing AWS resource inside EC2 instance!

Accessing AWS resource inside EC2 instance!

Introduction

Amazon Web Services (AWS) provides a wide range of cloud computing services, two of the most popular being Amazon EC2 and Amazon S3.

In this blog, we’ll combine these services to accomplish the following:

  • Launch an EC2 instance and configure SSH access.
  • Upload a file to an S3 bucket and generate a pre-signed URL for temporary access.
  • Use the EC2 instance to download the file from S3 using AWS CLI.

The diagram below illustrates the workflow, showing how different AWS services interact to achieve secure file transfer.
Screenshot 2025-02-20 at 11.15.42 AM

Getting Started.

The steps below serve as a guide on creating an EC2 instance and use it to access the file stored in S3.

Step 1: Launch an EC2 Instance and Allow SSH Access.

  1. Log in to the AWS Management Console and navigate to the EC2 service.

https://aws.amazon.com/console/

  1. Click on "Launch Instances" and configure the following:
  • Choose an Amazon Machine Image (AMI) (e.g., Amazon Linux 2).
  • Select an instance type (e.g., t2.micro for free-tier eligible).
  • Configure the instance details (default settings are fine for this task).
    565567F7-76EA-423C-99F5-0F7CD0D20C23
  1. Set up Security Group Rules:
  • Add a rule to allow SSH access from your specific IP address (use My IP to automatically detect your IP).
  • Example:
Type: SSH
Protocol: TCP
Port Range: 22
Source: Your IP (e.g., 203.0.113.25/32)

E97949F2-8107-4F88-8CEF-291D17EAD87D

  1. Launch the instance and download the private key file (.pem) if you don't already have one.
    B78359DF-8C2F-438F-9E99-E29A90A124D1

Step 2: Upload a File to S3 and Generate a Pre-Signed URL

  1. Navigate to the S3 service in the AWS Management Console and create a bucket (if you don’t already have one):
  • Click Create bucket and provide a unique bucket name.
  • Leave other settings as default and create the bucket.
    DCD1747F-A92F-4C0C-91C4-63208090BB89
  1. Upload a file to the S3 bucket:
  • Click on the bucket name, then click Upload.
  • Select the file you want to upload and complete the upload process.
    B716BCA0-9057-4447-91E7-04EF7B701EDB
  1. Generate a pre-signed URL for the file:
  • Go to the Objects tab in your bucket and find the uploaded file.
  • Click Actions" → "Generate pre-signed URL.
  • Specify an expiration time (e.g., 1 hour) and copy the URL.
    2329E3A0-A1C9-45CD-A44B-2B56951312B4

Step 3: Connect to the EC2 Instance

You can connect to your EC2 instance using either EC2 Instance Connect or your local terminal.

  1. Using EC2 Instance Connect (Web-Based):
  • Go to the EC2 Dashboard, select your instance, and click "Connect".
  • Choose "EC2 Instance Connect" and click "Connect" to open a terminal in your browser.
    22ACDC71-630A-48B0-9CD4-930F5459B893
  1. Using a Local Terminal:
  • Open your terminal and navigate to the directory where your .pem key file is stored.
  • Run the following command to SSH into the instance:
ssh -i "your-key-file.pem" ec2-user@<Public-IP-of-EC2>

E255805B-43EC-4753-88D0-8E70BEED4217

  • Replace your-key-file.pem with the path to your private key file and Public-IP-of-EC2 with the instance's public IP address.

Step 4: Install and Configure AWS CLI

  1. Install AWS CLI on the EC2 instance:
sudo yum update -y
sudo yum install aws-cli -y
  1. Configure AWS CLI:
    Run the following command to configure AWS CLI:
aws configure

Provide your AWS credentials (Access Key, Secret Key) and default region (e.g., us-east-1) when prompted.

Step 5: Download the File Using the Pre-Signed URL

  1. Use the curl command to download the file from the pre-signed URL:
curl 'presigned-url' --output file-name

74B905D4-7606-4F36-AD39-846854689186

  1. To check wether the file has been downloaded use the command below:
cat file-name

5CF5860A-522B-4996-A93F-D9FB926BE521

Conclusion

This blog demonstrated how to launch an EC2 instance, upload a file to S3, generate a pre-signed URL, and download the file using AWS CLI.
The goal of this process is to securely transfer files between Amazon S3 and an EC2 instance using AWS CLI and pre-signed URLs.

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.