0
0

More than 1 year has passed since last update.

Lambda関数をローカル環境からワンタッチでデプロイする

Last updated at Posted at 2022-07-24

ローカルPythonコードからboto3を経由してLambda関数を更新します。
作成済のLambda関数のロジック修正が簡単に実施できるので便利だと思います。

コード説明

  • デプロイ対象のLambda関数はPythonを前提とします。
  • デプロイ対象のLambda関数は作成済であることを前提とします。
  • このPythonファイルを置き換えたいLambda関数と同一フォルダへ配置します。
  • Pythonファイルが置かれたフォルダ名がLambda関数名と一致することを前提とします。
  • Lambda関数の中身を更新します。

upload_lambda.py

import io
import os
from zipfile import ZipFile
import boto3
import pathlib

def main(funcName):
    print("main start")
    file_name = "lambda_function.py"
    buf = io.BytesIO()
    with ZipFile(buf, 'w') as z:
        z.write(pathlib.Path(__file__).parent.joinpath(file_name), file_name)
    client = boto3.client('lambda', region_name="ap-northeast-1")
    response = client.update_function_code(
        FunctionName=funcName,
        ZipFile=buf.getvalue()
    )
    print("main end")

if __name__ == "__main__":
    print("start")
    funcName = pathlib.Path(__file__).parent.name
    main(funcName)
    print("end")
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