Day 43 - S3 Programmatic access with AWS-CLI ๐Ÿ’ป ๐Ÿ“

Day 43 - S3 Programmatic access with AWS-CLI ๐Ÿ’ป ๐Ÿ“

ยท

4 min read

S3

Amazon Simple Storage Service (Amazon S3) is an object storage service that provides a secure and scalable way to store and access data on the cloud. It is designed for storing any kind of data, such as text files, images, videos, backups, and more. Read more here

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

Read more about S3 using aws-cli here

Step1 :

  1. Create an instance from the Amazon console

  2. Once it is created, click on connect

  3. Navigate to SSH client and copy the Example

  4. Now open your terminal in a directory, where your pem. file is available

    and paste the SSH command

    ssh -i "rjthapaa.pem"

  5. we have connected to the instance now

Step2 : Creating a S3 bucker in AWS console

  1. Navigate to S3 in aws console

  2. Click on Create bucket

  3. Provide a unique bucket name and selected region

  4. Under Object Ownership

  5. Click on Create bucket

  6. We will upload a file to S3 bucket using SSH

  7. Uploaded a file called rootkey.csv in S3 bucket

  8. Now we need to access this file from S3 bucket using Aws CLI. For that you need to install AWS CLI and configure it

  9. Once the configuration is done, try to access the S3 bucket using below command

  10. s3 url will be available

    Amazon S3>Buckets>rjtapaas3bucket>rootkey.csv

    # List downs the available s3 buckets
    $ aws s3 ls
    
    # Copies the root.key.csv file from S3 to ec2 instance
    $ aws cp s3://rjtapaas3bucket/rootkey.csv /home/ubuntu
    

    See the file from the s3 bucket downloaded to the terminal

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.

  1. Navigate to Ec2 > Snapshot > Create snapshot

  2. Set the Resource type as Instance and select the instance ID

  3. Volumes can be optional and click on Create snapshot

  4. Successfully created a snapshot

  5. Now we will use a snapshot to launch an Ec2 instance

  6. Select the snapshot and under Actions > Create image from snapshot

  7. Provide the required details for the snapshot

  8. Click on Create image

  9. Image is created in Amazon machine Images (AMIS)

  10. Now select the AMI image and click on Launch instance from AMI

  11. Provide the name details for instance and launch the instance

  12. Log in as a "Ubuntu user" rather than a "root user"

  13. Now we will try to access the S3 bucket and download a file from it using AWS CLI

    aws s3 ls
    aws s3 cp s3://BUCKET_NAME/PATH/TO/FILE /PATH/TO/LOCAL/FILE
    

AWS CLI S3 Commands

Command to list all of the S3 buckets in your AWS account.

aws s3 ls

Command to list the objects in an S3 bucket.

aws s3 ls s3://bucket-name

Command to create a new S3 bucket with the specified name.

aws s3 mb s3://bucket-name

Command to delete a specified S3 bucket.

aws s3 rb s3://bucket-name

Command to upload a file to an S3 bucket.

aws s3 cp file.txt s3://bucket-name

Command to download a file from an S3 bucket to your local file system.

aws s3 cp s3://bucket-name/file.txt .

Command to upload a file from Local to Instance

scp -i "rjthapaa.pem" rootkey.csv ubuntu@ec2-54-205-10-188.compute-1.amazonaws.com:/home/ubuntu

Here we are downloading a file name rootkey.csv from the instance to the local machine

Command to download a file from Instance to local

scp -i "rjthapaa.pem" ubuntu@ec2-54-205-10-188.compute-1.amazonaws.com:/home/ubuntu/file.txt .

A file.txt file is been downloaded from the instance to the local machine

ย