
1Password CLIのShell PluginsでAWS CLIやAWS CDKの操作をするまでのステップを整理してみた
AWS CLIやAWS CDKで操作する際のMFAのコード入力を自動化したい
こんにちは、のんピ(@non____97)です。
皆さんはAWS CLIやAWS CDKで操作する際のMFAのコード入力を自動化したいなと思ったことはありますか? 私はあります。
AWS CLIはMFA入力後、キャッシュされた認証情報を用いてくれるため、MFAのコード再入力の頻度はそこまで多くありません。しかし、AWS CDKについてはcdk deploy
はもちろん、cdk synth
やcdk diff
を叩く際にも都度MFA入力が求められます。
そんな時に便利なのは1Password CLIです。
以下記事で紹介されているとおり、MFAを1Passwordのアプリからコピペすることなく、Touch IDで認証情報を渡すことができます。
ローカルにアクセスキー、シークレットアクセスキーを保存しなくても良いのも嬉しいポイントです。
ただし、以下ドキュメントに従って設定するにあたって、いくつか詰まった箇所がありました。
セットアップの過程を以降紹介します。
環境
現在の環境は以下のとおりです。
- OS : macOS 15.5
- ターミナル : iTerm2 Build 3.5.14
- シェル : fish
~/.aws/credentials
と~/.aws/config
は以下のとおりです。
[default]
aws_access_key_id = <アクセスキー>
aws_secret_access_key = <シークレットアクセスキー>
[default]
region = us-east-1
output = json
cli_pager=
[profile <プロファイル名>]
region = us-east-1
mfa_serial = arn:aws:iam::<IAMユーザーのAWSアカウントID>:mfa/<MFAデバイス名>
role_arn = arn:aws:iam::<Assum Role先AWSアカウントID>:role/<IAMロール名>
cli_pager=
role_session_name = <セッション名>
duration_seconds = 3600
source_profile = default
実際の操作はIAMロールからAssume Roleをしています。いわゆるJumpアカウント構成です。
Jumpアカウントについては以下記事をご覧ください。
なお、1Password CLIを使えば~/.aws/credentials
は不要なのですが、1Password CLIを使えない/使うことが向いていない場面に備えて残しています。
~/.aws/credentials
と~/.aws/config
の書き方については以下AWS公式ブログおよび、ブログ記事をご覧ください。
1Passowrd CLIのインストール
では、まず1Passowrd CLIのインストールからです。
以下1Passowrd公式ドキュメントに従って行います。
私はMacを使用しているので、Homebrewでインストールをします。
> brew install 1password-cli
==> Auto-updating Homebrew...
Adjust how often this is run with HOMEBREW_AUTO_UPDATE_SECS or disable with
HOMEBREW_NO_AUTO_UPDATE. Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> New Casks
aigcpanel insta360-link-controller
You have 7 outdated formulae installed.
==> Migrating cask wireshark to wireshark-app
==> Downloading https://cache.agilebits.com/dist/1P/op2/pkg/v2.31.1/op_darwin_arm64_v2.31.1.zip
Already downloaded: /Users/<ユーザー名>/Library/Caches/Homebrew/downloads/99746c0f8dfb89dd29cab6459ce0147d2b656793591dba11c8eb2b1595fe7de5--op_darwin_arm64_v2.31.1.zip
==> Installing Cask 1password-cli
==> Linking Binary 'op' to '/opt/homebrew/bin/op'
🍺 1password-cli was successfully installed!
> op --version
2.31.1
インストールが完了しました。
1Password CLIと1Passwordアプリとの連携
1Password CLIと1Passwordアプリとの連携を行います。
以下のように、1Passowrdアプリの設定の開発者
から1Passowrd CLIと連携
を有効化します。
また、Touch IDによる認証情報の入力も行いたいため、以下のように1Passwordのロック解除でTouch IDを使えるようにします。
この状態で1Passowrd Vaultのリストを1Passoword CLIで確認します。
> op vault list
[ERROR] 2025/06/24 22:09:24 You are not currently signed in. Please run `op signin --help` for instructions
まだサインインしていないと怒られてしまいました。
指示に従いサインインしてみます。
> op signin
[ERROR] 2025/06/24 22:11:20 Output of 'op signin' is meant to be executed by your terminal. Please run 'eval $(op signin)'. You can use the '-f' flag to override this warning.
これもダメです。
eval $(op signin)
を叩きます。
> eval $(op signin)
No accounts configured for use with 1Password CLI.
You can either:
- Turn on the 1Password desktop app integration to sign in with the accounts you've added to the app: https://developer.1password.com/docs/cli/app-integration/ for details.
- Add an account manually with 'op account add' and sign in by entering your password on the command line. See 'op account add --help' for details.
- Authenticate using a 1Password service account by setting the 'OP_SERVICE_ACCOUNT_TOKEN' environment variable to your service account token. Learn more: https://developer.1password.com/docs/service-accounts/
- Use 1Password CLI with a Connect server by setting the 'OP_CONNECT_HOST' and 'OP_CONNECT_TOKEN' environment variables to your Connect host and token, respectively. Learn more: https://developer.1password.com/docs/connect/
Do you want to add an account manually now? [Y/n] Y
Enter your sign-in address (example.1password.com): <1Passwordドメイン名>.1password.com
Enter the email address for your account on <1Passwordドメイン名>.1password.com: <ユーザー名>@<1Passwordドメイン名>.jp
Enter the Secret Key for <ユーザー名>@<1Passwordドメイン名>.jp on <1Passwordドメイン名>.1password.com: <シークレットキー>
Enter the password for <ユーザー名>@<1Passwordドメイン名>.jp at <1Passwordドメイン名>.1password.com:
Enter your six-digit authentication code: 105091
> op account list
SHORTHAND URL EMAIL USER ID
<組織名> https://<組織名>.1password.com <ユーザー名>@<ドメイン> <ユーザーID>
はい、各種1Passowrdの認証情報を渡すことでログインできたようです。
改めて1Password Vaultのリストを確認します。
> op vault list
ID NAME
<ボールトID> Employee
確認できましたね。
別のターミナルウィンドウを開いて、別セッションの場合はどうでしょう。
> op vault list
[ERROR] 2025/06/24 22:19:01 You are not currently signed in. Please run `op signin --help` for instructions
再度認証を要求されました。
認証をします。
> eval $(op signin)
Enter the password for <ユーザー名>@<ドメイン> at <組織名>.1password.com:
> op vault list
ID NAME
<ボールトID> Employee
初回の認証と異なり、パスワードを渡すだけで認証できました。
セッションごと都度認証を行いましょう。
Shell Pluginの設定
AWS CLI
それでは、Shell Pluginを用いてAWSの認証情報を受け渡しできるようにします。
ドキュメントは以下です。
AWS CLIのShell Pluginをインストールします。
> op plugin init aws
AWS CLI
Authenticate with AWS Access Key.
? Locate your AWS Access Key: [Use arrows to move, type to filter, ? for more help]
> AWS - Classmethod (Employee) - <IAMユーザー名>
Import into 1Password...
Search in 1Password...
アクセスキーが保存されているアイテムを選択します。
AWS - Classmethod (Employee)
というアイテムに保存しているため、こちらを選択します。
まだアクセスキーの保存をしていない場合は、Import into 1Password...
を選択し、指示に基づいて設定しましょう。
なお、以下情報が必須となります。
- Access Key ID
- Secret Access Key
また、私はTOTPのMFAを使用しているため、以下も設定しています。
- one-time passowrd
- mfa serial
具体的には以下のとおりです。
認証情報を保存したアイテムを選択したら、認証情報を使用するタイミングを設定します。
? Configure when the chosen credential(s) will be used to authenticate: [Use arrows to move, type to filter, ? for more help]
> Prompt me for each new terminal session
Use automatically when in this directory or subdirectories
Use as global default on my system
システムのデフォルト設定や、ディレクトリ設定とすることで自動で認証情報を使用してくれそうですが、思わぬ事故が発生しそうで怖いです。
新しいターミナルセッションごとに確認をするPrompt me for each new terminal session
を選択します。
? Configure when the chosen credential(s) will be used to authenticate: Prompt me for each new terminal session
The last step is to set up an alias for aws.
You can do so by running the following command:
echo "source /<ホームディレクトリ>/.config/op/plugins.sh" >> ~/.config/fish/config.fish && source ~/.config/fish/config.fish
Afterwards, run any aws command to see it in action!
設定が完了しました。
echo "source /<ホームディレクトリ>/.config/op/plugins.sh" >> ~/.config/fish/config.fish && source ~/.config/fish/config.fish
を叩けと言われていますね。
/<ホームディレクトリ>/.config/op/plugins.sh
の中身は以下のとおりです。
> cat /<ホームディレクトリ>/.config/op/plugins.sh
export OP_PLUGIN_ALIASES_SOURCED=1
alias aws="op plugin run -- aws"
直接AWS CLIを叩くのではなく、1Passowrd CLIのShell Plugin経由で叩くようになっていることが分かります。
それでは、指示されたとおりのコマンドを叩きます。
> echo "source /<ホームディレクトリ>/.config/op/plugins.sh" >> ~/.config/fish/config.fish && source ~/.config/fish/config.fish
この状態で認証をしてみます。
> aws sts get-caller-identity
[ERROR] 2025/06/24 22:31:22 Shell Plugins can only be used with the 1Password app integration enabled.
To learn more about this feature, check out:
https://developer.1password.com/docs/cli/about-biometric-unlock/
はい、1Passowrdアプリと連携ができている場合にのみ使用できると言われてしまいました。
事前に設定は完了したはずですが謎です。
Use the 1Password desktop app to sign in to 1Password CLIというドキュメントを見ると、OP_BIOMETRIC_UNLOCK_ENABLED
で生体認証(今回の場合はTouch ID)の使用有無を切り替えられそうです。
試しにこちらを設定して再チャレンジします。
> vi /<ホームディレクトリ>/.config/op/plugins.sh
> cat /<ホームディレクトリ>/.config/op/plugins.sh
export OP_PLUGIN_ALIASES_SOURCED=1
export OP_BIOMETRIC_UNLOCK_ENABLED=true
alias aws="op plugin run -- aws"
> source ~/.config/fish/config.fish
> aws sts get-caller-identity
このタイミングで1Passowrdアクセスリクエストのウィンドウが表示されました。
Touch IDで認証をします。
> aws sts get-caller-identity
? Locate your AWS Access Key: AWS - Classmethod (Employee) - <IAMユーザー名>
[ERROR] 2025/06/24 22:34:29 could not run plugin AWS CLI: failed to provision credentials, encountered error(s):
operation error STS: AssumeRole, failed to sign request: failed to retrieve credentials: operation error STS: GetSessionToken, https response error StatusCode: 403, RequestID: dd7a19b2-0d50-49f3-93ae-fbe80ec9d617, api error AccessDenied: Cannot call GetSessionToken with session credentials
> op plugin run --debug -- aws sts get-caller-identity
10:39PM | DEBUG | Skipped loading desktop app settings file. The desktop app might not be installed: read file: open /<ホームディレクトリ>/Library/Group Containers/2BUA8C4S2C.com.1password/Library/Application Support/1Password/Data/settings/settings.json: operation not permitted
10:39PM | DEBUG | Session delegation enabled
10:39PM | DEBUG | NM request: NmRequestAccounts
10:39PM | DEBUG | NM response: Success
10:39PM | DEBUG | NM request: NmRequestAccounts
10:39PM | DEBUG | NM response: Success
10:39PM | DEBUG | InitDefaultCache: successfully initialized cache
10:39PM | DEBUG | EncryptedKeysets: Cache hit on keyset
10:39PM | DEBUG | Vault: cache hit on vault <1Password Vault ID>
10:39PM | DEBUG | VaultItems: cache hit on vault items of vault <1Password Vault ID>
10:39PM | DEBUG | Item: VaultItems cache hit for vault <1Password Vault ID> - validating staleness using item version
10:39PM | DEBUG | Item: cache hit on item 6ax4eeb6arkx7qod6legnyq4he of vault <1Password Vault ID>
[ERROR] 2025/06/24 22:39:53 could not run plugin AWS CLI: failed to provision credentials, encountered error(s):
operation error STS: AssumeRole, failed to sign request: failed to retrieve credentials: operation error STS: GetSessionToken, https response error StatusCode: 403, RequestID: 35c0bf7b-0736-46d3-b861-54b87ccf0a1e, api error AccessDenied: Cannot call GetSessionToken with session credentials
Touch IDは正常にできたようですが、今度は違うエラーが出るようになってしまいました。
1Password Commyunityサイトを確認すると、これは既知の事象のようです。特に回避方法の言及はありませんでした。
failed to retrieve credentials: operation error STS: GetSessionToken,
やCannot call GetSessionToken with session credentials
とあることから、AssumeRoleする過程で、一時認証情報でIAMユーザーの一時的な認証情報を取得するGetSessionToken
をしようとしてエラーになっていそうです。
GetSessionToken
については以下記事をご覧ください。
無理にGetSessionToken
を使おうとしているのが気になります。
試しにアクセスキーやシークレットアクセスキーを保存しているアイテムからmfa serial
を削除します。
この状態でaws sts get-caller-identity
を叩きます。
> aws sts get-caller-identity
{
"UserId": "<ユーザーID>:<IAMユーザー名>",
"Account": "<Assum Role先AWSアカウントID>",
"Arn": "arn:aws:sts::<Assum Role先AWSアカウントID>:assumed-role/<IAMロール名>/<セッション名>"
}
> aws sts get-caller-identity
{
"UserId": "<ユーザーID>:<IAMユーザー名>",
"Account": "<Assum Role先AWSアカウントID>",
"Arn": "arn:aws:sts::<Assum Role先AWSアカウントID>:assumed-role/<IAMロール名>/<セッション名>"
}
> aws sts get-caller-identity --profile default
{
"UserId": "AIDAXA25AMYQMQFPPZHFQ",
"Account": "<IAMユーザーのAWSアカウントID>",
"Arn": "arn:aws:iam::<IAMユーザーのAWSアカウントID>:user/<IAMユーザー名>"
}
はい、正常にできました。defaultプロファイルも問題なく完了しています。
1Passowrd公式ドキュメントではYour edited item must include the one-time password and mfa serial fields:
と記載ありましたが、mfa serial
は不要なようです。
抜粋 : Use 1Password to authenticate the AWS CLI with biometrics | 1Password Developer
他コマンドが叩けられることも確認します。
> aws s3 ls
2022-04-26 13:45:06 amazon-macie-test-20220426
2024-03-29 19:07:40 amplify-djzcih29vo8av-mai-amplifydataamplifycodege-xlzfedi9oye4
2025-01-06 16:03:18 aurora-postgresql-log
.
.
(以下略)
.
.
正常に完了しました。MFAの入力も要求されていません。
別セッションでも問題ありませんでした。
> aws sts get-caller-identity
? Locate your AWS Access Key: AWS - Classmethod (Employee) - <IAMユーザー名>
{
"UserId": "<ユーザーID>:<IAMユーザー名>",
"Account": "<Assum Role先AWSアカウントID>",
"Arn": "arn:aws:sts::<Assum Role先AWSアカウントID>:assumed-role/<IAMロール名>/<セッション名>"
}
なお、別セッションなので再度Touch IDによる認証を求められました。
AWS CDK
続いて、AWS CDKです。
ドキュメントは以下です。
ステップはAWS CLIの場合と同じです。
> op plugin init cdk
AWS CDK Toolkit
Authenticate with AWS Access Key.
? Locate your AWS Access Key: AWS - Classmethod (Employee) - <IAMユーザー名>
? Configure when the chosen credential(s) will be used to authenticate: Prompt me for each new terminal session
The last step is to set up an alias for cdk.
You can do so by running the following command:
source /<ホームディレクトリ>/.config/op/plugins.sh
Afterwards, run any cdk command to see it in action!
/<ホームディレクトリ>/.config/op/plugins.sh
を確認します。
cat /<ホームディレクトリ>/.config/op/plugins.sh
export OP_PLUGIN_ALIASES_SOURCED=1
export OP_BIOMETRIC_UNLOCK_ENABLED=true
alias aws="op plugin run -- aws"
alias cdk="op plugin run -- cdk"
alias cdk="op plugin run -- cdk"
が一行追加されていますね。
source
で読み込んだのち、cdk list
を叩きます。
> source /<ホームディレクトリ>/.config/op/plugins.sh
> cdk list
DriftTestStack
はい、MFAのコード入力を求められずに実行できました。これは便利です。
なお、npx cdk list
のようにローカルにインストールされたAWS CDK CLIを使おうとすると、MFAのコード入力が求められます。
npx cdk list
MFA token for arn:aws:iam::<IAMユーザーのAWSアカウントID>:mfa/<MFAデバイス名> 189628
DriftTestStack
あくまでShell PluginはグローバルインストールしたAWS CDK CLIで効果を発揮します。
Shell Pluginの一時無効化
Shell Pluginを一時無効化したい場合があります。
そんな時は簡単です。以下のようにalias aws=aws
とするだけです。
> alias aws=aws
> aws sts get-caller-identity
Enter MFA code for arn:aws:iam::<IAMユーザーのAWSアカウントID>:mfa/<MFAデバイス名>:
{
"UserId": "<ユーザーID>:<IAMユーザー名>",
"Account": "<Assum Role先AWSアカウントID>",
"Arn": "arn:aws:sts::<Assum Role先AWSアカウントID>:assumed-role/<IAMロール名>/<セッション名>"
}
Shell Plguinを再度有効化したい場合は~/.config/op/plugins.sh
を読み込むだけです。
> source /<ホームディレクトリ>/.config/op/plugins.sh
> aws sts get-caller-identity
? Locate your AWS Access Key: AWS - Classmethod (Employee) - <IAMユーザー名>
{
"UserId": "<ユーザーID>:<IAMユーザー名>",
"Account": "<Assum Role先AWSアカウントID>",
"Arn": "arn:aws:sts::<Assum Role先AWSアカウントID>:assumed-role/<IAMロール名>/<セッション名>"
}
しばらく1Password CLIにお世話になりそう
1Password CLIのShell PluginsでAWS CLIやAWS CDKの操作をするための方法を整理してみました。
MFAのコード再入力の手間だけでなく、ローカルにアクセスキー、シークレットアクセスキーを保持しなくても良いのは非常にありがたいです。
私はしばらく1Password CLIにお世話になりそうです。
この記事が誰かの助けになれば幸いです。
以上、クラウド事業本部 コンサルティング部の のんピ(@non____97)でした!