iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐙

AWS CLI Configuration and Profile Switching

に公開

Introduction to AWS CLI

What is AWS CLI?

AWS CLI is a tool for managing AWS services from the command line.
By installing the AWS CLI, you will be able to use the aws command.

# Example of the aws command
aws s3 ls

To use it, you need to configure the following two items:

  • AWS Access Key ID
  • Secret Access Key

Installing AWS CLI

The official documentation provides installation instructions for each OS.

For macOS, you can install it using the following commands:

curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg ./AWSCLIV2.pkg -target /

If the version is displayed with aws --version, the installation was successful.

$ aws --version
aws-cli/2.7.2 Python/3.9.11 Darwin/21.6.0 exe/x86_64 prompt/off

Checking Current Settings

aws configure list

alt aws configure list

Configuring Connection Information

For default credentials

aws configure

For configuring credentials separately from the default

aws configure --profile profile-name

Executing the above will add the settings to ~/.aws/config and ~/.aws/credentials.

  • Contents of ~/.aws/config
    The region is written here.

    $ cat ~/.aws/config
    
    [default] # profile name
    region = ap-northeast-1
    [test] # profile name
    region = ap-northeast-1
    
  • Contents of ~/.aws/credentials
    The Access Key ID and Secret Access Key are written here.

    $ cat ~/.aws/credentials
    
    [default] # profile name
    aws_access_key_id = AAAAAAAAAAAAAAAAAAAA
    aws_secret_access_key = BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
    
    [test] # profile name
    aws_access_key_id = AAAAAAAAAAAAAAAAAAAA
    aws_secret_access_key = BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
    

Switching Settings

Switching Profiles

You can switch profiles by exporting the profile name to the environment variable AWS_DEFAULT_PROFILE.

export AWS_DEFAULT_PROFILE=profile-name
  • Example

    export AWS_DEFAULT_PROFILE=prod
    

    alt Switching Profiles

Switching profiles only during command execution

aws s3 ls --profile profile-name

Difference between AWS_DEFAULT_PROFILE and AWS_PROFILE

The profile to be used is managed by the following environment variables:

  • AWS_DEFAULT_PROFILE
  • AWS_PROFILE

If profiles are set in both environment variables, AWS_DEFAULT_PROFILE takes precedence.

📌 It is recommended to set the profile in AWS_DEFAULT_PROFILE.

GitHubで編集を提案

Discussion