LoginSignup
40
19

More than 3 years have passed since last update.

Serverless Framework v1 で --profileオプション / --regionオプションを作って AWS CLI credentialsを活用する

Last updated at Posted at 2016-10-06

バージョン

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.11.6
BuildVersion:   15G1004
$
$ $(npm bin)/sls -v
1.0.0-rc.2
$

何がしたかったか

serverless.yml に profile を直接書きたくなかった。


service: new-service
provider:
  name: aws
  runtime: nodejs4.3
  stage: ${opt:stage, self:custom.defaultStage}
  profile: ${self:custom.profiles.${self:provider.stage}}
custom:
  defaultStage: dev
  profiles:
    #ここ
    dev: devProfile
    #ここ
    prod: prodProfile

みたいにprofile名を直接書いちゃうとgit repoにのせずらいなーと思った。

なおかつ、slsコマンドが AWS_DEFAULT_PROFILE 環境変数を見ない感じだった。

こうした

service: test-sls-v1
provider:
  name: aws
  runtime: python2.7
  stage: ${opt:stage, self:custom.defaultStage}
  #profileオプション追加
  profile: ${opt:profile, self:custom.defaultProfile}
  #regionオプション追加
  region: ${opt:region, self:custom.defaultRegion}
custom:
  defaultStage: dev
  #何も指定がなければ default profile
  defaultProfile: default
  #何も指定がなければ 東京リージョン
  defaultRegion: ap-northeast-1
functions:
  hello:
    handler: handler.hello

こんな感じでprofileオプションを指定できるので AWS CLIぽく使える。

$ $(npm bin)/sls deploy --profile hogehoge-user

参考:

serverless/credentials.md at master · serverless/serverless

https://github.com/serverless/serverless/blob/master/docs/providers/aws/guide/credentials.md

40
19
2

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
40
19