DEV Community

Cover image for Running Linux Commands and Uploading the Output to S3 Directly Through an EC2 Instance
Durga Vellingiri
Durga Vellingiri

Posted on

1

Running Linux Commands and Uploading the Output to S3 Directly Through an EC2 Instance

Step 1: Launch an EC2 Instance

  1. Navigate to the EC2 Dashboard in the AWS Management Console.
  2. Click on Launch Instance.
  3. Select the desired Amazon Machine Image (AMI).
  4. Choose an instance type (e.g., t2.micro).

Image description


Step 2: Create a Key Pair

  1. During the EC2 setup, create a new Key Pair or use an existing one.
  2. Download the private key file (.pem) and keep it secure.

Image description


Step 3: EC2 Instance Created

  1. Complete the EC2 setup process and launch the instance.
  2. Note the Public IP of your instance.

Image description


Step 4: Download PuTTY

  1. Download PuTTY from the official PuTTY site.

Image description


Step 5: Install PuTTY

  1. Install PuTTY and PuTTYgen on your local machine.

Image description


Step 6: Edit Inbound and Outbound Rules

  1. Go to the Security Groups associated with your EC2 instance.
  2. Edit the Inbound Rules to allow SSH (port 22) from your IP address.
  3. Optionally, update Outbound Rules as needed.

Image description


Step 7: Change Security Groups and Whitelist Your IP

  1. Ensure that the inbound rules whitelist only your IP address for SSH access.

Image description


Step 8: Open PuTTY and Connect to the EC2 Instance

  1. Open PuTTY and configure it to connect to your EC2 instance:
  2. Enter the Public IP of the EC2 instance.
  3. Load your private key (.pem) file in PuTTYgen and save it as a .ppk file.
  4. Use the .ppk file in PuTTY to authenticate.
  5. Connect to the EC2 instance.

Image description


Step 9: Run Commands on the Connected Instance

Run any Linux commands to generate or process data.

Image description


Step 10: Save Output to a File

  1. Use the following command to save output to a file:echo "714022205029-durgav" > details.txt

Image description


Step 11: IAM Role Setup

  1. Navigate to the IAM Dashboard in the AWS Management Console.
  2. Start creating a new role with the following permissions:AmazonS3FullAccess or a custom policy for specific bucket access.

Image description


Step 12: Role Created

1.Complete the role creation process and name the role appropriately.

Image description


Step 13: Attach the Role to the EC2 Instance

  1. Attach the newly created IAM role to your EC2 instance:
  2. Go to the EC2 Dashboard.
  3. Select your instance and choose Actions > Security > Modify IAM Role.
  4. Attach the role.

Image description


Step 14: Verify IAM Role Connection

1.Run the following command to verify the IAM role is working:aws s3 ls

Image description


Step 15: Upload the File to an S3 Bucket

  1. Use the following command to upload the file to S3:aws s3 cp details.txt s3://714022205029/

Image description


Step 16: Delete the IAM Role

1.After completing the tasks, delete the IAM role if it's no longer needed.

Image description


Step 17: Terminate the EC2 Instance

1.Go to the EC2 Dashboard and terminate the instance to stop incurring charges.

Image description


This step-by-step guide ensures you can securely run Linux commands on an EC2 instance, upload the output to S3, and clean up resources afterward.

Top comments (0)

Retry later