1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

sam localで起動したLambdaにProfileを当てて起動する

Posted at

TL;DR

  • sam local start-lambda --profile [profile-name]
  • [profile-name]に割り当てたRoleを指定したprofileを指定して起動すると、lambdaにもprofileを渡してくれる
  • AWS_PROFILE=[profile-name] sam local start-lambdaでもいい
  • profileはAWS CLI (.aws/config, credentials) で設定するものと同一
  • 普通にコマンドラインで起動時にprofileを指定しているのであれば、samの時も同様に指定してあげること

背景

  • SAM CLIで作成したアプリ内で、AWS SDKを使ってAWSから情報を取得なりして応答するAPIを作成していた
  • 一旦デバッグ目的で直接起動して動作確認はできたけど、sam local start-api経由だとRoleが渡ってなかったやんどう渡すやんこれ...

 やってた内容

コード

import boto3
import json


def lambda_handler(event, context):
    result = main()

    return result

def main():
    cli = boto3.client('ec2')
    result = cli.describe_instances()

    return result


if __name__ == '__main__':
    main()

みたいなコードで情報を取得して、API GW経由で結果を返すAPIを作成しようとしていた

そのまま実行

AWS_PROFILE=kono-profile python kono_code.py

直接実行時はProfileを指定してた

SAM CLI経由で実行

sam local start-api

何も気にせずこれで実行すると、default profileに目的のものが設定されていなければInvalidClientTokenIdとかで怒られる

直接実行時と同じようにprofileを指定してあげると、コンテナ内のlambdaにもprofileの情報を渡してくれるよう

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?