我的情况是我在向我的无服务器框架项目添加外部NPM包时遇到了一些麻烦(特定包是geopoint).
我去了无服务器项目的根文件夹并运行npm install geopoint --save.package.json已更新,dependencies": { "geopoint": "^1.0.1" }并创建了node_modules文件夹.
我的文件夹结构是这样的:
根项目文件夹
-functions
--geospatial
--- handler.js
-node_modules
--geopoint
在我的函数/ geospatial/handler.js中,我将geopoint模块声明为:
var geopoint = require('geopoint');
var geopoint = require('../../geopoint');
var geopoint = require('../../../geopoint');
Run Code Online (Sandbox Code Playgroud)
lambda控制台返回错误:
{
"errorMessage": "Cannot find module '../../geopoint'",
"errorType": "Error",
"stackTrace": []
}
Run Code Online (Sandbox Code Playgroud)
如何正确地将外部NPM模块添加到无服务器框架项目?
我想定义请求和响应模型.我在AWS中使用无服务器框架,我所看到的一切建议使用无服务器aws文档
README说我需要有这条线 custom.documentation.models.MODELNAME
schema: ${file(models/error.json)}
Run Code Online (Sandbox Code Playgroud)
但是他们没有models/error.json用作基线的示例文件.
在实际的例子中,serverless.yml有一个这样的定义:
-
name: DoSomethingRequest
contentType: "application/json"
schema:
type: array
items:
type: string
Run Code Online (Sandbox Code Playgroud)
这没有为我正在尝试做的事情提供足够的细节.
我的目标是为字符串对象,消息和状态代码数组定义模式.但是,消息和状态代码是可选的.这些也可能是其他模型的一部分,如果可能的话,我不想重复每个模型的定义.
我目前的尝试是:
-
name: ReturnArrayResponse
contentType: "application/json"
schema:
type: array
itemsArray:
type: string
message:
type: string
statusCode:
type: number
Run Code Online (Sandbox Code Playgroud)
我认为这会做我想要的,但我怎么能拥有message并可statusCode选择并在我的其他模特中重复这两个项目?
我很满意我可以放入我的serverless.yml文件或我可以引用的json文件的yml解决方案.
amazon-web-services aws-api-gateway serverless-framework serverless-plugins
我在 Vercel 上部署了一个 Nuxt.js 服务器端网站。我注意到,在某些情况下,我会收到504: GATEWAY_TIMEOUT错误,并显示代码FUNCTION_INVOCATION_TIMEOUT和消息“此无服务器函数已超时”。
为什么我会看到这个错误?
我使用nodeJS sdk将项目放到dynamoDB,该项目是:
{
"eventId": date + '-' + eventName + '-' + eventPurpose,
"eventName": eventName,
"eventPurpose": eventPurpose,
"eventDates": eventDates,
"attendees": attendees
}
Run Code Online (Sandbox Code Playgroud)
将项目放入dynamoDB的当前代码:
const params = {
TableName: "event",
Item: {
"eventId": date + '-' + eventName + '-' + eventPurpose,
"eventName": eventName,
"eventPurpose": eventPurpose,
"eventDates": eventDates,
"attendees": attendees
},
ReturnValues: "ALL_OLD"
};
dynamo.put(params, (err, data) => {
console.log("coming here");
if (err) {
console.log("error : " + JSON.stringify(err));
}
console.log("data" + JSON.stringify(data));
cb(null, data);
});
Run Code Online (Sandbox Code Playgroud)
插入正确发生,返回值是一个空对象.
我想返回插入的项目.我找到了这个文档.但只有在更新旧值时才会返回.除了这个,我找不到任何其他有用的信息.
是否有任何解决方法或者我们只需要 …
amazon-web-services node.js amazon-dynamodb aws-lambda serverless-framework
我想动态地在我的文件中构建一个ARN,但我需要获取当前的AccountId.如何将其作为变量访问?
例如:
example: arn:aws:states:${region}:${accountId}:stateMachine:${self:service}-${self:custom.stage}-example
Run Code Online (Sandbox Code Playgroud)
参考当前region和accountId?的正确方法是什么?
编辑:(解决方案)
由于解决方案的丑陋和冗长,我对这个解决方案并不十分满意,Fn::Join但我最终做的是制作一个arns.yml文件,其中所有这些只在这一个地方然后通过其他地方的变量引用.
# arns.yml
example:
Fn::Join:
- ":"
- - arn
- aws
- states
- Ref: AWS::Region
- Ref: AWS::AccountId
- stateMachine
- ${self:service}-${self:custom.stage}-example
Run Code Online (Sandbox Code Playgroud)
然后:
# serverless.yml
custom:
stage: "${opt:stage, self:provider.stage}"
functions:
foo:
handler: handler.foo
environment:
example_arn: ${file(arns.yml):example}
Run Code Online (Sandbox Code Playgroud)
编辑2 :(更好的解决方案)
这可能听起来很蹩脚,但我最终选择的解决方案是将其硬编码到我的自定义变量中.我实际上有两个帐户,我使用自定义生成步骤来复制具有帐户特定设置的两个文件,如下所示:
account.stag.yml
account.prod.yml
Run Code Online (Sandbox Code Playgroud)
每个文件可能如下所示:
# account.stag.yml
account: 123456789
region: ${opt:region, "us-east-1"}
domain: mycompany.qa
Run Code Online (Sandbox Code Playgroud)
当我构建时,我指定一个帐户,并使用gulp来完成我的所有建筑:
gulp build --account stag
Run Code Online (Sandbox Code Playgroud)
然后,我将我的帐户特定设置重命名为
build/account.yml
Run Code Online (Sandbox Code Playgroud)
我可以在我的serverless.yml中引用它,如下所示:
# build/serverless.yml
custom: ${file(account.yml)}
functions: …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用AWS Lambda和无服务器框架为单页面Web应用程序构建API .我想使用Redis Cloud进行存储,主要是因为它结合了速度和数据持久性.我将来可能会使用更多的Redis Cloud功能,所以我宁愿避免使用ElastiCache.我的Redis Cloud实例与我的函数在同一AWS区域中运行.
我有一个函数调用related,它从GET请求到API端点获取一个hashtag,并检查数据库中是否有一个条目.如果它在那里,它应该立即返回结果.如果没有,它应该查询RiteTag,将结果写入Redis,然后将结果返回给用户.
我对此很陌生,所以我可能正在做一些天真的事情.这是事件处理程序:
'use strict'
const lib = require('../lib/related')
module.exports.handler = function (event, context) {
lib.respond(event, (err, res) => {
if (err) {
return context.fail(err)
} else {
return context.succeed(res)
}
})
}
Run Code Online (Sandbox Code Playgroud)
这是../lib/related.js文件:
var redis = require('redis')
var jsonify = require('redis-jsonify')
var rt = require('./ritetag')
var redisOptions = {
host: process.env.REDIS_URL,
port: process.env.REDIS_PORT,
password: process.env.REDIS_PASS
}
var client = jsonify(redis.createClient(redisOptions))
module.exports.respond = …Run Code Online (Sandbox Code Playgroud) amazon-web-services redis node.js aws-lambda serverless-framework
我正在使用 Node JS 无服务器框架构建微服务。说实话,这是我第一次使用 Serverless 构建微服务。我已经在我的计算机上配置了 AWS 凭证。
我创建了运行以下命令的项目。
serverless create -u https://github.com/codingly-io/sls-base -n aution-service
Run Code Online (Sandbox Code Playgroud)
然后我npm install在项目文件夹中运行。
这是我的 serverless.yml 文件夹。
service:
name: auction-service
plugins:
- serverless-bundle
- serverless-pseudo-parameters
provider:
name: aws
runtime: nodejs12.x
memorySize: 256
stage: ${opt:stage, 'dev'}
region: eu-west-2
functions:
hello:
handler: src/handlers/hello.handler
events:
- http:
method: GET
path: /hello
Run Code Online (Sandbox Code Playgroud)
我已经有了模板附带的 lambda 函数。然后我运行serverless deploy来部署服务。当我运行命令时,出现以下错误。
Error:
Object notation for "service" property is not supported. Set "service" property directly with service name.
Run Code Online (Sandbox Code Playgroud)
我的配置或代码有什么问题以及如何修复它?
我想用无服务器创建一个安全的APIG,在我目前的" s-fuction.json "我已经拥有:
"apiKeyRequired": true,
Run Code Online (Sandbox Code Playgroud)
在我的" s-resources-cf.json "中我已经拥有:
"AWSApiKey": {
"Type": "AWS::ApiGateway::ApiKey",
"Properties" : {
"Description" : "ApiKey for secure the connections to the xxx API",
"Enabled" : true
}
}
Run Code Online (Sandbox Code Playgroud)
它正确地为lambda(包括CORS)和API Key创建了一个Lambda,一个APIG,但是我需要手动"分配"生成的APIG-Stage的密钥,你对我怎么能这样做有什么想法自动使用无服务器?
我从这里阅读了有关我想要的功能的AWS文档(似乎有可能):AWS CloudFormation API密钥
文档显示它可以通过以下方式完成:
"ApiKey": {
"Type": "AWS::ApiGateway::ApiKey",
"DependsOn": ["TestAPIDeployment", "Test"],
"Properties": {
"Name": "TestApiKey",
"Description": "CloudFormation API Key V1",
"Enabled": "true",
"StageKeys": [{
"RestApiId": { "Ref": "RestApi" },
"StageName": "Test"
}]
}
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何添加对无服务器自动创建的APIG的引用以及如何等待创建APIG.
何时释放lambda中的连接和清理资源.在普通的Node JS应用程序中,我们使用钩子
process.on('exit', (code) => {
console.log(`About to exit with code: ${code}`);
});
Run Code Online (Sandbox Code Playgroud)
但是,这不适用于AWS Lambda.导致睡眠模式下的Mysql连接.我们没有足够的资源用于此类活动连接.AWS文档均未指定实现此目的的方法.
如何接收AWS Lambda容器的停止事件?
我正在使用无服务器框架在AWS Lambda中部署功能,但我无法找到从创建的URL端点中删除阶段说明符的位置/方式.文档似乎没有涉及这一部分.
例如,这是我的serverless.yml(省略了不相关的部分):
service: cd-mock
provider:
name: aws
runtime: python3.6
region: eu-west-1
package:
include:
- handler.py
functions:
index:
handler: handler.index
events:
- http:
path: /
method: get
Run Code Online (Sandbox Code Playgroud)
在a之后serverless deploy,返回以下服务信息:
service: cd-mock
stage: dev
region: eu-west-1
stack: cd-mock-dev
api keys:
None
endpoints:
GET - https://ab1cd2ef3g.execute-api.eu-west-1.amazonaws.com/dev/
functions:
index: cd-mock-dev-index
Run Code Online (Sandbox Code Playgroud)
注意/devURL端点中的部分以及函数中的部分.这dev是stage配置文件中参数的默认值.
stage: something在serverless.yml文件中指定将/something在URL 中作为后缀,并作为函数的一部分.
问题:如何从生成的URL端点中删除阶段规范,或者:如何防止该阶段规范成为生成的URL的一部分?
(该阶段是该功能的一部分,很好.这将很容易在AWS Lambda仪表板中分离staging和production运行.)
amazon-web-services aws-lambda serverless-framework serverless-plugins