Introduction :
Amazon Web Services (AWS) is a widely-used cloud computing platform that offers a wide range of services to build, deploy, and manage applications in the cloud. In this blog, we will walk through two essential tasks: Launching an EC2 instance and managing an S3 bucket. We'll cover the steps to create an EC2 instance, connect to it using Secure Shell (SSH), and then demonstrate how to create an S3 bucket, upload a file to it, and access that file from the EC2 instance using the AWS Command Line Interface (AWS CLI). Additionally, we'll explore some commonly used AWS CLI commands for Amazon S3.
Task-01: Launching an EC2 Instance and Accessing it via SSH
Amazon Elastic Compute Cloud (EC2) provides scalable compute capacity in the cloud. Let's go through the steps to launch an EC2 instance and access it using Secure Shell (SSH).
Launch an EC2 Instance
Log in to the AWS Management Console.
Navigate to the EC2 dashboard.
Click on "Launch Instance" and select an Amazon Machine Image (AMI) of your choice.
Choose an instance type based on your requirements.
Configure instance details such as network settings, security groups, and storage.
Review the configuration and launch the instance.
Connect to the EC2 Instance via SSH
Retrieve the public IP address or public DNS of the EC2 instance from the EC2 dashboard.
Open your terminal or SSH client and use the following command to connect to the instance:
ssh -i <path-to-your-key.pem> ec2-user@<public-ip-or-dns>
Task-02: Managing an S3 Bucket and Accessing Data from EC2
Amazon Simple Storage Service (S3) is a highly scalable object storage service that allows you to store and retrieve any amount of data. Let's learn how to create an S3 bucket, upload a file to it, and access the file from the EC2 instance using AWS CLI.
Step 1: Create a Snapshot of the EC2 Instance
Go to the AWS Management Console and navigate to the EC2 dashboard.
In the left navigation pane, click on "Instances" and select the EC2 instance for which you want to create a snapshot.
Right-click on the selected instance and choose "Create Image (EBS AMI)."
Enter a descriptive name and description for the snapshot, then click "Create Image."
The snapshot creation process will start, and you can monitor its progress in the "Snapshots" section of the EC2 dashboard.
Step 2: Launch a New EC2 Instance from the Snapshot
Once the snapshot is created, go to the "AMIs" section in the EC2 dashboard.
Select the snapshot you just created, click on "Actions," and choose "Launch Instance."
Follow the instance launch wizard, selecting the desired instance type, network settings, security groups, and other configurations.
Click "Launch" to start the new EC2 instance using the snapshot as its base.
Create an S3 Bucket and Upload a File
Go to the AWS Management Console and navigate to the S3 service.
Click on "Create Bucket" and provide a unique name and region for your bucket.
Once the bucket is created, click on it and choose "Upload."
Select a file from your local system to upload to the bucket.
Access the File from EC2 using AWS CLI
Log in to your EC2 instance using SSH, as described in Task-01.
Install AWS CLI on the EC2 instance using the following commands:
sudo yum update -y
sudo yum install -y aws-cli
- After installation, run the following command to configure AWS CLI with your credentials:
aws configure
- Enter your AWS Access Key ID, Secret Access Key, default region, and preferred output format.
Download the File from S3 Bucket
- Use the AWS CLI command to download the file from the S3 bucket to your EC2 instance:
aws s3 cp s3://<bucket-name>/file.txt .
Verify File Contents on Both EC2 Instances
- Use the
cat
command to view the contents of the file on the EC2 instance:
- Use the
cat file.txt
Commonly Used AWS CLI Commands for Amazon S3
- List all S3 buckets in your AWS account:
aws s3 ls
- Create a new S3 bucket:
aws s3 mb s3://bucket-name
- Delete an S3 bucket:
aws s3 rb s3://bucket-name
- Upload a file to an S3 bucket:
aws s3 cp file.txt s3://bucket-name
- Download a file from an S3 bucket to your local system:
aws s3 cp s3://bucket-name/file.txt .
- Sync the contents of a local folder with an S3 bucket:
aws s3 sync local-folder s3://bucket-name
- List objects in an S3 bucket:
aws s3 ls s3://bucket-name
- Delete an object from an S3 bucket:
aws s3 rm s3://bucket-name/file.txt
- Generate a pre-signed URL for an S3 object:
aws s3 presign s3://bucket-name/file.txt
- Retrieve a list of all S3 buckets using the S3 API:
aws s3api list-buckets
Conclusion
Mastering AWS services like EC2 and S3 is essential for efficiently deploying applications in the cloud. In this blog, we covered the step-by-step process of launching an EC2 instance, accessing it via SSH, creating an S3 bucket, uploading a file, and accessing that file from the EC2 instance using AWS CLI. Additionally, we explored some commonly used AWS CLI commands for Amazon S3. By understanding these tasks, you'll be better equipped to manage your cloud infrastructure effectively and leverage AWS services to their full potential. Happy cloud computing!