DEV Community

 

What is AWS CLI and How to work with it ?

Image description

The AWS Command Line Interface (AWS CLI) is an open source tool that enables you to manage AWS services from the command line. The AWS CLI provides a unified interface to interact with AWS services, including Amazon S3, Amazon EC2, Amazon DynamoDB, and more. With the AWS CLI, you can easily automate repetitive tasks, manage large amounts of data, and build scripts for more complex applications.

Here are some examples of how to use the AWS CLI to manage AWS services:

Amazon S3

Amazon Simple Storage Service (Amazon S3) is a highly scalable, durable, and secure object storage service that enables you to store and retrieve any amount of data, at any time, from anywhere on the web. Amazon S3 provides a simple interface that you can use to store and retrieve any amount of data, at any time, from anywhere on the web. One of the ways to interact with Amazon S3 is by using the AWS Command Line Interface (AWS CLI).

The 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. In this article, we will go through how you can use the AWS CLI to interact with Amazon S3.

Before we get started, you need to have the AWS CLI installed on your system. You can follow the official documentation for the installation process for your operating system. After the installation, you need to configure the AWS CLI with your AWS credentials. You can configure the AWS CLI using the following command:

aws configure
Enter fullscreen mode Exit fullscreen mode

This will prompt you for your AWS Access Key ID, AWS Secret Access Key, default region name, and default output format. After providing the required information, the AWS CLI will be configured and ready to use.

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

Create a bucket:
To create a bucket in Amazon S3, you can use the following command:

aws s3 mb s3://my-bucket
Enter fullscreen mode Exit fullscreen mode

Replace my-bucket with the desired name for your bucket.

List Buckets:
To list all the buckets in your Amazon S3 account, you can use the following command:

aws s3 ls
Enter fullscreen mode Exit fullscreen mode

Upload a file:
To upload a file to a bucket in Amazon S3, you can use the following command:

aws s3 cp file.txt s3://my-bucket/file.txt
Enter fullscreen mode Exit fullscreen mode

Replace file.txt with the name of the file you want to upload and my-bucket with the name of your bucket.

Download a file:
To download a file from a bucket in Amazon S3, you can use the following command:

aws s3 cp s3://my-bucket/file.txt file.txt
Enter fullscreen mode Exit fullscreen mode

Replace file.txt with the name of the file you want to download and my-bucket with the name of your bucket.

Delete a file:
To delete a file from a bucket in Amazon S3, you can use the following command:

aws s3 rm s3://my-bucket/file.txt
Enter fullscreen mode Exit fullscreen mode

Replace file.txt with the name of the file you want to delete and my-bucket with the name of your bucket.

Delete a bucket:
To delete a bucket in Amazon S3, you can use the following command:

aws s3 rb s3://my-bucket
Enter fullscreen mode Exit fullscreen mode

Replace my-bucket with the name of your bucket.

These are just some of the basic AWS CLI commands for Amazon S3. With the AWS CLI, you can perform more complex operations on Amazon S3, such as copying multiple files, syncing directories, and managing bucket.

The Amazon Elastic Compute Cloud (Amazon EC2)

The Amazon Elastic Compute Cloud (Amazon EC2) is a scalable computing platform offered by Amazon Web Services (AWS). The AWS Command Line Interface (AWS CLI) is a unified tool that enables you to manage your AWS services from the command line. In this article, we will explain how to use EC2 with the AWS CLI to create and manage virtual machines in the cloud.

Before you start, make sure that you have installed the AWS CLI on your local machine and that you have set up your AWS credentials. You can do this by running the following command in your terminal or command prompt:

aws configure
Enter fullscreen mode Exit fullscreen mode

This command will prompt you to enter your AWS Access Key ID, AWS Secret Access Key, and default region name.

Once you have set up the AWS CLI, you can start using it to interact with EC2. Here are some of the most common EC2 tasks that you can perform using the AWS CLI:

Create an EC2 instance
To create an EC2 instance, you can use the following command:

aws ec2 run-instances --image-id ami-0c55b159cbfafe1f0 --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids sg-0123456789abcdef0 --subnet-id subnet-0123456789abcdef0
Enter fullscreen mode Exit fullscreen mode

This command creates a single EC2 instance of the t2.micro type, using the Amazon Machine Image (AMI) with the ID ami-0c55b159cbfafe1f0. The instance is associated with the key pair MyKeyPair and the security group sg-0123456789abcdef0, and is launched in the subnet subnet-0123456789abcdef0.

List EC2 instances
To list all the EC2 instances in your AWS account, you can use the following command:

aws ec2 describe-instances
Enter fullscreen mode Exit fullscreen mode

This command returns a JSON object that contains information about the instances, including their IDs, types, and states.

Terminate an EC2 instance
To terminate an EC2 instance, you can use the following command:

aws ec2 terminate-instances --instance-ids i-0123456789abcdef0
Enter fullscreen mode Exit fullscreen mode

This command terminates the EC2 instance with the ID i-0123456789abcdef0.

Start an EC2 instance
To start an EC2 instance, you can use the following command:

aws ec2 start-instances --instance-ids i-0123456789abcdef0
Enter fullscreen mode Exit fullscreen mode

This command starts the EC2 instance with the ID i-0123456789abcdef0.

These are just a few examples of the EC2 tasks that you can perform using the AWS CLI. The AWS CLI provides a rich set of commands that enable you to manage your EC2 instances, create snapshots, and perform many other tasks. To learn more about the AWS CLI and EC2, you can refer to the official AWS documentation.

Top comments (0)

Welcome! 👋 New to DEV?

Head over to our Welcome Thread and tell us a bit about yourself!