AWS CLI Installing and Utilizing ๐Ÿš€

jay75chauhan
6 min readAug 29, 2024

--

The AWS Command Line Interface (CLI) is a unified tool that allows you to manage and automate various AWS services using commands in your terminal. It simplifies the process of working with AWS by providing a powerful way to control and script AWS services.

1. Installing AWS CLI

To install the AWS CLI on your system:

  • Verify Installation:
aws --version

2. Configuring AWS CLI

Set up your AWS credentials:

aws configure
  • AWS Access Key ID: (Your access key ID)
  • AWS Secret Access Key: (Your secret access key)
  • Default region name: (e.g., us-west-2)
  • Default output format: (e.g., json)

3. Common AWS CLI Commands for DevOps Engineers

Here are some essential AWS CLI commands that DevOps engineers frequently use:

EC2 โ€” Elastic Compute Cloud

  • Launch a new EC2 instance:
aws ec2 run-instances --image-id ami-12345678 --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids sg-1a2b3c4d --subnet-id subnet-6e7f829e
  • Describe EC2 instances:
aws ec2 describe-instances
  • Stop an EC2 instance:
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
  • Terminate an EC2 instance:
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0

S3 โ€” Simple Storage Service

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

ECR โ€” Elastic Container Registry

  • Authenticate Docker with ECR:
aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin <aws_account_id>.dkr.ecr.us-west-2.amazonaws.com
  • Create a new ECR repository:
aws ecr create-repository --repository-name my-repo
  • Push a Docker image to ECR:
docker tag my-image:latest <aws_account_id>.dkr.ecr.us-west-2.amazonaws.com/my-repo:latest docker push <aws_account_id>.dkr.ecr.us-west-2.amazonaws.com/my-repo:latest

EKS โ€” Elastic Kubernetes Service

  • Update kubeconfig for EKS cluster:
aws eks --region us-west-2 update-kubeconfig --name my-cluster
  • List EKS clusters:
aws eks list-clusters

CloudFormation

  • Create a CloudFormation stack:
aws cloudformation create-stack --stack-name my-stack --template-body file://my-template.json
  • Describe the CloudFormation stack:
aws cloudformation describe-stacks --stack-name my-stack

IAM โ€” Identity and Access Management

  • Create a new IAM user:
aws iam create-user --user-name my-user
  • Attach a policy to an IAM user:
aws iam attach-user-policy --user-name my-user --policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess

CloudWatch

  • Create a CloudWatch alarm:
aws cloudwatch put-metric-alarm --alarm-name "CPUAlarm" --metric-name "CPUUtilization" --namespace "AWS/EC2" --statistic "Average" --period 300 --threshold 70 --comparison-operator "GreaterThanOrEqualToThreshold" --dimensions "Name=InstanceId,Value=i-1234567890abcdef0" --evaluation-periods 1 --alarm-actions "arn:aws:sns:us-west-2:123456789012:my-sns-topic"

RDS โ€” Relational Database Service

  • Create a new RDS instance:
aws rds create-db-instance --db-instance-identifier mydbinstance --db-instance-class db.t2.micro --engine mysql --master-username admin --master-user-password secret99 --allocated-storage 20
  • Describe RDS instances:
aws rds describe-db-instances

Lambda โ€” Serverless Compute

  • Create a new Lambda function:
aws lambda create-function --function-name my-function --runtime python3.8 --role arn:aws:iam::123456789012:role/execution_role --handler lambda_function.lambda_handler --zip-file fileb://function.zip
  • Invoke a Lambda function:
aws lambda invoke --function-name my-function --payload '{"key1":"value1"}' response.json
  • List all Lambda functions:
aws lambda list-functions

CloudFront โ€” Content Delivery Network

  • Create a CloudFront distribution:
aws cloudfront create-distribution --origin-domain-name mybucket.s3.amazonaws.com
  • List CloudFront distributions:
aws cloudfront list-distributions
  • Invalidate a CloudFront distribution cache:
aws cloudfront create-invalidation --distribution-id EXXXXXXX --paths "/*"

DynamoDB โ€” NoSQL Database

  • Create a DynamoDB table:
aws dynamodb create-table --table-name my-table --attribute-definitions AttributeName=Id,AttributeType=S --key-schema AttributeName=Id,KeyType=HASH --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
  • Put an item into a DynamoDB table:
aws dynamodb put-item --table-name my-table --item '{"Id": {"S": "123"}, "Name": {"S": "ItemName"}}'
  • Scan a DynamoDB table:
aws dynamodb scan --table-name my-table

SNS โ€” Simple Notification Service

  • Create an SNS topic:
aws sns create-topic --name my-topic
  • Subscribe an email to the SNS topic:
aws sns subscribe --topic-arn arn:aws:sns:us-west-2:123456789012:my-topic --protocol email --notification-endpoint myemail@example.com
  • Publish a message to an SNS topic:
aws sns publish --topic-arn arn:aws:sns:us-west-2:123456789012:my-topic --message "Hello World!"

SQS โ€” Simple Queue Service

  • Create an SQS queue:
aws sqs create-queue --queue-name my-queue
  • Send a message to the SQS queue:
aws sqs send-message --queue-url https://sqs.us-west-2.amazonaws.com/123456789012/my-queue --message-body "Hello, this is a test message"
  • Receive messages from the SQS queue:
aws sqs receive-message --queue-url https://sqs.us-west-2.amazonaws.com/123456789012/my-queue

Route 53 โ€” Domain Name System

  • Create a new Route 53 hosted zone:
aws route53 create-hosted-zone --name example.com --caller-reference 2021-01-01-18:47
  • List all Route 53 hosted zones:
aws route53 list-hosted-zones
  • Create a DNS record in Route 53:
aws route53 change-resource-record-sets --hosted-zone-id Z1234567890 --change-batch file://change-batch.json

Elastic Beanstalk โ€” Platform as a Service

  • Create a new Elastic Beanstalk application:
aws elasticbeanstalk create-application --application-name my-app

S3 Glacier โ€” Archival Storage

  • Create a new Glacier vault:
aws glacier create-vault --account-id - --vault-name my-vault
  • Upload an archive to Glacier:
aws glacier upload-archive --vault-name my-vault --archive-description "My archive" --body myfile.txt
  • List archives in a vault:
aws glacier list-multipart-uploads --vault-name my-vault

Secrets Manager โ€” Secrets Management

  • Create a new secret:
aws secretsmanager create-secret --name my-secret --secret-string '{"username":"admin","password":"secret99"}'
  • Retrieve a secret value:
aws secretsmanager get-secret-value --secret-id my-secret
  • Delete a secret:
aws secretsmanager delete-secret --secret-id my-secret

CodePipeline โ€” Continuous Integration/Continuous Delivery

  • Create a new CodePipeline:
aws codepipeline create-pipeline --pipeline file://pipeline-definition.json
  • List all pipelines:
aws codepipeline list-pipelines
  • Start a pipeline execution:
aws codepipeline start-pipeline-execution --name my-pipeline

CodeBuild โ€” Build Service

  • Start a build project:
ws codebuild start-build --project-name my-build-project
  • List build projects:
aws codebuild list-projects
  • Batch get build details:
aws codebuild batch-get-builds --ids build-id

Elasticache โ€” In-Memory Caching

  • Create a new Elasticache cluster:
aws elasticache create-cache-cluster --cache-cluster-id my-cache-cluster --cache-node-type cache.t2.micro --engine redis --num-cache-nodes 1
  • List Elasticache clusters:
aws elasticache describe-cache-clusters
  • Delete an Elasticache cluster:
aws elasticache delete-cache-cluster --cache-cluster-id my-cache-cluster

Kinesis โ€” Real-Time Data Processing

  • Create a new Kinesis stream:
aws kinesis create-stream --stream-name my-stream --shard-count 1
  • List Kinesis streams:
aws kinesis list-streams
  • Put a record into a Kinesis stream:
aws kinesis put-record --stream-name my-stream --partition-key my-key --data "my-data"

Application Load Balancer (ALB)

Create a new ALB:

aws elbv2 create-load-balancer --name my-alb --subnets subnet-12345678 subnet-23456789 --security-groups sg-12345678
  • List ALBs:
aws elbv2 describe-load-balancers
  • Delete an ALB:
aws elbv2 delete-load-balancer --load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-alb/50dc6c495c0c9188

CloudTrail โ€” API Monitoring and Governance

  • Create a new CloudTrail trail:
aws cloudtrail create-trail --name my-trail --s3-bucket-name my-bucket
  • List CloudTrail trails:
aws cloudtrail describe-trails
  • Start logging for a CloudTrail trail:
aws cloudtrail start-logging --name my-trail
  • Stop logging for a CloudTrail trail:
aws cloudtrail stop-logging --name my-trail

Cost Explorer โ€” Cost Management

  • Get cost and usage report:
aws ce get-cost-and-usage --time-period Start=2024-01-01,End=2024-01-31 --granularity MONTHLY --metrics "UnblendedCost"
  • List cost categories:
aws ce list-cost-categories

Conclusion

This extended guide includes additional AWS CLI commands for a broad array of AWS services, covering both fundamental and advanced functionalities. Mastery of these commands will help you efficiently manage and automate AWS resources across various service domains, enhancing your DevOps capabilities.

--

--

No responses yet