我想创建一个包来使用AWS部署无服务器和的WebPack。
在serverless.yml我想声明所有资源(主要是 DynamoDb 表)和函数。我想使用外部node.js库。
文件夹结构为:
|- serverless.yml
|- webpack.config.js
|- package.json
|- src
\ - file1.js
| - file2.js
Run Code Online (Sandbox Code Playgroud)
摘自 serverless.yml
functions:
function1:
handler: src/file1.f1
function2:
handler: src/file2.f2
Run Code Online (Sandbox Code Playgroud)
摘自 webpack.config.js
module.exports = {
entry: {
file1: './src/file1.js',
file2: './src/file2.js',
},
target: 'node',
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, '.webpack'),
filename: '[name].js',
},
module: {
loaders: [
{
test: /\.json$/,
loaders: ['json-loader'],
},
],
},
};
Run Code Online (Sandbox Code Playgroud)
做当serverless deploy一切都很好,但测试时的λ我得到一个错误:
{
"errorMessage": "Cannot …Run Code Online (Sandbox Code Playgroud) amazon-web-services node.js webpack serverless-framework serverless-architecture
有没有办法从 API 网关返回自定义错误对象和状态代码?我获得了 200 状态。
var response = {
status: 400,
errors: [
{
code: "226",
message: "Password and password confirmation do not match."
}
]
}
context.done(JSON.stringify(response));
Run Code Online (Sandbox Code Playgroud) 我尝试将 2 个表添加到 serverless.yml 以与 DynamoDB 链接。
我在 serverless.yml 中的一部分代码:
...
resources:
Resources:
ItemsTable:
Type: "AWS::DynamoDB::Table"
Properties:
TableName: "InvoiceConfig"
AttributeDefinitions:
- AttributeName: "providerName"
AttributeType: "S"
KeySchema:
- AttributeName: "providerName"
KeyType: "HASH"
ProvisionedThroughput:
ReadCapacityUnits: 2
WriteCapacityUnits: 2
TableName: "DifferentTermsPages"
AttributeDefinitions:
- AttributeName: "id"
AttributeType: "S"
- AttributeName: "providerName"
AttributeType: "S"
- AttributeName: "productType"
AttributeType: "S"
- AttributeName: "language"
AttributeType: "S"
- AttributeName: "terms"
AttributeType: "L"
KeySchema:
- AttributeName: "id"
KeyType: "HASH"
- AttributeName: "providerName"
KeyType: "HASH"
- AttributeName: "productType"
KeyType: "HASH" …Run Code Online (Sandbox Code Playgroud) 我正在运行如下命令。
serverless invoke local --function twilio_incoming_call
Run Code Online (Sandbox Code Playgroud)
当我在我的代码中本地运行时,我计划检测这一点,而不是寻找 POST 变量,而是寻找我将提供的 MOCK 文件。
但是,我不知道如何检测我是否使用此本地命令运行无服务器。
你怎么做到这一点?
我在无服务器网站上环顾四周,可以找到很多关于在本地运行但没有检测到您是否在本地的信息。
在带有 event: 的 lambda 函数中,在s3:ObjectCreated:*创建的对象上调用 head 对象会返回 NotFound 错误。
module.exports.handler = async function(event, context, callback) {
try {
const Bucket = event.Records[0].s3.bucket.name;
const Key = event.Records[0].s3.object.key;
console.log('Bucket', Bucket);
console.log('Key', Key);
const objectHead = await s3.headObject({ Bucket, Key }).promise();
console.log('Alas! I will never discover that the objectHead is:', objectHead);
callback();
} catch(err) {
console.error('Error', err);
callback(err);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
{
NotFound: null
message: null,
code: 'NotFound',
region: null,
time: 2018-02-19T11:06:35.894Z,
requestId: 'XXXXXXXXXXX',
extendedRequestId: 'XXX.....XXX',
cfId: undefined,
statusCode: 404,
retryable: …Run Code Online (Sandbox Code Playgroud) 如果我想每 2 分钟启动一个 lambda 函数,就会调用一个带有索引号的 api。我将如何存储 lambda 的索引号以在初始化时读取并在每次 lambda 函数成功调用 api 时将其加一?
我认为只为柜台准备一个发电机桌是矫枉过正的。
我一直在使用 Node 6 和我的无服务器应用程序,并且我决定迁移到 async/await,因为版本 8.x 已发布。
尽管如此,我的授权人功能有问题。由于我删除了回调参数并仅返回值,因此它停止工作。如果我向回调参数发送一些内容,它会继续正常工作,但它不是类似异步/等待的。即使我抛出异常,它也不起作用。
module.exports.handler = async (event, context) => {
if (typeof event.authorizationToken === 'undefined') {
throw new InternalServerError('Unauthorized');
}
const decodedToken = getDecodedToken(event.authorizationToken);
const isTokenValid = await validateToken(decodedToken);
if (!isTokenValid) {
throw new InternalServerError('Unauthorized');
} else {
return generatePolicy(decodedToken);
}
};
Run Code Online (Sandbox Code Playgroud)
有关如何进行的任何建议?
谢谢大家!
尝试sls deploy使用带有schedule事件的函数时出现以下错误:
An error occurred: SupWorldEventsRuleSchedule1 - The requested
resource exceeds the maximum number allowed. (Service:
AmazonCloudWatchEvents; Status Code: 400; Error Code:
LimitExceededException; Request ID: f39cee40-a651-11e8-a111-97a9e3d0f938).
Run Code Online (Sandbox Code Playgroud)
配置:
functions:
supWorld:
handler: dist/handlers/index.helloWorld
events:
- schedule:
rate: cron(*/10 * * * ? *)
enabled: true
Run Code Online (Sandbox Code Playgroud)
此错误的奇怪之处在于没有schedule与此 lambda 堆栈相关联的其他事件。
var result = [{
count : 10,
data : [{"id":11,"id":22}]
}];
var response = {
statusCode: 200,
count: result.length,
body: result
};
callback(null, response);
Run Code Online (Sandbox Code Playgroud)
控制台错误
根据 API Gateway 规范,正文内容必须是字符串化的。检查您的 Lambda 响应并确保您在 body 对象上调用 JSON.stringify(YOUR_CONTENT)
node.js aws-lambda aws-api-gateway serverless-framework serverless-framework-offline
我设法根据https://serverless.com/framework/docs/providers/cloudflare/guide/使用无服务器框架部署了我的第一个 cloudflare 工作器, 并且当我到达云端时它正在工作。
在开发过程中,希望能够在http://localhost:8080/ *上进行测试
使用 serverless.yml 中指定的函数启动本地 http 服务器并处理我的请求的最简单方法是什么?
我查看了https://github.com/serverless/examples/tree/master/google-node-simple-http-endpoint 但没有“开始”脚本。
https://github.com/serverless/上似乎没有关于 cloudflare 的例子
cloudflare serverless-framework serverless serverless-plugins cloudflare-workers
aws-lambda ×6
node.js ×3
serverless ×3
amazon-s3 ×1
async-await ×1
cloudflare ×1
serverless-framework-offline ×1
webpack ×1