@akym03 さんがポストされてた記事がとても役立ったのですが、404になってしまっているので自分の備忘録として残させて頂きます。
やりたいこと
WLS内にインストールしたAWSのSAM(Serverless Application Model)のCLIから、Docker for Windowsのデーモンにアクセスして、ローカルのLambdaにアクセスしたい
なぜできないの?
現状WLS内のDockerはホストにはなれず、あくまでもクライアントのため、WSLのDockerにアクセスしても意味がないのです。
詳しくはこちらの図がとても参考になりました。(画像リンクしてます)
WSL(Bash on Windows)でDockerを使用する - Qiita
環境
- Windows 10 Pro
- バージョン 1809
- OSビルド 17763.316
- DISTRIB_DESCRIPTION="Ubuntu 18.04.2 LTS"
Dockerの設定
Settings > General にある Expose daemon on tcp://localhost:2375 without TLS
にチェックを入れる
WSLの設定
WSLからDocker for Windowsに接続するために、WSLで環境変数を設定する
$ export DOCKER_HOST=tcp://localhost:2375
SAM local実行
sam init で作成されたプロジェクトを動作させてみます。
$ sam local invoke HelloWorldFunction --event event_file.json
2019-07-17 10:47:53 Invoking app.lambdaHandler (nodejs10.x)
2019-07-17 10:47:53 Found credentials in shared credentials file: ~/.aws/credentials
Fetching lambci/lambda:nodejs10.x Docker container image......
2019-07-17 10:47:55 Mounting /c/opt/box/box-reinforcement-lambda/hello-world as /var/task:ro,delegated inside runtime container
START RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72 Version: $LATEST
END RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72
REPORT RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72 Duration: 9.58 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 43 MB
{"statusCode":200,"body":"{\"message\":\"hello world\"}"}
ローカルでAPI起動
以下に沿ってStep 2: Test the Application Locally
をやってみます
Quick Start - AWS Serverless Application Model
$ sam local start-api
2019-07-17 11:15:54 Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
2019-07-17 11:15:54 You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template
2019-07-17 11:15:54 * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
2019-07-17 11:16:00 127.0.0.1 - - [17/Jul/2019 11:16:00] "GET / HTTP/1.1" 403 -
2019-07-17 11:16:00 127.0.0.1 - - [17/Jul/2019 11:16:00] "GET /favicon.ico HTTP/1.1" 403 -
2019-07-17 11:16:11 Invoking app.lambdaHandler (nodejs10.x)
2019-07-17 11:16:11 Found credentials in shared credentials file: ~/.aws/credentials
Fetching lambci/lambda:nodejs10.x Docker container image......
2019-07-17 11:16:13 Mounting /c/opt/box/box-reinforcement-lambda/hello-world as /var/task:ro,delegated inside runtime container
START RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72 Version: $LATEST
END RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72
REPORT RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72 Duration: 7.61 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 43 MB
2019-07-17 11:16:14 No Content-Type given. Defaulting to 'application/json'.
2019-07-17 11:16:14 127.0.0.1 - - [17/Jul/2019 11:16:14] "GET /hello HTTP/1.1" 200 -
ブラウザからhttp://127.0.0.1:3000/hello
にアクセスします
200でjson返ってきました。
{"message":"hello world"}