ローカルでAWS-Lambdaのコード検証を行えるべく環境構築をしております。
恐らくですが、node_modulesがdockerにマウントできていないのではないかと思っているのですが、invalid ELF headerとなり関数が実行できない状況です。
xml2jsonが原因ということまでわかったのですが、どのようにすればxml2jsonを利用できるかが分からず困っております。
ご教示頂けましたら幸いです。
#やったこと
//samのバージョン $ sam --version SAM CLI, version 0.16.1 //サンプルコードを取得 $ sam init -r nodejs8.10
/sam-app/hello-worldのapp.jsを下記に変更
const parser = require('xml2json'); const fs = require('fs'); exports.lambdaHandler = async (event, context) => { var test = {"test":"test"}; return test; };
app.js内ではまだ使っていないですが、xml2json等を/sam-app/hello-world/node_modulesへ
$ npm install xml2json --prefix "/path/to/sam-app/hello-world" $ npm install fs --prefix "/path/to/sam-app/hello-world"
app.jsの実行
// sam-app内へ移動 $ cd sam-app $ sam local start-api 2019-05-31 14:29:27 Found credentials in shared credentials file: ~/.aws/credentials 2019-05-31 14:29:27 Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET] 2019-05-31 14:29:27 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-05-31 14:29:27 * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
ブラウザで127.0.0.1:3000/helloにアクセス
2019-05-31 14:31:54 Invoking app.lambdaHandler (nodejs8.10) Fetching lambci/lambda:nodejs8.10 Docker container image...... 2019-05-31 14:31:56 Mounting /Users/tsujita/estockweb/docker-lambda/sam-app/hello-world as /var/task:ro,delegated inside runtime container START RequestId: 2b410a4c-6939-16b6-2fad-2969877db06a Version: $LATEST module initialization error: Error at Object.Module._extensions..node (module.js:681:18) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Module.require (module.js:596:17) at require (internal/module.js:11:18) at bindings (/var/task/node_modules/bindings/bindings.js:112:48) at Object.<anonymous> (/var/task/node_modules/node-expat/lib/node-expat.js:4:32) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) END RequestId: 2b410a4c-6939-16b6-2fad-2969877db06a REPORT RequestId: 2b410a4c-6939-16b6-2fad-2969877db06a Duration: 140.64 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 31 MB 2019-05-31 14:31:59 Invalid API Gateway Response Keys: {'errorMessage', 'errorType', 'stackTrace'} in {'errorMessage': '/var/task/node_modules/node-expat/build/Release/node_expat.node: invalid ELF header', 'errorType': 'Error', 'stackTrace': ['Module.load (module.js:565:32)', 'tryModuleLoad (module.js:505:12)', 'Function.Module._load (module.js:497:3)', 'Module.require (module.js:596:17)', 'require (internal/module.js:11:18)', 'bindings (/var/task/node_modules/bindings/bindings.js:112:48)', 'Object.<anonymous> (/var/task/node_modules/node-expat/lib/node-expat.js:4:32)', 'Module._compile (module.js:652:30)', 'Object.Module._extensions..js (module.js:663:10)']} 2019-05-31 14:31:59 Function returned an invalid response (must include one of: body, headers or statusCode in the response object). Response received: {"errorMessage":"/var/task/node_modules/node-expat/build/Release/node_expat.node: invalid ELF header","errorType":"Error","stackTrace":["Module.load (module.js:565:32)","tryModuleLoad (module.js:505:12)","Function.Module._load (module.js:497:3)","Module.require (module.js:596:17)","require (internal/module.js:11:18)","bindings (/var/task/node_modules/bindings/bindings.js:112:48)","Object.<anonymous> (/var/task/node_modules/node-expat/lib/node-expat.js:4:32)","Module._compile (module.js:652:30)","Object.Module._extensions..js (module.js:663:10)"]} 2019-05-31 14:31:59 127.0.0.1 - - [31/May/2019 14:31:59] "GET /hello HTTP/1.1" 502 - 2019-05-31 14:31:59 127.0.0.1 - - [31/May/2019 14:31:59] "GET /favicon.ico HTTP/1.1" 403 -
ちなみにtemplate.ymlはsam initで発行されたものをそのまま流用しております。
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: > sam-app Sample SAM Template for sam-app # More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst Globals: Function: Timeout: 3 Resources: HelloWorldFunction: Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction Properties: CodeUri: hello-world/ Handler: app.lambdaHandler Runtime: nodejs8.10 Events: HelloWorld: Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api Properties: Path: /hello Method: get Outputs: # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function # Find out more about other implicit resources you can reference within SAM # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api HelloWorldApi: Description: "API Gateway endpoint URL for Prod stage for Hello World function" Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/" HelloWorldFunction: Description: "Hello World Lambda Function ARN" Value: !GetAtt HelloWorldFunction.Arn HelloWorldFunctionIamRole: Description: "Implicit IAM Role created for Hello World function" Value: !GetAtt HelloWorldFunctionRole.Arn
#原因
xml2jsonをnode_modulesに加えなければ、問題なく動きました。
xml2jsonをインストールした際の挙動を追記します。
npm WARN deprecated hoek@4.2.1: This version has been deprecated in accordance with the hapi support policy (hapi.im/support). Please upgrade to the latest version to get the best features, bug fixes, and security patches. If you are unable to upgrade at this time, paid support is available for older versions (hapi.im/commercial). npm WARN deprecated joi@13.7.0: This version has been deprecated in accordance with the hapi support policy (hapi.im/support). Please upgrade to the latest version to get the best features, bug fixes, and security patches. If you are unable to upgrade at this time, paid support is available for older versions (hapi.im/commercial). npm WARN deprecated hoek@5.0.4: This version has been deprecated in accordance with the hapi support policy (hapi.im/support). Please upgrade to the latest version to get the best features, bug fixes, and security patches. If you are unable to upgrade at this time, paid support is available for older versions (hapi.im/commercial). npm WARN deprecated topo@3.0.3: This module has moved and is now available at @hapi/topo. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues. npm WARN deprecated hoek@6.1.3: This module has moved and is now available at @hapi/hoek. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues. > node-expat@2.3.18 install /Users/tsujita/estockweb/docker-lambda/sam-app/hello-world/node_modules/node-expat > node-gyp rebuild CC(target) Release/obj.target/expat/deps/libexpat/lib/xmlparse.o CC(target) Release/obj.target/expat/deps/libexpat/lib/xmltok.o CC(target) Release/obj.target/expat/deps/libexpat/lib/xmlrole.o LIBTOOL-STATIC Release/libexpat.a CXX(target) Release/obj.target/node_expat/node-expat.o SOLINK_MODULE(target) Release/node_expat.node + xml2json@0.11.2 added 12 packages from 54 contributors and audited 13 packages in 16.423s found 0 vulnerabilities
#構造
sam-app内の構造
sam-app |- event.json |- READ.me |- template.yml |- hello-world |- package-lock.json |- package.json |- app.js |- node_modules |- tests
#参考サイト
- SAMのコマンド
https://qiita.com/gnk263/items/7f8796c26b9b61d33d96
- node_modulesを利用する場合
あなたの回答
tips
プレビュー