ローカル環境のAWS接続情報(CLI, プログラム)にAWS SSOを利用する

2021-07-24
山下 徳光
#
AWS
#
#

こんにちは、山下です。

今回はAWS SSOを利用してAWS Profileを設定する方法をご紹介します。一般的な以下の方法と比べて、SSOユーザーでIAMを一元管理できる、有効期限付きの認証情報を都度発行するためセキュリティが高くなる、といったメリットがあります。

一般的なローカル環境の認証情報設定:

  1. プログラムに直接指定
  2. 環境変数 AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY を設定
  3. 環境変数 AWS_PROFILEを設定

詳細は公式ドキュメントまで。

AWS Single Sign-On を使用するための AWS CLI の設定 - AWS Command Line Interface

前提

$ aws --version
aws-cli/2.2.13 Python/3.8.8 Darwin/20.5.0 exe/x86_64 prompt/off

初期設定

aws configure sso コマンドで、SSOのURLやロールを指定することでプロファイルを生成します。

$ aws configure sso
SSO start URL [None]: https://d-xxxxxxxxxx.awsapps.com/start
SSO Region [None]: ap-northeast-1
Attempting to automatically open the SSO authorization page in your default browser.
If the browser does not open or you wish to use a different device to authorize this request, open the following URL:

https://device.sso.ap-northeast-1.amazonaws.com/

Then enter the code:

XXXX-XXXX
There are 4 AWS accounts available to you.
Using the account ID 000000000000
There are 2 roles available to you.
Using the role name "AWSAdministratorAccess"
CLI default client Region [ap-northeast-1]:
CLI default output format [json]:
CLI profile name [AWSAdministratorAccess-000000000000]: blog-profile

To use this profile, specify the profile name using --profile, as shown:

aws s3 ls --profile blog-profile

CLIからの接続

通常の aws configure による設定と同様にprofileを指定することで動作します。

$ aws s3 ls --profile blog-profile

プログラムからの接続

プログラムからの接続は従来のProfile指定と異なり、専用のCredentials Providerを使用する必要があります。

AWS SDK for JavaScript v3 の実装例を示します。実際は、ローカル環境はSSO, 本番環境は指定なし(サービスロール)になりますので、環境変数などで初期化の設定を分岐することになると思います。

import { S3 } from '@aws-sdk/client-s3';
import { fromSSO } from '@aws-sdk/credential-provider-sso';

const s3 = new S3({
  region: 'ap-northeast-1',
  credentials: fromSSO({
    profile: 'blog-profile',
  }),
});

(async () => {
  const resp = await s3.listBuckets({});
  console.log(resp);
})();

@aws-sdk/credential-provider-sso | AWS SDK for JavaScript v3

トークンのリフレッシュ

発行した認証情報の有効期限が切れると、以下のようなエラーが発生します。

CredentialsProviderError: The SSO session associated with this profile has expired or is otherwise invalid. To refresh this SSO session run aws sso login with the corresponding profile.

その場合、 aws sso login コマンドで認証情報をリフレッシュすることで解消します。

$ aws sso login --profile blog-profile

おまけ

aws configure sso コマンドを実行することで、以下のファイルが生成されていました。永続的な認証情報は設定しないので ~/.aws/credentials に設定項目は追加されず、代わりに ~/.aws/sso/cache/* に一時認証情報が設定されるみたいです。

$ cat ~/.aws/config
...
[profile blog-profile]
sso_start_url = https://d-0000000000.awsapps.com/start
sso_region = ap-northeast-1
sso_account_id = 000000000000
sso_role_name = AWSAdministratorAccess
region = ap-northeast-1
output = json

$ ls -1 ~/.aws/sso/cache
0000000000000000000000000000000000000000.json
botocore-client-id-ap-northeast-1.json


こんにちは、山下です。

今回はAWS SSOを利用してAWS Profileを設定する方法をご紹介します。一般的な以下の方法と比べて、SSOユーザーでIAMを一元管理できる、有効期限付きの認証情報を都度発行するためセキュリティが高くなる、といったメリットがあります。

一般的なローカル環境の認証情報設定:

  1. プログラムに直接指定
  2. 環境変数 AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY を設定
  3. 環境変数 AWS_PROFILEを設定

詳細は公式ドキュメントまで。

AWS Single Sign-On を使用するための AWS CLI の設定 - AWS Command Line Interface

前提

$ aws --version
aws-cli/2.2.13 Python/3.8.8 Darwin/20.5.0 exe/x86_64 prompt/off

初期設定

aws configure sso コマンドで、SSOのURLやロールを指定することでプロファイルを生成します。

$ aws configure sso
SSO start URL [None]: https://d-xxxxxxxxxx.awsapps.com/start
SSO Region [None]: ap-northeast-1
Attempting to automatically open the SSO authorization page in your default browser.
If the browser does not open or you wish to use a different device to authorize this request, open the following URL:

https://device.sso.ap-northeast-1.amazonaws.com/

Then enter the code:

XXXX-XXXX
There are 4 AWS accounts available to you.
Using the account ID 000000000000
There are 2 roles available to you.
Using the role name "AWSAdministratorAccess"
CLI default client Region [ap-northeast-1]:
CLI default output format [json]:
CLI profile name [AWSAdministratorAccess-000000000000]: blog-profile

To use this profile, specify the profile name using --profile, as shown:

aws s3 ls --profile blog-profile

CLIからの接続

通常の aws configure による設定と同様にprofileを指定することで動作します。

$ aws s3 ls --profile blog-profile

プログラムからの接続

プログラムからの接続は従来のProfile指定と異なり、専用のCredentials Providerを使用する必要があります。

AWS SDK for JavaScript v3 の実装例を示します。実際は、ローカル環境はSSO, 本番環境は指定なし(サービスロール)になりますので、環境変数などで初期化の設定を分岐することになると思います。

import { S3 } from '@aws-sdk/client-s3';
import { fromSSO } from '@aws-sdk/credential-provider-sso';

const s3 = new S3({
  region: 'ap-northeast-1',
  credentials: fromSSO({
    profile: 'blog-profile',
  }),
});

(async () => {
  const resp = await s3.listBuckets({});
  console.log(resp);
})();

@aws-sdk/credential-provider-sso | AWS SDK for JavaScript v3

トークンのリフレッシュ

発行した認証情報の有効期限が切れると、以下のようなエラーが発生します。

CredentialsProviderError: The SSO session associated with this profile has expired or is otherwise invalid. To refresh this SSO session run aws sso login with the corresponding profile.

その場合、 aws sso login コマンドで認証情報をリフレッシュすることで解消します。

$ aws sso login --profile blog-profile

おまけ

aws configure sso コマンドを実行することで、以下のファイルが生成されていました。永続的な認証情報は設定しないので ~/.aws/credentials に設定項目は追加されず、代わりに ~/.aws/sso/cache/* に一時認証情報が設定されるみたいです。

$ cat ~/.aws/config
...
[profile blog-profile]
sso_start_url = https://d-0000000000.awsapps.com/start
sso_region = ap-northeast-1
sso_account_id = 000000000000
sso_role_name = AWSAdministratorAccess
region = ap-northeast-1
output = json

$ ls -1 ~/.aws/sso/cache
0000000000000000000000000000000000000000.json
botocore-client-id-ap-northeast-1.json


株式会社グランドリームでは、AWSを駆使した開発からUI/UXデザインまで、Webアプリケーションに関するすべての要望に応えます。
まずは一度お気軽にご相談ください。

お問い合わせはこちら