LoginSignup
1
0

More than 1 year has passed since last update.

素の docker と ecr 公式イメージで aws lambda をlocal で動かす

Last updated at Posted at 2022-11-07

Overview

sam local とか serverless とかまどろっこしいんじゃ!というせっかちな方向けに、
最短距離で aws lambda を local で動かす方法をお届け。

注意

本物の aws lambda 環境だと LAMBDA_TASK_ROOT の上書きができない。
必ず $[LAMBDA_TASK_ROOT}/ 下にコード展開する必要があるので注意。

ソース

お好きな公式イメージ 使ってください。
サンプルでは特に意図はないけど node18 でいきます。

docker-compose.yaml
version: '3.9'

services:
  node:
    image: public.ecr.aws/lambda/nodejs:18.2022.11.03.18
    command:
      - app.handler
    ports:
      - 9000:8080
    environment:
      LAMBDA_TASK_ROOT: /mnt/
    volumes:
      - ${PWD}:/mnt/
app.mjs
export async function handler(event, context) {
    console.log(JSON.stringify(event))
    console.log(JSON.stringify(context))
    console.log(123)

    return "ok";
}

起動

bash
$ docker-compose up

実行

別のターミナルから

bash
$ curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"payload":"hello world!"}'

とかやると、 docker-compose up したターミナルに結果が出るはず。
あとは公式イメージをベースに Dockerfile 書いてくなり、 docker-compose.yaml で肉付けしてくなりすれば良いと思います。

cf.

まぁ細かい話はこのへん見てください。
https://gallery.ecr.aws/lambda
https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/configuration-envvars.html

lambda の ric とか rie とかの詳しい仕組みはこの辺が詳しく説明してくれてるのでおすすめ。
https://goropikari.hatenablog.com/entry/aws_lambda_ric_rie

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