How to Configure and Setup AWS CLI and Key Pairs
Automating AWS infrastructure requires proper setup of the AWS Command Line Interface (CLI), secure key management, and an understanding of core components like AMIs. This guide walks through configuring the AWS CLI and generating and managing EC2 key pairs.
Contents
Installing the AWS CLI
The AWS CLI is the primary tool for interacting with AWS services programmatically. Below are installation instructions for different operating systems.
Linux/macOS installation
- Install unzip(if not installled) and download the latest AWS CLI version:
sudo apt install unzip
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
- Extract the packages:
unzip awscliv2.zip
- Run the installer with sudo:
sudo ./aws/install
- Verify the installation:
aws --version
Your expected output should look like this:
aws-cli/2.27.4 Python/3.13.2 Linux/6.11.0-9-generic exe/x86_64.ubuntu.24
Windows installation
- Download the AWS CLI MSI installer.
- Run the installer and follow the prompts.
- Open PowerShell and verify installation:
aws --version
Alternatively, you can use a preconfigured CloudRay automation script to install and configure the AWS CLI across multiple environments for consistency.
Configuring AWS CLI
Before using the AWS CLI, you need to configure authentication credentials.
- Login to the AWS IAM Console
- Navigate to Users → Select your IAM user → Security credentials.

-
Under Access keys, click Create access key.
-
Choose CLI (Command Line Interface) as the use case and click Next.

- You can give your access key tag (optional) and click “create access key”

- You can copy the access and secret access key on the AWS console

Additionally, you can download the .csv file or copy the keys manually
- Run the configuration wizard:
aws configure
You will be prompted for:
- AWS Access Key ID - A unique identifier for your IAM user
- AWS Secret Access Key - A secret password tied to your access key
- Default region name - The AWS region where resources will be created (e.g., us-east-1)
- Default output format - How responses are displayed (json, text, or table)

- Verify that the CLI can authentication with AWS:
aws sts get-caller-identity

A successful response includes your AWS account ID and IAM user ARN.
Managing EC2 Key pairs
SSH key pairs are essential for secure access to your EC2 instances. This section covers creating, managing, and using key pairs with AWS CLI. Here is how to create a new key pair using AWS CLI:
- First, generate a key pair using AWS CLI:
aws ec2 create-key-pair \
--key-name "my-production-key" \
--key-type ed25519 \
--query 'KeyMaterial' \
--output text > my-production-key.pem
- Set proper file permissions:
chmod 400 my-production-key.pem
Here are what all the key parameters represents:
--key-name
: Unique identifier for your key--key-type
: Choose betweenrsa
(default) or more secureed25519
KeyMaterial
: The private key content (saved to .pem file)
Additionally, here is how to list all available key pairs in your region:
aws ec2 describe-key-pairs \
--query 'KeyPairs[*].KeyName' \
--output table

This shows the keys present in your AWS user account
Finally, here is how to delete a key pair:
- First, verify no instances are using the key:
aws ec2 describe-instances \
--filters "Name=key-name,Values=my-production-key" \
--query "Reservations[].Instances[].InstanceId"
- Delete the key pair:
aws ec2 delete-key-pair --key-name "my-production-key"
NOTE
You can locate Amazon Machine Images (AMIs) in the EC2 Console under AMIs in the left navigation. Use the search filters to find official AWS-provided images or community AMIs. For production environments, always verify AMI sources and use the most recent stable versions.
While manual AWS CLI configuration works for individual setups, managing infrastructure at scale requires automation. CloudRay provides centralised management for Bash scripts, enabling teams to securely automate and schedule AWS operations like instance provisioning, key rotation, and infrastructure monitoring.
For a practical implementation of these concepts, see our guide on Automating AWS Infrastructure with Bash and CLI which covers EC2 lifecycle management, automated backups, and monitoring solutions.