LoginSignup
11
6

More than 3 years have passed since last update.

AWS Lambda で git コマンドを使う

Last updated at Posted at 2020-04-06

AWS Lambdaでgitコマンドを使う

AWS Lambdaでgitコマンドを使いたい場合があります。
githubのAPIを呼んだり、dulwichを使ってもいいのですが、新しく勉強するよりは、できれば、いつもの使い慣れたgitコマンドを使いたいものです。
AWS Lambda で実行できるコマンドを作成する環境を作ってみた

使う物

AWS Lambda用にコマンドとライブラリをいい感じにまとめてくれるDockerコンテナ
https://hub.docker.com/r/qualitiaco/lambda-build-pack

*このDockerコンテナの作り方については、前回の記事をご参考ください。
AWS Lambda で実行できるコマンドを作成する環境を作ってみた
https://qiita.com/qualitia_cdev/items/4aebb1e22be9afab0a16

gitコマンドとライブラリを抽出

gitコマンドを取り出すスクリプト

src/build.sh
#!/bin/sh
yum install -y git
cp -a /usr/bin/git ${OUTPUT_PATH}
cp -a /usr/libexec/git-core/git-remote-https ${OUTPUT_PATH}
cp -a /usr/libexec/git-core/git-remote-http ${OUTPUT_PATH}

yum でgitをインストールし(もしかしたら既に入っているかもしれませんが)、gitコマンド実行に必要な/usr/bin/gitとhttps通信に必要な、/usr/lib/exec/git-code/git-remote-http(s) を${OUTPUT_PATH}ディレクトリにコピーします。

この後のdockerコマンドが、${OUTPUT_PATH}にあるコマンドに必要なライブラリを自動的に抽出してくれます。

今回はyumで入れましたが、最新がよければ、gitコマンドをbuild.shスクリプト内でコンパイルしても構いません。

AWS Lambda環境用のgitコマンドとライブラリの取り出し

docker run -it --rm -v $(pwd)/src:/src -v $(pwd)/output:/output qualitiaco/lambda-build-pack

AWS Lambdaにアップロードする

Lambda Functionの作成

今回はPythonで作成してみます。
さっきできたoutputディレクトリの中に作成します。

output/lambda_function.py
import subprocess
import os
def lambda_handler(event, context):
    cwd = os.getcwd()
    os.chdir("/tmp")
    subprocess.call([
        os.path.join(cwd, "git"),
        f"--exec-path={cwd}",
        "clone",
        "https://github.com/qualitiaco/action-lambda-build-pack-sample.git"])
    print(open("/tmp/action-lambda-build-pack-sample/src/build.sh", "r").read())

githubのhttps://github.com/qualitiaco/action-lambda-build-pack-sample.git からgit cloneして、その中にあるsrc/build.shを出力するだけのプログラムです。

zipする

outputディレクトリの中から圧縮します。
シンボリックリンクを含みますので、「-y」オプションを忘れないよう付けてください。

cd output
zip -9yr ../lambda_function.zip *

確認

AWS Lambda Functionをあらかじめ作成してzipファイルをアップロードします。
少しサイズが大きいので、関数のソースコードをWebで確認することはできません。

実行すると、

lambda1.png

githubからgit cloneして取得したソースコードが表示されました。

終わりに

Dockerコンテナqualitiaco/lambda-build-packを使用してAWS Lambdaに必要なgitコマンドとライブラリを取得し、AWS Lambdaでgitコマンドを実行してみました。

本記事で使用したソースコードは、
https://github.com/qualitiaco/action-lambda-build-pack
にも置いてありますので、併せてご覧ください。

*本記事は @qualitia_cdevの中の一人、@hirachanさんに書いていただきました。

11
6
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
11
6