インフラ

AWS SSO環境でaws2-wrapを使ってCDK実行時の認証情報コピペを不要にする

awsロゴ

こんにちは、ちゃりおです。

CDKを使うことがあるのですが、現状CDKはSSOのprofileに対応していません。(2021年3月時点)

SSO環境で実行する際は、一時的な認証情報をセットすれば実行できます。
しかし、毎回やるのは面倒です。

そこで、aws2-wrapを使用してSSOの認証情報をCDKで使えるようにして、毎回の認証情報セットの手間をなくします。

sso profile

Contents

CDKのオプション「–profile」でSSOのプロファイルが使えない

sample-appのスタックでcdkコマンドを実行しました。
以下のように、SSOのプロファイルだとエラーがでます。

% cdk diff --profile my-sso-profile                                                                                                                                                                       
Unable to resolve AWS account to use. It must be either configured when you define your CDK or through the environment

issueが上がっているみたいです。

AWS SSO Named Profiles Support #5455

aws2-wrapとは

SSOに対応していないツールでも、SSOの認証情報を使えるようにするツールです。

これは、AWSプロファイルのssoエントリを理解しないツールでAWSシングルサインオン認証情報を簡単に使用できるようにするためのシンプルなスクリプトです。
This is a simple script to make it easier to use AWS Single Sign On credentials with tools that don’t understand the sso entries in an AWS profile.

github aws2-wrap

cdkは通常の認証情報は使えるため、「.aws/config」のcredential_processにセットして使います。

aws2-wrapを使ってCDK実行

前提条件

aws2-wrapをインストール

pipでインストールします。

% pip3 install aws2-wrap==1.1.9
% aws2-wrap -h                                                                                                                                                                                       (git)-[master]
usage: aws2-wrap [-h] [--export | --generate | --process | --exec EXEC] [--profile PROFILE] [--outprofile OUTPROFILE] [--configfile CONFIGFILE] [--credentialsfile CREDENTIALSFILE] ...

positional arguments:
  command               a command that you want to wrap

optional arguments:
  -h, --help            show this help message and exit
  --export              export credentials as environment variables
  --generate            generate credentials file from the input profile
  --process
  --exec EXEC
  --profile PROFILE     the source profile to use for creating credentials
  --outprofile OUTPROFILE
                        the destination profile to save generated credentials
  --configfile CONFIGFILE
                        the config file to append resulting config
  --credentialsfile CREDENTIALSFILE
                        the credentials file to append resulting credentials

.aws/configにcredential_processを設定

以下のコマンドで「credential_process」に必要な認証情報を表示できます。

aws2-wrap --process --profile my-sso-profile

コマンド実行して、認証情報を返ってくることを確認したら.aws/configに設定します。

[profile my-sso-profile]
sso_start_url = https://my-sso.awsapps.com/start
sso_region = ap-southeast-2
sso_account_id = 11111111111
sso_role_name = AdministratorAccess
region = ap-northeast-1
output = json
[profile my-profile]
credential_process = aws2-wrap --process --profile my-sso-profile

設定後credential_processを指定したprofileでもawsコマンドが実行できるようになります。

% aws s3 ls --profile my-profile

エラーが出ていたcdk実行も無事に成功しました。

% cdk diff --profile my-profile                                                                                                                                                                       (git)-[master]
% cdk diff --profile chari                                                                                                                                                                           
Stack SampleStack
IAM Statement Changes
┌───┬────────────────────┬────────┬─────────────────┬───────────────────────────┬────────────────────────────────────────────────────┐
│   │ Resource           │ Effect │ Action          │ Principal                 │ Condition                                          │
├───┼────────────────────┼────────┼─────────────────┼───────────────────────────┼────────────────────────────────────────────────────┤
│ + │ ${SampleQueue.Arn} │ Allow  │ sqs:SendMessage │ Service:sns.amazonaws.com │ "ArnEquals": {                                     │
│   │                    │        │                 │                           │   "aws:SourceArn": "${SampleTopic}"                │
│   │                    │        │                 │                           │ }                                                  │
└───┴────────────────────┴────────┴─────────────────┴───────────────────────────┴────────────────────────────────────────────────────┘
(NOTE: There may be security-related changes not in this list. See https://github.com/aws/aws-cdk/issues/1299)

Conditions
[+] Condition CDKMetadata/Condition CDKMetadataAvailable: {"Fn::Or":[{"Fn::Or":[{"Fn::Equals":[{"Ref":"AWS::Region"},"ap-east-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"ap-northeast-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"ap-northeast-2"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"ap-south-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"ap-southeast-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"ap-southeast-2"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"ca-central-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"cn-north-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"cn-northwest-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"eu-central-1"]}]},{"Fn::Or":[{"Fn::Equals":[{"Ref":"AWS::Region"},"eu-north-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"eu-west-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"eu-west-2"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"eu-west-3"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"me-south-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"sa-east-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"us-east-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"us-east-2"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"us-west-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"us-west-2"]}]}]}

Resources
[+] AWS::SQS::Queue SampleQueue SampleQueue49AAAEFF 
[+] AWS::SQS::QueuePolicy SampleQueue/Policy SampleQueuePolicy6B63351F 
[+] AWS::SNS::Subscription SampleQueue/SampleStackSampleTopic95768FB9 SampleQueueSampleStackSampleTopic95768FB97B34B6F6 
[+] AWS::SNS::Topic SampleTopic SampleTopic5FE9B5DC 

まとめ

aws ssoの認証情報を使ってcdkを実行するために、aws2-wrapを使いました。
環境が複数あると、毎回のコピペ作業の手間も多くなるので活用していきたいです。

Aamzonで「AWS」の本を見てみる

楽天で「AWS」の本をみてみる!!

cdk
CDKの始め方 ワークショップではまらないためにこんにちは、ちゃりおです。 先日、社内向けに「CDKの始め方」をテーマに勉強会を行いました。 本記事では、内容をブログ向けにまとめまし...
実践AWS CDK
CDKの基礎を「実践 AWS CDK – TypeScript でインフラもアプリも!」で学ぼうこんにちは、ちゃりおです。 以前から気になっていた「実践 AWS CDK – TypeScript でインフラもアプリも!」を読みまし...
awsロゴ
AWS SSOでアカウントの切り替えを楽にする小技こんにちは、ちゃりおです。 複数AWSアカウントの管理にAWS SSOは便利です。 しかし、他のアカウントに切り替える際にセッションが...