Created
April 26, 2020 00:43
-
-
Save mamemomonga/5fb800371ffd0be050ed54fe0f51a1b6 to your computer and use it in GitHub Desktop.
awscliスニペット集
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eu | |
# ECRにログイン | |
# Version2にはレポジトリパスが必要 | |
ecr_login() { | |
local awscli_version="$( aws --version | perl -nlE 'say $1 if (m#aws-cli/(\d)\.#)' )" | |
case "$awscli_version" in | |
"1" ) | |
aws ecr get-login --no-include-email --region ap-northeast-1 | sh | |
;; | |
"2" ) | |
aws ecr get-login-password --region ap-northeast-1 | \ | |
docker login --username AWS --password-stdin $DOCKER_REPOS | |
;; | |
* ) | |
echo "aws-cli not found." | |
exit 1 | |
;; | |
esac | |
} | |
# AWS_DEFAULT_PROFILEを使った2つのACCOUNTの切り替え | |
aws_account() { | |
if [ -z "${AWS_DEFAULT_PROFILE:-}" ]; then | |
echo "環境変数 AWS_DEFAULT_PROFILE が設定されてません" | |
echo "以下のように環境変数を設定してください" | |
echo | |
echo ' export AWS_DEFAULT_PROFILE=プロファイル名' | |
echo | |
exit 1 | |
fi | |
local accountid="$( aws sts get-caller-identity --query 'Account' --output text )" | |
case "$accountid" in | |
"012345678990" ) | |
echo -e "アカウントIDは dev(開発) 用です" | |
ENVTYPE=dev | |
;; | |
"012345678990" ) | |
echo -e "アカウントIDは prod(本番) 用です" | |
ENVTYPE=prod | |
;; | |
* ) | |
echo "合致していないか、エラーが発生しています。awscliの設定を見直して下さい。" | |
echo | |
exit 1 | |
;; | |
esac | |
} | |
aws_account | |
ecr_login | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment