How to Use AWS CLI: Automate Cloud Management with Command Line

 


The AWS Command Line Interface (AWS CLI) is a powerful tool that allows developers and system administrators to interact with AWS services directly from the terminal. It provides automation capabilities, improves workflow efficiency, and enables seamless cloud resource management.

Why Use AWS CLI?

  • Automation: Automate repetitive tasks using scripts.
  • Efficiency: Manage AWS services without navigating the AWS Management Console.
  • Speed: Perform bulk operations faster than using the web interface.
  • Scripting & Integration: Combine AWS CLI commands with scripts for complex workflows.

1. Installing AWS CLI

Windows

  1. Download the AWS CLI installer from AWS official site.
  2. Run the installer and follow the prompts.
  3. Verify installation:
  • aws --version

macOS

  1. Install using Homebrew:
  • brew install awscli
  1. Verify installation:
  • aws --version

Linux

  1. Install using package manager:
  • curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "awscliv2.pkg" sudo installer -pkg awscliv2.pkg -target /
  1. Verify installation:
  • aws --version

2. Configuring AWS CLI

After installation, configure AWS CLI with your credentials:

aws configure

You’ll be prompted to enter:

  • AWS Access Key ID
  • AWS Secret Access Key
  • Default Region (e.g., us-east-1)
  • Default Output Format (json, text, or table)

Example:

AWS Access Key ID [****************ABCD]:
AWS Secret Access Key [****************XYZ]:
Default region name [us-east-1]:
Default output format [json]:

To verify credentials:

aws sts get-caller-identity

3. Common AWS CLI Commands

Managing EC2 Instances

  • List EC2 instances:
  • aws ec2 describe-instances
  • Start an instance:
  • aws ec2 start-instances --instance-ids i-1234567890abcdef0
  • Stop an instance:
  • aws ec2 stop-instances --instance-ids i-1234567890abcdef0

S3 Bucket Operations

  • List all S3 buckets:
  • aws s3 ls
  • Create a new S3 bucket:
  • aws s3 mb s3://my-new-bucket
  • Upload a file to a bucket:
  • aws s3 cp myfile.txt s3://my-new-bucket/
  • Download a file from a bucket:
  • aws s3 cp s3://my-new-bucket/myfile.txt .

IAM User Management

  • List IAM users:
  • aws iam list-users
  • Create a new IAM user:
  • aws iam create-user --user-name newuser
  • Attach a policy to a user:
  • aws iam attach-user-policy --user-name newuser --policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess

Lambda Function Management

  • List Lambda functions:
  • aws lambda list-functions
  • Invoke a Lambda function:
  • aws lambda invoke --function-name my-function output.json

CloudFormation Deployment

  • Deploy a stack:
  • aws cloudformation deploy --stack-name my-stack --template-file template.yaml
  • Delete a stack:
  • aws cloudformation delete-stack --stack-name my-stack

4. Automating Tasks with AWS CLI and Bash Scripts

AWS CLI allows users to automate workflows using scripts. Below is an example script to start and stop EC2 instances at scheduled intervals:

#!/bin/bash
INSTANCE_ID="i-1234567890abcdef0"
# Start instance
aws ec2 start-instances --instance-ids $INSTANCE_ID
echo "EC2 Instance $INSTANCE_ID started."
# Wait 60 seconds before stopping
sleep 60
# Stop instance
aws ec2 stop-instances --instance-ids $INSTANCE_ID
echo "EC2 Instance $INSTANCE_ID stopped."

Make the script executable:

chmod +x manage_ec2.sh

Run the script:

./manage_ec2.sh

5. Best Practices for AWS CLI Usage

  • Use IAM Roles: Avoid storing AWS credentials locally. Use IAM roles for security.
  • Enable MFA: Add Multi-Factor Authentication for additional security.
  • Rotate Access Keys Regularly: If using access keys, rotate them periodically.
  • Use Named Profiles: Manage multiple AWS accounts efficiently using profiles.
  • aws configure --profile my-profile
  • Log Command Outputs: Store logs for debugging and monitoring purposes.
  • aws s3 ls > s3_log.txt

Final Thoughts

AWS CLI is a powerful tool that enhances productivity by automating cloud operations. Whether you’re managing EC2 instances, deploying Lambda functions, or securing IAM users, AWS CLI simplifies interactions with AWS services.

By following best practices and leveraging automation, you can optimize your cloud management and improve efficiency. Start experimenting with AWS CLI today and take full control of your AWS environment!

WEBSITE: https://www.ficusoft.in/aws-training-in-chennai/

Comments

Popular posts from this blog

Best Practices for Secure CI/CD Pipelines

What is DevSecOps? Integrating Security into the DevOps Pipeline

SEO for E-Commerce: How to Rank Your Online Store