S3 Programmatic access with AWS-CLI

S3 Programmatic access with AWS-CLI

Day 43

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.

Task-01

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

    1. Log in to the AWS Management Console.

    2. Navigate to the EC2 service and launch an EC2 instance using the console.

    3. Connect to the EC2 instance using SSH.

      Paste copy ssh command in your Terminal.

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

    1. Log in to your AWS Management Console and go to the S3 service.

    2. Click on the "Create bucket" button.

    3. Enter a unique name for your bucket, select the region you want to create it in, and then click "Create."

    4. Once your bucket is created, click on its name to open it.

    5. Click on the "Upload" button to upload a file.

    6. In the "Upload" window, click on the "Add files" button to select the file you want to upload.

    7. Once you've selected your file, click "Next."

      you can set permissions for your file, configure the storage class and set metadata.

      Review the details before clicking on the "Upload" button to upload your file.

    8. Once the upload is complete, you can see the file in your S3 bucket.

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

    1. Install the AWS CLI.

    2. Check whether aws-cli is installed or not using checking the aws version

    3. Once you have installed the AWS CLI, open a terminal and run the command aws configure to configure your account credentials.

    4. Enter your AWS Access Key ID and Secret Access Key

    5. List s3 buckets using the command: aws s3 ls

    6. you can use the aws s3 cp command to copy the file from your S3 bucket to your EC2 instance and view the content of the file using the cat command.

Task-02

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

    1. EC2 Instance Page, click a snapshot, click Create snapshot select the EC2 instance that you want to create a snapshot of.

    2. Snapshot created.

    3. On the right side, click on Actions and select 'Create image from snapshot'

    4. On the "Create Image from snapshot" window, enter a name and description for the image.

    5. Click on 'create image'.

    6. Once the image is created, go to the "AMIs" section in the EC2 Dashboard.

      check image is created.

    7. Select the newly created AMI, right-click on it, and select "Launch Instance."

    8. In the "Launch Instance" window, choose the configuration options for the new instance.

      Choose the VPC and subnet you want to launch the new instance in.

      In the "Add Storage" section, you can choose to modify the storage volumes as per your requirements.

      Review the instance details and click "Launch instance" to launch the new instance.

    9. here login as the user "ubuntu" rather than the user "root"

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

    aws s3 cp

    1. command to download the file from your S3 bucket to your EC2 instance.

       aws s3 cp s3://BUCKET_NAME/PATH/TO/FILE /PATH/TO/LOCAL/FILE
      
  • Verify that the contents of the file are the same on both EC2 instances.

AWS CLI commands for S3

  1. This command lists all of the S3 buckets in your AWS account.

     aws s3 ls
    
  2. This command lists the objects in an S3 bucket.

     aws s3 ls s3://bucket-name
    
  3. This command creates a new S3 bucket with the specified name.

     aws s3 mb s3://bucket-name
    
  4. This command deletes the specified S3 bucket.

     aws s3 rb s3://bucket-name
    
  5. This command uploads a file to an S3 bucket.

     aws s3 cp file.txt s3://bucket-name
    
  6. This command downloads a file from an S3 bucket to your local file system.

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

Thankyou