LoginSignup
0
0

More than 3 years have passed since last update.

aws-vault の登録内容から AWS CLI 設定ファイルを作成する

Last updated at Posted at 2020-02-27

AWS CLI v2とaws-vaultとpecoを使ってプロファイルを選択方式にする で書いた、aws-vault の登録内容から AWS CLI 設定ファイル (~/.aws/config) を作成する shell を作ってみました。

aws_config_maker.sh
#!/bin/sh

# Variables
readonly TARGET_HOME=$HOME/.aws
readonly TARGET_FILE=$TARGET_HOME/config
readonly REGION=ap-northeast-1
readonly OUTPUT=json

# Initialize .aws/config
if [ ! -e "$TARGET_HOME" ]; then
  echo Create a config form folder.
  mkdir -p "$TARGET_HOME"
fi

# Back up .aws/config
if [ -e "$TARGET_FILE" ]; then
  echo Back up config.
  cp -p "$TARGET_FILE" "$TARGET_FILE".`date "+%Y%m%d%H%M%S"`
fi

# Create default profile
echo Register default settings.
cat <<EOF > "$TARGET_FILE"
[default]
credential_process=/PATH/TO/credential-selector.sh
region=$REGION
output=$OUTPUT
EOF

# Added settings for each credential
for credential in ` aws-vault ls | awk 'NR>2 {if ($2 != "-") print $2}'`
do
  echo Register profile [$credential].
  cat <<EOF >> "$TARGET_FILE"

[profile $credential]
credential_process=aws-vault exec -j $credential --no-session
region=$REGION
output=$OUTPUT
EOF

done

これを実行すると

$ sh ./aws_config_maker.sh
Back up config.
Register default settings.
Register profile [prof1].
Register profile [prof2].
Register profile [prof3].
~/.aws/config
[default]
credential_process=/PATH/TO/credential-selector.sh
region=ap-northeast-1
output=json

[profile prof1]
credential_process=aws-vault exec -j prof1 --no-session
region=ap-northeast-1
output=json

[profile prof2]
credential_process=aws-vault exec -j prof2 --no-session
region=ap-northeast-1
output=json

[profile prof3]
credential_process=aws-vault exec -j prof3 --no-session
region=ap-northeast-1
output=json

って感じで既にファイルがあればバックアップして aws-vault の登録内容から config を生成します。
~/.aws/credentials の方はなんにもしません。

作ってみたものの、かなりの数のクレデンシャルを登録しないと shell の効果を感じることは無さそうな。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0