0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WindowsのRancher Desktopと sam local start-apiでLambda+APIGatewayをローカルで動かしたメモ

0
Last updated at Posted at 2026-05-02

概要

WindowsにRancher DesktopとSAM CLIをインストールして、sam local start-apiを行ったところエラーとなった。
issueによると、WindowsのSAMとRancherDesktopの相性が悪くマウントに失敗していたもよう。
WSL上でSAMを実行することで解決できた。

この時点のソースコード

環境

  • OS: Windows 11 Pro 25H2
  • WSL: v2
  • WSL ディストリビューション: Ubuntu 24.04
  • Docker: Rancher Desktop 1.22.0
  • IaC: AWS CDK 2.1119.0
  • (Windows) SAM CLI 1.158.0
  • (WSL) SAM CLI 1.159.1

※ WSLにUbuntuをいれた後にRancherDesktopを入れ、UbuntuでもDockerが使えるように設定。

発生した事象

sam local start-apiを実行したところ、下記エラーが発生

Runtime.IMportModuleError: Cannnot find module 'index'
Require stack:
- /var/runtime/index.mjs

/var/runtime/index.mjsを実行したときに/var/task配下にindex.jsがないという状態

仮説

テンプレートファイルが作成されていない

sam local start-apiの前にcdk synthで作成されており、index.jsも出力されていることを確認

マウントに失敗

sam local start-api --warn-container EAGERで起動し確認。
/bin/shはlambaコンテナに入っていないのでコマンド直接いれている。

docker ps
docker exec -it <コンテナID> pwd #/var/task
docker exec -it <コンテナID> ls #表示されない → マウント失敗

対応

環境設定

Windows

wsl --version # バージョンを確認
wsl -l # ディストリビューションの規定値を確認。Ubuntuがない場合はインストール要
wsl --install -d Ubuntu
wsl --set-default Ubuntu
winget uninstall SUSE.RancherDesktop # Ubuntuを入れる前にRancherDesktopを入れていた場合は削除
winget install -e --id SUSE.RancherDesktop

RancherDesktopでUbuntuが使えるようにする。 Preference > WSL Integrationsから設定

WSL

公式

sugo apt install unzip # javaがWindowsではパスが長くて回答できないのでwsl上で解凍する
wget https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip
unzip awscli-exe-linux-x86_64.zip -d sam-installation # 時間がかかるのでコーヒーでも淹れてくる
sudo ./sam-installation/install
sam --version

WSL上でSAMを実行

package.json
{
  "name": "infra",
  "version": "0.1.0",
  "bin": {
    "infra": "bin/infra.js"
  },
  "scripts": {
    "build": "tsc",
    "watch": "tsc -w",
    "test": "jest",
    "cdk": "cdk",
    "synth": "cdk synth",
+    "prelocal-api": "cross-env LOCAL_API=true npm run synth",
+    "local-api": "wsl -d Ubuntu -- bash -lc 'SAM_CLI_TELEMETRY=0 AWS_SECRET_ACCESS_KEY=dummy AWS_ACCESS_KEY_ID=dummy sam local start-api --template cdk.out/InfraStack.template.json'"
  },
  "devDependencies": {
    "@types/jest": "^30",
    "@types/node": "^25.6.0",
    "aws-cdk": "2.1119.0",
    "cross-env": "^10.1.0",
    "esbuild": "^0.28.0",
    "jest": "^30",
    "ts-jest": "^29",
    "ts-node": "^10.9.2",
    "typescript": "~6.0.3"
  },
  "dependencies": {
    "aws-cdk-lib": "^2.251.0",
    "constructs": "^10.6.0"
  }
}

なお、wsl上で"local-api": "cross-env SAM_CLI_TELEMETRY=0 AWS_SECRET_ACCESS_KEY=dummy AWS_ACCESS_KEY_ID=dummy sam local start-api --template cdk.out/InfraStack.template.json"npm run local-apiで起動するとWindows側でSAMを起動したことになり、エラーとなる。

参考

AWS CDKで作成したAPIGateway+Lambda(Node18)をローカルで動かしてみたメモ
Bug: sam invoke start-api doesn't mount sam artifacts on /var/task on Windows with Rancher Desktop

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?