この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
本日(2022/03/29)リリースされたCDKv2.18.0、二日前リリースされたv1.150.0より、CDKのCLIにてSSO profileを使っての認証ができるようになりました!
SSO profileって何?
各アカウント上にAWS SSOによって作成されるIAM Roleの権限でAWS CLIを使用するための名前付きプロファイルです。普通の(=SSOではない)名前付きプロファイルとは設定項目が異なります。
要は、SSOの各ユーザーに対して各アカウントへのアクセス権限を設定しますが、その設定した権限でAWS CLIを使いたいという時に使うプロファイルです。
このSSO profileですが、AWS CLI以外の他のツールでもサポートが広がっています。例えばTerraformで使う場合は以下をご参照いただければと思います。
そして今回、CDKでもサポートされるようになったというわけです。個人的には待望のアップデートです?
やってみた
SSO profileを作成する
~/.aws/config
に以下のようなprofileを記載してください。
[profile my-dev-profile]
sso_start_url = https://my-sso-portal.awsapps.com/start
sso_region = ap-northeast-1
sso_account_id = 123456789011
sso_role_name = AdministratorAccess
region = ap-northeast-1
output = json
もちろん手動でこの書式でprofileを書いても良いですが、aws configure sso
という便利コマンドを使うと質問回答形式で、かつ入力補完も付くのでより簡単に作成できます。
aws configure sso コマンド実行例
$ aws configure sso
SSO start URL [None]: https://my-sso-portal.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:
ZKTN-KTCZ
SSO Regionまで答えるとブラウザで自動でSSOポータルが開きます。
Profileを作りたいSSOユーザーでログインします。
ログイン完了すると以下画面になるので、Allowを押下します。
以下画面に遷移したらこのブラウザタブは閉じてOKです。
ターミナルに戻ります。ログインしたSSOユーザーがスイッチロールできるアカウント一覧が出てくるので、Profileを作りたいアカウントを選択します。
その後も、使いたいロール、デフォルトリージョン、デフォルトアウトプットフォーマット、profile名を順に訊かれます。
There are 3 AWS accounts available to you.
Using the account ID 123456789012
There are 2 roles available to you.
Using the role name "AdministratorAccess"
CLI default client Region [ap-northeast-1]:
CLI default output format :
CLI profile name [AdministratorAccess-123456789012]: test-sso-profile
To use this profile, specify the profile name using --profile, as shown:
aws s3 ls --profile test-sso-profile
ここまで完了すれば、~/.aws/config
末尾に前述の書式のProfileが追記されています。
CDKプロジェクトの作成
% npx cdk init sample-app --language typescript
Applying project template sample-app for typescript
# Welcome to your CDK TypeScript project
You should explore the contents of this project. It demonstrates a CDK app with an instance of a stack (`HogeStack`)
which contains an Amazon SQS queue that is subscribed to an Amazon SNS topic.
The `cdk.json` file tells the CDK Toolkit how to execute your app.
## Useful commands
* `npm run build` compile typescript to js
* `npm run watch` watch for changes and compile
* `npm run test` perform the jest unit tests
* `cdk deploy` deploy this stack to your default AWS account/region
* `cdk diff` compare deployed stack with current state
* `cdk synth` emits the synthesized CloudFormation template
Initializing a new git repository...
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Executing npm install...
npm WARN deprecated source-map-url@0.4.1: See https://github.com/lydell/source-map-url#deprecated
npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated source-map-resolve@0.5.3: See https://github.com/lydell/source-map-resolve#deprecated
npm WARN deprecated sane@4.1.0: some dependency vulnerabilities fixed, support for node < 10 dropped, and newer ECMAScript syntax/features added
✅ All done!
% npx cdk version
2.18.0 (build 75c90fa)
SSOユーザーでログイン
作ったSSO profileを使うためには、そのロールにassume roleするSSOユーザーとしてまずはログインする必要があります。この部分もCLIでできます。
※ 「SSO profileを作成する」項でaws configure sso
コマンドを使って設定した場合は、すでにログイン状態になっているのでこの工程は不要です。
% aws sso login --profile my-dev-profile
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:
QVPX-DFFV
ここまで来ると aws configure sso
コマンドと同様SSOポータルが開くので、画面に沿ってログインし、Allowを押下してターミナルに戻ってきてください。
CDKデプロイ
% npx cdk deploy --profile my-dev-profile
✨ Synthesis time: 5.12s
This deployment will make potentially sensitive changes according to your current security approval level (--require-approval broadening).
Please confirm you intend to make the following modifications:
IAM Statement Changes
┌───┬──────────────────┬────────┬─────────────────┬───────────────────────────────┬────────────────────────────────┐
│ │ Resource │ Effect │ Action │ Principal │ Condition │
├───┼──────────────────┼────────┼─────────────────┼───────────────────────────────┼────────────────────────────────┤
│ + │ ${HogeQueue.Arn} │ Allow │ sqs:SendMessage │ Service:sns.amazonaws.com │ "ArnEquals": { │
│ │ │ │ │ │ "aws:SourceArn": "${HogeTopi │
│ │ │ │ │ │ c}" │
│ │ │ │ │ │ } │
└───┴──────────────────┴────────┴─────────────────┴───────────────────────────────┴────────────────────────────────┘
(NOTE: There may be security-related changes not in this list. See https://github.com/aws/aws-cdk/issues/1299)
Do you wish to deploy these changes (y/n)? y
HogeStack: deploying...
[0%] start: Publishing 6d931bf6490ec3a2a58597879d17350d572420230ec926fd10827c0aa90c552a:current_account-current_region
[100%] success: Published 6d931bf6490ec3a2a58597879d17350d572420230ec926fd10827c0aa90c552a:current_account-current_region
HogeStack: creating CloudFormation changeset...
✅ HogeStack
✨ Deployment time: 100.89s
Stack ARN:
arn:aws:cloudformation:ap-northeast-1:123456789012:stack/HogeStack/bfa5a040-af2b-11ec-8c7c-0e0fc0d6447d
✨ Total time: 106.01s
ログアウト
% aws sso logout --profile my-dev-profile
環境変数を使うのがおすすめ
上記では各コマンドで --profile
オプションでSSO profileを指定していましたが、環境変数AWS_PROFILE
を一度設定して--profile
オプションは使わないやり方のほうが便利だと思います。どうせ一連の処理で使うプロファイルは同じだと思うので。
% export AWS_PROFILE=my-dev-profile
% aws sso login
% npx cdk deploy
% aws sso logout