Title: Mastering AWS CLI for EC2: A DevOps Guide
In the world of DevOps and cloud computing, Amazon Web Services (AWS) stands as a giant, offering a vast array of services to support your infrastructure needs. Among these services, Amazon Elastic Compute Cloud (EC2) is one of the most fundamental and versatile components. AWS CLI (Command Line Interface) is a powerful tool that allows you to interact with AWS services, and mastering it is essential for efficient DevOps practices on AWS. In this blog post, we will explore the AWS CLI and dive deep into how it can be used to manage EC2 instances effectively.
What is AWS CLI?
AWS CLI is a unified tool provided by AWS that enables you to manage various AWS services from the command line. It offers a rich set of commands and options for interacting with your AWS resources programmatically, making it an indispensable tool for DevOps engineers and system administrators.
Getting Started with AWS CLI
Before diving into EC2-specific commands, you need to set up and configure AWS CLI on your local machine. Here's a quick guide to getting started:
1. Install AWS CLI:
You can install AWS CLI on your system using various methods, such as pip, Homebrew, or direct downloads. AWS provides installation instructions for various platforms on their website.
2. Configure AWS CLI:
After installation, configure AWS CLI by running the aws configure
command. You'll need to provide your AWS Access Key ID, Secret Access Key, default region, and preferred output format (JSON, text, or table).
Managing EC2 Instances
Now, let's explore some common AWS CLI commands for managing EC2 instances.
- Launching EC2 Instances
To launch a new EC2 instance, you can use the aws ec2 run-instances
command. Specify the Amazon Machine Image (AMI), instance type, and other configurations as required.
**NOTE :- Run this command where you have already downloaded pem key . and do not use .pem
2) image-id ami changes . so use latest everytime
- Listing EC2 Instances
To view a list of your running EC2 instances, use the aws ec2 describe-instances
command. This command provides detailed information about your instances.
NOTE:- if you use - - output table (just see the magic) at the end
- Stopping and Terminating Instances
To stop or terminate EC2 instances, use the aws ec2 stop-instances
and aws ec2 terminate-instances
commands, respectively.
```
- Creating EC2 Snapshots
You can create snapshots of your EBS volumes using the aws ec2 create-snapshot
command. Snapshots are crucial for backup and disaster recovery.
bash
5. Security Group Management
Managing security groups is essential for controlling inbound and outbound traffic to your instances. You can create, modify, and delete security groups with AWS CLI.
- Key Pair Management
Use the aws ec2 create-key-pair
and aws ec2 delete-key-pair
commands to manage key pairs for secure SSH access to your instances.
Conclusion
Mastering AWS CLI is a crucial skill for DevOps engineers working with AWS services like EC2. It allows you to automate tasks, manage resources efficiently, and integrate AWS into your DevOps workflows seamlessly. By understanding and harnessing the power of AWS CLI, you can unlock the full potential of AWS and elevate your DevOps game to the next level.
Remember, AWS CLI is a vast tool, and this blog post only scratches the surface. To become an AWS CLI expert, continue exploring its capabilities, read the official documentation, and practice using it in your daily DevOps tasks.
Projects:-
Script to start all instances
- Script to shutdown all unused instance during off-office hours
You can easily get the script on my github page . here’s my link
Do give it a read ..