How To set up AWS CLI profiles and configure AWS CDK with AWS credentials?

Sharing is Caring:

To set up AWS CLI profiles and configure AWS CDK with AWS credentials, follow this step-by-step guide.


1. Create AWS CLI Profiles

AWS CLI uses profiles to manage multiple sets of AWS credentials.

Step 1: Configure AWS CLI Profile

Run the following command in your terminal (or command prompt):

aws configure --profile myprofile

Replace myprofile with any profile name you want.

You will be prompted to enter:

  • AWS Access Key ID: (Get this from AWS IAM)
  • AWS Secret Access Key: (Get this from AWS IAM)
  • Default region name: (e.g., us-east-1, us-west-2, etc.)
  • Default output format: (Choose json, text, or table, default is json)

Example:

AWS Access Key ID [None]: AKIAEXAMPLEID
AWS Secret Access Key [None]: wJalrXEXAMPLEKEY
Default region name [None]: us-east-1
Default output format [None]: json

Step 2: Verify Profile

To check if the profile is created, run:

aws configure list --profile myprofile

or list all profiles:

cat ~/.aws/credentials

(on Windows, use notepad %USERPROFILE%\.aws\credentials)


2. Export AWS CLI Profile for Use

On Linux/macOS

To set a profile for use in the current session:

export AWS_PROFILE=myprofile

To permanently set the profile, add the above line to ~/.bashrc or ~/.zshrc.

On Windows (PowerShell)

$env:AWS_PROFILE="myprofile"

On Windows (Command Prompt)

set AWS_PROFILE=myprofile

3. Connect AWS CDK with AWS CLI Profile

AWS CDK (Cloud Development Kit) uses AWS credentials from AWS CLI profiles.

Step 1: Install AWS CDK

If you haven’t installed AWS CDK yet, install it using npm:

npm install -g aws-cdk

Verify installation:

cdk --version

Step 2: Bootstrap AWS CDK Environment

Run the following command using your AWS CLI profile:

cdk bootstrap --profile myprofile

This will set up AWS resources required for deploying AWS CDK apps in your account.

Step 3: Create a New AWS CDK Project

Run:

mkdir my-cdk-app
cd my-cdk-app
cdk init app --language=typescript

This initializes a new CDK project in TypeScript (you can also use Python, Java, or C#).

Step 4: Deploy AWS CDK App Using a Specific AWS Profile

When deploying or synthesizing a stack, use:

cdk deploy --profile myprofile

or

cdk synth --profile myprofile

4. Verify AWS CDK is Using the Correct Profile

To check which credentials AWS CDK is using:

aws sts get-caller-identity --profile myprofile

This should return the AWS account and IAM user details.


5. Switch Between AWS CLI Profiles

To change the profile in a session:

export AWS_PROFILE=another-profile  # Linux/macOS
$env:AWS_PROFILE="another-profile"  # Windows (PowerShell)
set AWS_PROFILE=another-profile      # Windows (Command Prompt)

To reset and use the default profile:

unset AWS_PROFILE  # Linux/macOS
Remove-Item Env:\AWS_PROFILE  # Windows (PowerShell)
set AWS_PROFILE=  # Windows (Command Prompt)

Summary

✅ Created AWS CLI profiles using aws configure
✅ Exported AWS_PROFILE for session-wide use
✅ Installed and set up AWS CDK
✅ Bootstrapped AWS CDK with cdk bootstrap
✅ Deployed AWS CDK stacks with a specific profile

Sharing is Caring:
Asad
Asad

Hi, I'm Asad Mahmood, a passionate Cloud Technology Enthusiast. With a fervent interest in the transformative power of cloud computing, I'm dedicated to exploring its capabilities and leveraging them to drive innovation and efficiency in the digital landscape.

Articles: 14

Leave a Reply

Your email address will not be published. Required fields are marked *