Serverless Frameworkで複数AWSアカウントを切り替えられるようにする

serverless.com

AWSのアクセスキーを登録すると~/.aws/credentialsに情報が登録される。登録方法は以下を参照。

https://serverless.com/framework/docs/providers/aws/guide/credentials#setup-with-serverless-config-credentials-command

通常だと上記リンク先のコマンドで登録した一つのアクセスキーのみ管理することになるが、複数のAWSアカウントに対するアカウントを管理したい場合は以下のように~/.aws/credentialsを書き換える。

https://serverless.com/framework/docs/providers/aws/guide/credentials#use-an-existing-aws-profile

[default]
aws_access_key_id=***************
aws_secret_access_key=***************

[production]
aws_access_key_id=***************
aws_secret_access_key=***************

デプロイ時にproductionのアクセスキーを使用したい場合は以下のように--aws-profileオプションを設定する。

https://serverless.com/framework/docs/providers/aws/guide/credentials#using-the-aws-profile-option

$ serverless deploy --aws-profile production

--aws-profileオプションを指定しなければdefaultのアクセスキーが使用される。