MOCs

Overview

The AWS CLI is an open-source tool that enables you to interact with AWS services using commands in your command-line shell. With minimal configuration, the AWS CLI enables you to start running commands that implement functionality equivalent to that provided by the browser-based AWS Management Console.

Documenation

For detailed installation instructions for various platforms, refer to the official AWS CLI documentation.

Tips and Tricks

  • Using Profiles for Multiple Accounts:
    If you manage multiple AWS accounts, you can configure named profiles for each.
aws configure --profile user2
  • Specifying Output Formats:
    You can specify the output format for the commands. The default is JSON, but you can also specify text or table.
aws s3 ls --output table

Installation

AWS CLI can be installed using pip, the Python package manager, or by downloading and running the installer from AWS.

bash pip install awscli

Configuration

Before you can run any commands, you need to configure the AWS CLI with your credentials.

aws configure

This command prompts you for four pieces of information:

  • AWS Access Key ID
  • AWS Secret Access Key
  • Default region name
  • Default output format

EC2 Management

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

S3 Operations

  • List S3 Buckets:
aws s3 ls
  • Copy File to S3 Bucket:
aws s3 cp my-file.txt s3://my-bucket/
  • Download File from S3 Bucket:
aws s3 cp s3://my-bucket/my-file.txt my-file.txt

IAM Management

  • List IAM Users:
aws iam list-users
  • Create IAM User:
aws iam create-user --user-name my-new-user

RDS Operations

  • List RDS Instances:
aws rds describe-db-instances
  • Create RDS Instance:
aws rds create-db-instance --db-instance-identifier mydbinstance --db-instance-class db.t2.micro --engine mysql --master-username myawsuser --master-user-password myawsuserpassword --allocated-storage 20

Lambda Functions

  • List Lambda Functions:
aws lambda list-functions
  • Create Lambda Function:
aws lambda create-function --function-name my-function --zip-file fileb://function.zip --handler index.handler --runtime nodejs12.x --role arn:aws:iam::123456789012:role/execution_role

CloudFormation

  • List CloudFormation Stacks:
aws cloudformation describe-stacks
  • Create CloudFormation Stack:
aws cloudformation create-stack --stack-name my-stack --template-body file://my-template.yaml

CloudWatch

  • List CloudWatch Alarms:
aws cloudwatch describe-alarms
  • Put CloudWatch Metric Alarm:
aws cloudwatch put-metric-alarm --alarm-name my-alarm --metric-name CPUUtilization --namespace AWS/EC2 --statistic Average --period 300 --threshold 70 --comparison-operator GreaterThanThreshold --dimensions Name=InstanceId,Value=i-1234567890abcdef0 --evaluation-periods 2 --alarm-actions arn:aws:sns:us-west-2:111122223333:my-sns-topic