英文:
detect is localhost AWS Serverless framework
问题
我使用Serverless离线库运行我的无服务器应用,使用sls offline
命令,我有几个Lambda函数。如何检测我的Lambda API是从本地主机还是服务器触发的?谢谢。
我已经尝试过像process.env.IS_LOCAL
、process.env.AWS_EXECUTION_ENV
等方法,但返回未定义。
英文:
i'm running my serverless with serverless offline library, i'm running using sls offline
and i have several. how i detect is my lambda API triggered from localhost or server? thank you
i already try like process.env.IS_LOCAL, process.env.AWS_EXECUTION_ENV but returning undefined.
答案1
得分: 1
你可以像这样使用 LAMBDA_TASK_ROOT
:
const isAws = !!process.env.LAMBDA_TASK_ROOT
英文:
You can use LAMBDA_TASK_ROOT
as in
const isAws = !!process.env.LAMBDA_TASK_ROOT
答案2
得分: 1
你可以在你的 Lambda 处理程序中添加类似 console.log(process.env)
的代码,以了解在从本地主机或服务器调用时存在哪些环境变量,然后查看你将使用哪些来检测 Lambda 的触发方式。
例如:
exports.handler = async (event) => {
const response = {
statusCode: 200,
body: {"env": process.env},
};
console.log(process.env)
return response;
};
会返回:
{
"statusCode": 200,
"body": {
"env": {
"AWS_LAMBDA_FUNCTION_VERSION": "$LATEST",
"AWS_SESSION_TOKEN": "TOKEN",
"LAMBDA_TASK_ROOT": "/var/task",
"LD_LIBRARY_PATH": "/var/lang/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/lib",
"AWS_LAMBDA_LOG_GROUP_NAME": "/aws/lambda/credential-exfiltration",
"AWS_LAMBDA_RUNTIME_API": "127.0.0.1:9001",
"AWS_LAMBDA_LOG_STREAM_NAME": "2023/07/11/[$LATEST]c50b302786f4448ba80c142eea1c94fa",
"AWS_EXECUTION_ENV": "AWS_Lambda_nodejs16.x",
"AWS_XRAY_DAEMON_ADDRESS": "169.254.79.129:2000",
"AWS_LAMBDA_FUNCTION_NAME": "credential-exfiltration",
"PATH": "/var/lang/bin:/usr/local/bin:/usr/bin/:/bin:/opt/bin",
"AWS_DEFAULT_REGION": "us-east-1",
"PWD": "/var/task",
"AWS_SECRET_ACCESS_KEY": "AWS_SECRET_ACCESS_KEY",
"LANG": "en_US.UTF-8",
"LAMBDA_RUNTIME_DIR": "/var/runtime",
"AWS_LAMBDA_INITIALIZATION_TYPE": "on-demand",
"NODE_PATH": "/opt/nodejs/node16/node_modules:/opt/nodejs/node_modules:/var/runtime/node_modules:/var/runtime:/var/task",
"AWS_REGION": "us-east-1",
"TZ": ":UTC",
"AWS_ACCESS_KEY_ID": "AWS_ACCESS_KEY_ID",
"SHLVL": "0",
"_AWS_XRAY_DAEMON_ADDRESS": "169.254.79.129",
"_AWS_XRAY_DAEMON_PORT": "2000",
"AWS_XRAY_CONTEXT_MISSING": "LOG_ERROR",
"_HANDLER": "index.handler",
"AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "128",
"NODE_EXTRA_CA_CERTS": "/etc/pki/tls/certs/ca-bundle.crt",
"_X_AMZN_TRACE_ID": "Root=1-64ad16dd-11b6c8747177397c3f4f0f2e;Parent=49939b5605c39c50;Sampled=0;Lineage=920b0c92:0"
}
}
}
希望对你有所帮助!
英文:
You may want to add something like console.log(process.env)
in your lambda handler to learn what environment variables exist in both cases called either from localhost or server, then see what you would use to detect how lambda is triggered.
for example:
exports.handler = async (event) => {
const response = {
statusCode: 200,
body: {"env": process.env},
};
console.log(process.env)
return response;
};
Would return:
{
"statusCode": 200,
"body": {
"env": {
"AWS_LAMBDA_FUNCTION_VERSION": "$LATEST",
"AWS_SESSION_TOKEN": "TOKEN",
"LAMBDA_TASK_ROOT": "/var/task",
"LD_LIBRARY_PATH": "/var/lang/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/lib",
"AWS_LAMBDA_LOG_GROUP_NAME": "/aws/lambda/credential-exfiltration",
"AWS_LAMBDA_RUNTIME_API": "127.0.0.1:9001",
"AWS_LAMBDA_LOG_STREAM_NAME": "2023/07/11/[$LATEST]c50b302786f4448ba80c142eea1c94fa",
"AWS_EXECUTION_ENV": "AWS_Lambda_nodejs16.x",
"AWS_XRAY_DAEMON_ADDRESS": "169.254.79.129:2000",
"AWS_LAMBDA_FUNCTION_NAME": "credential-exfiltration",
"PATH": "/var/lang/bin:/usr/local/bin:/usr/bin/:/bin:/opt/bin",
"AWS_DEFAULT_REGION": "us-east-1",
"PWD": "/var/task",
"AWS_SECRET_ACCESS_KEY": "AWS_SECRET_ACCESS_KEY",
"LANG": "en_US.UTF-8",
"LAMBDA_RUNTIME_DIR": "/var/runtime",
"AWS_LAMBDA_INITIALIZATION_TYPE": "on-demand",
"NODE_PATH": "/opt/nodejs/node16/node_modules:/opt/nodejs/node_modules:/var/runtime/node_modules:/var/runtime:/var/task",
"AWS_REGION": "us-east-1",
"TZ": ":UTC",
"AWS_ACCESS_KEY_ID": "AWS_ACCESS_KEY_ID",
"SHLVL": "0",
"_AWS_XRAY_DAEMON_ADDRESS": "169.254.79.129",
"_AWS_XRAY_DAEMON_PORT": "2000",
"AWS_XRAY_CONTEXT_MISSING": "LOG_ERROR",
"_HANDLER": "index.handler",
"AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "128",
"NODE_EXTRA_CA_CERTS": "/etc/pki/tls/certs/ca-bundle.crt",
"_X_AMZN_TRACE_ID": "Root=1-64ad16dd-11b6c8747177397c3f4f0f2e;Parent=49939b5605c39c50;Sampled=0;Lineage=920b0c92:0"
}
}
}
Hope that helps!
答案3
得分: 0
如果你想确定Lambda API是在本地主机还是服务器上触发,你可以使用 serverless offline
插件,然后简单地使用 IS_OFFLINE 环境变量。如果你使用 sls offline
调用Lambda,process.env.IS_OFFLINE 的值将为 true
。
在 package.json 中,你可以添加这个依赖:
"devDependencies": {
"serverless-offline": "^8.8.0"
},
在 serverless.yml 中,你可以添加这行:
# 插件
plugins:
- serverless-offline
这是相关的文档,你可以阅读 链接。
英文:
If you want to determine whether the Lambda API is triggered on the localhost or server, you can use serverless offline plugins
then you can simply use the IS_OFFLINE environment variable. If you invoke Lambda using sls offline
process.env.IS_OFFLINE value will be true
.
in package.json you can add this dependency :
"devDependencies": {
"serverless-offline": "^8.8.0"
},
in serverless.yml you can add this line :
# Plugins
plugins:
- serverless-offline
This is the related documentation that you can read
link
答案4
得分: 0
如果您正在使用 serverless-offline
插件,最佳方法是使用文档中记录的 process.env.IS_OFFLINE
变量,点击这里查看。
英文:
If you're using serverless-offline
plugin, the best approach is to use process.env.IS_OFFLINE
variable documented here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论