Day 43: S3 Programmatic access with AWS-CLI.

Day 43: S3 Programmatic access with AWS-CLI.

Today I have covered,

  • What is Amazon Simple Storage Service (Amazon S3)?

  • How to AWS S3 with AWS-CLI?

  • How to Create AWS S3 Bucket?

What is AWS S3?

  • Amazon Simple Storage Service (Amazon S3) is object storage service offering industry-leading scalability, data availability, security, and performance. Customers of all sizes and industries can store and protect any amount of data for virtually any use case, such as data lakes, cloud-native applications, and mobile apps.

  • With cost-effective storage classes and easy-to-use management features, you can optimize costs, organize data, and configure fine-tuned access controls to meet specific business, organizational, and compliance requirements.

What is AWS CLI?

  • The AWS Command Line Interface (AWS CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.

  • The AWS CLI v2 offers several new features including improved installers, new configuration options such as AWS IAM Identity Center (successor to AWS SSO), and various interactive features.


Task-01

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

  • Step-01: Launch an EC2 instance using the AWS Management Console and select OS as Ubuntu.

Screenshot from 2023-07-12 10-00-17

  • Step-02: Select instance type ,key pair and security group.

Screenshot from 2023-07-12 10-01-25

Step-03: Launch the instance via SSH so copy the command and paste it in your terminal.

imageedit_4_4927819131

Step-04: Now you can see your instance is running.

imageedit_7_6423977729


Before creating an S3 bucket we need to install AWS CLI in our EC2 instance.

  • Step-01: Install AWS CLI using the following command
sudo apt update
sudo apt install unzip
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

imageedit_17_3657219052

  • Step-02: Verify the installation using the following command
aws --version

imageedit_19_8441271999

  • Step-03: Configure the AWS CLI using the following command in which we will provide the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY which we have created in the previous task and also provide the region we want to work and the output format.
aws configure

imageedit_25_2247222209

  • Step-04: Verify the configuration using the following command which will show the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY which we have provided in the previous step.
aws configure list

Screenshot from 2023-07-06 00-05-06


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

  • Step-01: Go to S3 service and click on Create bucket.

Screenshot from 2023-07-12 10-05-16

  • Step-02: Give a name to your bucket as 'rohit-s3bucket'.

Screenshot from 2023-07-12 10-22-42

Step-03: Now click on Create bucket.

Screenshot from 2023-07-12 10-23-13

Step-04: Now click on Upload and upload a file.

Screenshot from 2023-07-12 10-23-37

Screenshot from 2023-07-12 10-24-57

Step-05: Now click on Upload.

Screenshot from 2023-07-12 10-25-19


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

  • aws s3 ls - This command lists all of the S3 buckets in your AWS account.

Screenshot from 2023-07-12 10-46-21

  • Now let's create a text file and upload it to our S3 bucket.

Screenshot from 2023-07-12 12-06-57

aws s3 cp <filename> s3://<bucketname>
  • Thus we have uploaded our file to our S3 bucket.

Screenshot from 2023-07-12 12-09-28

  • Check the file in the S3 bucket.

Screenshot from 2023-07-12 12-10-02

  • Now we will see how to download a file from S3 bucket to our EC2 instance.

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

Screenshot from 2023-07-12 12-31-22

aws s3 cp s3://rohit-s3bucket/rohit.txt /home/ubuntu/storage

Screenshot from 2023-07-12 12-31-50


Task-02

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

  • Step-01: Go to EC2 service and click on snapshot.

imageedit_12_2673864321

  • Step-02: In Snapshot click on resource type and select instance with appropriate instance id.

Screenshot from 2023-07-12 12-56-58

  • Step-03: After creating the snapshot,we will create image from snapshot.

Screenshot from 2023-07-12 13-15-44

  • Step-04: Now we are creating image from snapshot.

Screenshot from 2023-07-12 13-18-01

  • Step-05: Now we will select the snapshot from the Amazon Machine Image(AMI).

imageedit_16_4082641438

  • Select (Owned by me) and click on Launch instance from AMI.

imageedit_14_8012926792

  • Launch snapshot by AMI instance.

imageedit_19_5549087778

  • Step-06: Now we will launch the instance which is a SnapShot instance of the first instance.

Screenshot from 2023-07-12 14-27-19

  • Step-07: Now we will check the key pair login and Network Setting of the new instance and create it.

Screenshot from 2023-07-12 13-28-10

  • Step-08: After creating the instance we will ssh into the instance.

imageedit_22_3919884492

Please login as the user "ubuntu" rather than the user "root" - This error occurs because we are trying to login as root user.

imageedit_30_8945820103

  • So follow this command to login as ubuntu user.
ssh -i "s3bucket.pem" ubuntu@ec2-13-234-204-102.ap-south-1.compute.amazonaws.com

imageedit_35_9342945161


  • Now we will check the content of Instance-one (MyS3Bucket) is snapped into instance-two (rohit-s3bucket-snapshot-server)*.*

Screenshot from 2023-07-12 18-53-11

  • Verify that the contents of the file are the same on both EC2 instances.

imageedit_42_7727636729

  • Thus we have verified that the contents of the file are the same in both EC2 instances.

Here are some commonly used AWS CLI commands for Amazon S3:

  • aws s3 ls - This command lists all of the S3 buckets in your AWS account.

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

  • aws s3 rb s3://bucket-name - This command deletes the specified S3 bucket.

  • aws s3 cp file.txt s3://bucket-name - This command uploads a file to an S3 bucket.

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

  • aws s3 sync local-folder s3://bucket-name - This command syncs the contents of a local folder with an S3 bucket.

  • aws s3 ls s3://bucket-name - This command lists the objects in an S3 bucket.

  • aws s3 rm s3://bucket-name/file.txt - This command deletes an object from an S3 bucket.

  • aws s3 presign s3://bucket-name/file.txt - This command generates a pre-signed URL for an S3 object, which can be used to grant temporary access to the object.

  • aws s3api list-buckets - This command retrieves a list of all S3 buckets in your AWS account, using the S3 API.


Happy Learning :)

Did you find this article valuable?

Support DevOps by becoming a sponsor. Any amount is appreciated!