Voiced by Amazon Polly |
Overview
The AWS CLI profile is a crucial component for AWS users, allowing them to manage multiple AWS accounts and access resources seamlessly. AWS Command Line Interface (CLI) profile essentially encapsulates authentication credentials and configuration settings, streamlining the interaction with different AWS environments. With the ability to define named profiles, users can effortlessly switch between accounts, regions, and even access keys, enhancing flexibility and security in their command-line interactions.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
Introduction
Welcome to the realm of AWS Command Line Interface (CLI) profiles, a pivotal element in the toolkit of AWS enthusiasts and cloud professionals.
In this blog, we embark on a journey to unravel the significance of AWS CLI profiles, exploring their role in simplifying authentication configuration and empowering users to navigate the AWS landscape with finesse. Whether you’re a seasoned cloud engineer or just stepping into the world of AWS, understanding and harnessing the capabilities of AWS CLI profiles is a key step in optimizing your command-line experience. Let’s dive in!
AWS CLI Profiles
In AWS CLI, profiles are named configurations that streamline the management of multiple AWS settings. They include access keys, regions, and output preferences, enabling users to switch between AWS accounts and regions effortlessly. Profiles are a key feature for efficiently handling authentication and customizing configurations, enhancing flexibility and security in AWS CLI operations.
Steps to Set up the AWS CLI Profiles
- Open your terminal or command prompt.
- Run the following command, replacing <profile-name> with the desired name for your AWS CLI profile:
1 |
aws configure --profile <profile-name> |
3. You will be prompted to enter the AWS Access Key ID, Secret Access Key, default region, and output format. Provide the necessary information when prompted.
4. After entering the required information, the AWS CLI will create a profile with the specified name in your AWS configuration file (~/.aws/config on Linux or macOS or %UserProfile%\.aws\config on Windows).
Listing all the AWS CLI Profiles
To list all the AWS CLI profiles, run the command “aws configure list-profiles”
Using AWS CLI Profiles
You can use the AWS CLI profiles with all the aws commands using the flag “—profile”. For example: “aws s3 ls –profile <profile-name>”
You can also export the environment variable “AWS_PROFILE” with the profile name, and the aws cli would pick that profile for subsequent aws cli calls.
For more information on using AWS CLI profiles, please refer this link.
The Challenge
While working on a client project with multiple AWS accounts, I set up multiple AWS CLI profiles for each. But switching between them was a very tedious task. Whenever I opened a new command prompt, I had to manually export the “AWS_PROFILE” variable with the appropriate profile value. Also, when I wanted to switch accounts, I had to export the “AWS_PROFILE” variable again. Doing this multiple times a day was quite frustrating. Then, I took the matter into my own hands and wrote a bash script to tackle this challenge.
The Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#!/bin/bash # Listing AWS Profiles profiles=$(aws configure list-profiles) # Count the number of lines in the output num_lines=$(echo "$profiles" | wc -l) # Check if there are any profiles if [ "$num_lines" -eq 0 ]; then echo "No AWS profiles found." else echo "Number of AWS Profiles: $num_lines" # Loop through the profiles using a for loop for i in $(seq 1 $num_lines); do profile_name=$(echo "$profiles" | sed -n "${i}p") echo "$i: $profile_name" done read -p "Enter the number of the AWS profile you want to use: " profile_number # Check if the input is a valid number if [[ ! $profile_number =~ ^[0-9]+$ || $profile_number -lt 1 || $profile_number -gt $num_lines ]]; then echo "Invalid input. Please enter a valid number." exit 1 fi # Get the selected profile name selected_profile=$(echo "$profiles" | sed -n "${profile_number}p") export AWS_PROFILE="${selected_profile}" echo "Selected AWS Profile: $selected_profile" fi |
How to execute it?
Copy the script from above, make a file in “/usr/local/bin/” with the “acps” (short form of AWS CLI profile selector), and paste the contents in the file. Then make it an executable using “chmod +x <filename>” and you are ready. Run “. acps” from the command prompt, and the script will execute.
Note: You need the “.” in front of the command to export the variable.
Conclusion
In conclusion, the custom Bash script introduced in this blog revolutionizes AWS CLI profile management. As Cloud Engineers, optimizing workflows is key, and this script provides an efficient solution. Seamlessly integrating profile selection into the command prompt enhances productivity and simplifies the AWS CLI experience, making it an invaluable tool for cloud professionals.
Drop a query if you have any questions regarding AWS CLI and we will get back to you quickly.
Making IT Networks Enterprise-ready – Cloud Management Services
- Accelerated cloud migration
- End-to-end view of the cloud environment
About CloudThat
CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.
CloudThat is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 650k+ professionals in 500+ cloud certifications and completed 300+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, Microsoft Gold Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, and many more.
To get started, go through our Consultancy page and Managed Services Package, CloudThat’s offerings.
FAQs
1. How do I create a new AWS CLI profile?
ANS: – You can create a new AWS CLI profile using the aws configure –profile command, followed by the desired profile name. This will prompt you to enter the required configuration details for the new profile.
2. How can I list all existing AWS CLI profiles on my system?
ANS: – You can use the command aws configure list-profiles to list all AWS CLI profiles.
3. Is it possible to update the configuration of an existing AWS CLI profile?
ANS: – Yes, you can update the configuration of an existing AWS CLI profile by using the aws configure –profile <profile-name> command. It will prompt you to modify the existing configuration.
WRITTEN BY Avinash Kumar
Avinash Kumar is a Senior Research Associate at CloudThat, specializing in Cloud Engineering, NodeJS development, and Google Cloud Platform. With his skills, he creates innovative solutions that meet the complex needs of today's digital landscape. He's dedicated to staying at the forefront of emerging cloud technologies.
Click to Comment