Day 10 - AWS CLI

Day 10 - AWS CLI

What is AWS CLI?

AWS CLI stands for Amazon Web Services Command Line Interface. It is a unified tool that allows you to manage your AWS services and resources from the command line. AWS CLI provides commands for a wide range of AWS services, allowing you to automate and script various tasks and operations.

Why did AWS CLI come into existence?

Overall, the AWS CLI simplifies the management and administration of AWS resources by providing a command-line interface that offers efficiency, automation capabilities, and deep integration with AWS services and workflows.

Here are several reasons why the AWS CLI was created:

  • Automation and Scripting: AWS CLI allows users to automate common tasks and operations across AWS services. This is particularly useful for tasks like provisioning resources, configuring settings, and managing deployments in a streamlined, programmatic way.

  • Flexibility and Control: It provides granular control over AWS resources without needing to use the AWS Management Console. This is beneficial for users who prefer command-line interfaces for their efficiency and flexibility.

  • Integration with DevOps Processes: Many organizations use AWS CLI as part of their DevOps workflows. It integrates well with continuous integration and continuous deployment (CI/CD) pipelines, allowing teams to manage infrastructure as code and automate deployments.

  • Multi-Platform Support: AWS CLI is designed to work on various platforms including Windows, macOS, and Linux, making it accessible to a wide range of developers and administrators.

  • Extensibility and Plugins: It supports plugins and extensions that extend its functionality beyond the core AWS services. This enables users to customize and enhance their AWS CLI experience based on specific needs.

  • Access to AWS Services: AWS CLI provides direct access to AWS services through commands, allowing users to interact with AWS resources, manage IAM permissions, configure networking, and more.

Connect to to your AWS account from AWS CLI

If you've created an IAM user from the AWS Management Console and you need to find the Access Key ID and Secret Access Key for that user, you can follow these steps:

Finding Access Key ID and Secret Access Key for an IAM User

  1. Navigate to IAM: From the services menu, select "IAM" under "Security, Identity, & Compliance".

  2. Locate the IAM User:

    In the IAM console, click on "Users" in the left navigation pane.

    Find the IAM user you created from the list of users.

  3. Access Key Management:

    • Click on the IAM user's name to open their details.

    • Go to the "Security credentials" tab where you manage the user's access keys, passwords, and MFA devices.

  4. Manage Access Keys:

    • Under the "Access keys" section, you should see any existing access keys associated with the user.

    • If no access keys are listed or you want to create a new set, click on the "Create access key" button.

  5. View or Download Credentials:

    • After creating an access key, you will see the Access Key ID displayed immediately.

    • You will also have the option to download the Secret Access Key as a .csv file.

To connect to your AWS account using AWS CLI (Command Line Interface), follow these simple steps:

  • Install AWS CLI: If you haven't already installed AWS CLI, you can download it and follow installation instructions from here.

  • Configure AWS CLI: After installation, configure AWS CLI with your AWS credentials. This includes your AWS Access Key ID, Secret Access Key, default region, and output format. You can do this by running:

      aws configure
    

    Follow the prompts to enter your credentials and configuration settings.

Validate by Test Connection: You can test your connection to AWS by running a simple command, such as:

aws sts get-caller-identity

Automation and Scripting is one of the most valid reasons that CLI was made

Hence we will be showing one example

Step-by-Step Example: Automating EC2 Instance Provisioning with AWS CLI

Example Scenario:

Let's say we want to automate the provisioning of two EC2 instances:

  • Instance 1: t2.micro instance with Amazon Linux 2 AMI, tagged with "Name" and "Environment" tags.

  • Instance 2: t2.micro instance with Ubuntu Server 20.04 LTS AMI, tagged similarly.

    Step 1: Create a Script

    Create a Bash script (launch_ec2_instances.sh, for example) with the following content:

      #!/bin/bash
      # Variables for instance configuration
      AMI_AWS_LINUX_2="ami-0a4a70bd98d1e715f"
      AMI_UBUNTU_20_04="ami-0dba2cb6798deb6d8"
      INSTANCE_TYPE="t2.micro"
      KEY_NAME="your-key-pair-name"
      SECURITY_GROUP_ID="sg-xxxxxxxx"
      SUBNET_ID="subnet-xxxxxxxx"
      TAG_NAME="MyEC2Instance"
      TAG_ENV="Development"
    
      # Launch EC2 instances using AWS CLI
      aws ec2 run-instances \
          --image-id $AMI_AWS_LINUX_2 \
          --instance-type $INSTANCE_TYPE \
          --key-name $KEY_NAME \
          --security-group-ids $SECURITY_GROUP_ID \
          --subnet-id $SUBNET_ID \
          --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=$TAG_NAME},{Key=Environment,Value=$TAG_ENV}]"
    
      aws ec2 run-instances \
          --image-id $AMI_UBUNTU_20_04 \
          --instance-type $INSTANCE_TYPE \
          --key-name $KEY_NAME \
          --security-group-ids $SECURITY_GROUP_ID \
          --subnet-id $SUBNET_ID \
          --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=$TAG_NAME},{Key=Environment,Value=$TAG_ENV}]"
    
  • Step 2: Explanation of the Script

    • Variables: Define variables for AMI IDs, instance type, key pair name, security group ID, subnet ID, and tags.

    • AWS CLI Commands: Use aws ec2 run-instances command to launch EC2 instances.

      • --image-id: Specifies the AMI ID of the instance.

      • --instance-type: Specifies the instance type (e.g., t2.micro).

      • --key-name: Specifies the name of the key pair used for SSH access.

      • --security-group-ids: Specifies the security group IDs to associate with the instance.

      • --subnet-id: Specifies the subnet ID for the instance.

      • --tag-specifications: Specifies tags for the instance (e.g., Name and Environment).

    • Step 3: Execute the Script

      Make the script executable:

        codechmod +x launch_ec2_instances.sh
      

      Run the script to launch EC2 instances:

        ./launch_ec2_instances.sh
      

      Step 4: Results

    • The script will launch two EC2 instances:

      • One instance with Amazon Linux 2 AMI.

      • Another instance with Ubuntu Server 20.04 LTS AMI.

    • Instances will be tagged with "Name" and "Environment" tags as specified.