# Day #43: S3 Programmatic access with AWS-CLI

# 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](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html)

## 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](https://docs.aws.amazon.com/cli/latest/reference/s3/index.html)

## 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. Launch an EC2 instance by providing all the default configurations as we have seen in my previous blogs.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681912760808/0ddb0dc9-5ace-4fdc-9b04-922dda7a398f.png?auto=compress,format&format=webp align="left")
    
2. Connect to the server using SSH. Open Command Prompt in your local system and navigate to the private key ".pem" file location.
    
3. Copy the ssh command from the console and paste on CMD.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681912952630/6fe27177-2d8f-4ea7-b599-7d63469f3aea.png?auto=compress,format&format=webp align="left")
    
4. Now you are connected to the EC2 instance.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681913155613/566ab17a-0a2c-415a-97d1-f9c38efa6bb6.png?auto=compress,format&format=webp align="left")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681913184456/c3effd23-2302-4c67-95dc-1f4f87c2c66f.png?auto=compress,format&format=webp align="left")
    

* Create an S3 bucket and upload a file to it using the AWS Management Console.
    
    1. Navigate to the S3 page in the AWS console and provide the bucket name and choose the region for it.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681913568404/fd96e2b2-2bc3-4c43-af12-49e88cf87adb.png?auto=compress,format&format=webp align="left")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681913503238/2c49cefc-f665-40d6-b462-4dcc61d060fc.png?auto=compress,format&format=webp align="left")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681913527412/69f971ce-fc19-4eef-a258-eba022210d4e.png?auto=compress,format&format=webp align="left")
        
    2. The S3 bucket is created now.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681913628144/41a9e8bb-9a89-471a-97ed-6335f83b8482.png?auto=compress,format&format=webp align="left")
        
    3. Now upload a file to the S3 bucket.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681913694844/5d782019-255e-4d40-8b92-d43a7fc9e5df.png?auto=compress,format&format=webp align="left")
        
    4. I have added my resume and a fileS3.txt to the bucket from my local system.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681913974520/5603d4b0-5c8d-487b-9f75-9aa58224beb1.png?auto=compress,format&format=webp align="left")
        
    5. The file is now uploaded to the S3 bucket.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681914149918/f3fb6027-e6e6-4c21-abc0-9a82105648c2.png?auto=compress,format&format=webp align="left")
        
* Access the file from the EC2 instance using the AWS Command Line Interface (AWS CLI).
    
    1. Navigate to your instance and install AWS CLI.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681914412682/e17b50da-6749-45af-a13a-e1c0286b7a4b.png?auto=compress,format&format=webp align="left")
        
    2. Configure this with the IAM user having specific permission to have a connection between AWS CLI to the management console.
        
    3. Provide the necessary Access key IDs to build the connection.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681918444012/09de98b6-c920-4a4b-9cee-14e544361f05.png?auto=compress,format&format=webp align="left")
        
    4. Now you can view the S3 bucket list and copy the file from S3 to EC2.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681918753984/5ba79d52-c5da-443c-837c-8aaae0178bd1.png?auto=compress,format&format=webp align="left")
        
    5. You can view the file content now in AWS CLI.
        

## **Task 2: Creating a EC2 snapshot and integrating to S3**

* Create a snapshot of the EC2 instance and use it to launch a new EC2 instance.
    
    1. Navigate to the snapshot section in the AWS console.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681919281053/602806ca-351a-467e-aea2-2e0d4b7e9eb5.png?auto=compress,format&format=webp align="left")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681919404740/d4534935-85c3-4eca-ab38-a636bd02b8e4.png?auto=compress,format&format=webp align="left")
        
    2. Snapshot is now created.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681919741586/ea42dfa2-6c4f-4039-823c-95c46deb0351.png?auto=compress,format&format=webp align="left")
        
    3. Now create an image from the snapshot.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681919807754/54ee5ca0-6db3-4cc5-8936-4ab1454cbca3.png?auto=compress,format&format=webp align="left")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681919887197/2ee38fc7-bcbd-40e1-bc41-96942b0f9c4a.png?auto=compress,format&format=webp align="left")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681919906559/62c15692-ffc0-404a-aa2d-8047e7ca5f65.png?auto=compress,format&format=webp align="left")
        
    4. Navigate to AMIs section in EC2 dashboard and you can see the Machine image. Then create an instance from the image.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681920049196/a4748f76-0597-4b55-b706-673fef72a116.png?auto=compress,format&format=webp align="left")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681920114307/fa770816-bd4b-4716-8f67-cd3d02d52758.png?auto=compress,format&format=webp align="left")
        
    5. You can see the new instance now.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681920206158/5eb87543-661c-4893-9f12-c3b6b8b533a2.png?auto=compress,format&format=webp align="left")
        
* Download a file from the S3 bucket using the AWS CLI.
    
    1. Now, connect the instance from AWS CLI to the AWS management console.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681920712795/e1f3dcaf-362a-4815-bc76-941cac46471d.png?auto=compress,format&format=webp align="left")
        
* Verify that the contents of the file are the same on both EC2 instances.
    
    1. Download the file from S3 on the new instance and verify the content is matched with the old instance.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681920839051/cedea427-38d3-4add-baa6-06d26b9626e9.png?auto=compress,format&format=webp align="left")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681920880769/49ac5724-e651-41c2-8a23-947146ef4997.png?auto=compress,format&format=webp align="left")
        

Some important AWS S3 commands are:-

1. `aws s3 ls` - This command lists all of the S3 buckets in your AWS account.
    
2. `aws s3 mb` [`s3://bucket-name`](s3://bucket-name) - This command creates a new S3 bucket with the specified name.
    
3. `aws s3 rb` [`s3://bucket-name`](s3://bucket-name) - This command deletes the specified S3 bucket.
    
4. `aws s3 cp file.txt` [`s3://bucket-name`](s3://bucket-name) - This command uploads a file to an S3 bucket.
    
5. `aws s3 cp` [`s3://bucket-name/file.txt`](s3://bucket-name/file.txt) `.` - This command downloads a file from an S3 bucket to your local file system.
    
6. `aws s3 sync local-folder` [`s3://bucket-name`](s3://bucket-name) - This command syncs the contents of a local folder with an S3 bucket.
    
7. `aws s3 ls` [`s3://bucket-name`](s3://bucket-name) - This command lists the objects in an S3 bucket.
    
8. `aws s3 rm` [`s3://bucket-name/file.txt`](s3://bucket-name/file.txt) - This command deletes an object from an S3 bucket.
    
9. `aws s3 presign` [`s3://bucket-name/file.txt`](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.
    
10. `aws s3api list-buckets` - This command retrieves a list of all S3 buckets in your AWS account, using the S3 API.
