标签: serverless

使用无服务器有条件地设置 lambda 的预置并发

lambda 函数是使用 serverless(2.22) 创建的,并使用 codebuild (standard5.0) 进行部署。我想有条件地仅为 UAT 和 Prod env 设置预置并发,而不是为 dev 和 test env 设置预置并发。

这些是我尝试过的事情:

无服务器.yml:

    ...
    alarms:
      - functionErrors
    Conditions:
      isProdOrUAT: {"Fn::Or": [{"${opt:stage}": "prod"}, {"${opt:stage}": "uat"}]} 
functions:
  TestApi:
    handler: test.api.StreamLambdaHandler::handleRequest
    description: Lambda function for the Test API
    timeout: 20
    custom: !If [isProdOrUAT, "${file(./provisionedconcurrency.yml)}", !Ref "AWS::NoValue"]
Run Code Online (Sandbox Code Playgroud)

配置并发.yml

provisionedConcurrency: "${ssm:/${opt:stage}/TestsServiceProvisionedConcurrency}"
Run Code Online (Sandbox Code Playgroud)

在这种情况下,会成功导入文件,构建也通过。但 UAT 和 Prod 的 lambda 配置中没有附加预配置并发。

无服务器.yml

provisionedConcurrency: "${ssm:/${opt:stage}/TestsServiceProvisionedConcurrency}"
Run Code Online (Sandbox Code Playgroud)

在这种情况下出现以下错误

Error --------------------------------------------------
  Error: The CloudFormation template is invalid: [/Resources/TestApiProvConcLambdaAlias/Type/ProvisionedConcurrencyConfig/ProvisionedConcurrentExecutions] 'null' values are not allowed in templates 
Run Code Online (Sandbox Code Playgroud)

检查了一下 …

amazon-web-services aws-lambda serverless-framework serverless

5
推荐指数
1
解决办法
3088
查看次数

Vercel Node Serverless 导入 ES6 模块失败(vercel dev 本地工作正常)

当访问包含无服务器端点的顶级文件夹的 React 应用程序 Vercel 部署中的 API 端点时,我收到此错误api/。有趣的是,这个 API 端点在本地使用vercel dev. 更有趣的是,同一 API 文件夹中也使用 ES6 模块导入的类似 API 端点工作得很好。

[POST] /api/checkout
20:49:23:62
2021-02-20T04:49:23.818Z    undefined   ERROR   /var/task/api/checkout.js:1
import { firebaseAdmin, stripe } from "./_firebase_stripe";
^^^^^^
SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at /var/task/___vc_launcher.js:26:28
    at Object.<anonymous> (/var/task/___vc_launcher.js:74:7)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
RequestId: 0e27de3f-fdd1-401a-a5e2-030327a15a31 Error: Runtime exited with error: …
Run Code Online (Sandbox Code Playgroud)

node.js es6-modules serverless vercel

5
推荐指数
0
解决办法
589
查看次数

Nextjs api“pages/api”在 vercel 服务器上不起作用

大家好,你可以帮助我吗?

\n

我正在尝试配置我的假 API 来创建一些个人项目,但就我而言,使用/pages/api文件夹的方法仅适用于本地主机中的我,当我部署到 Vercel 上的服务器时,项目无法找到我的终点。

\n

就我而言,我使用 src/ 文件夹方法来开发我的应用程序,我不知道这种结构是否会导致 api 文件夹出现问题。

\n

我尝试和工作的一件事是使用应用程序根目录下的 api 文件夹(在 /src 文件夹之外)部署到 vercel,但 api 在本地主机上停止工作。

\n

此结构适用于本地主机,但不适用于服务器:

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80public/\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80src/\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80api/\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80pages/\n    ...\nnext.config.js\npackage.json\n
Run Code Online (Sandbox Code Playgroud)\n

此结构适用于服务器,但不适用于本地主机:

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80api/\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80public/\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80src/\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80pages/\n    ...\nnext.config.js\npackage.json\n
Run Code Online (Sandbox Code Playgroud)\n

这是我用来获取数据的方法:

\n

AXIOS API:

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80public/\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80src/\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80api/\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80pages/\n    ...\nnext.config.js\npackage.json\n
Run Code Online (Sandbox Code Playgroud)\n

驻波比挂钩:

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80api/\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80public/\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80src/\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80pages/\n    ...\nnext.config.js\npackage.json\n
Run Code Online (Sandbox Code Playgroud)\n

驻波比回调:

\n
import axios from \'axios\'\n\nconst api = axios.create({\n  baseURL: \'/api/\'\n})\n\nexport default api\n
Run Code Online (Sandbox Code Playgroud)\n

我希望我能解释一下,我的问题是如何使用此功能的最佳方式,因为 /src 文件夹通常与 nextjs 一起使用,但我不知道这是否是 api 文件夹的真正问题。

\n

谢谢!

\n

rest reactjs next.js serverless vercel

5
推荐指数
1
解决办法
8057
查看次数

Serverless-webpack 不包含 `pg` 包

我正在尝试修改从模板生成的代码aws-nodejs-typescript。我已经安装了typeorm,reflect-metadatapg使用 PostgreSQL 。

他们dependenciespackage.json 在此输入图像描述

webpack 配置是默认配置

const path = require('path');
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');

/*
This line is only required if you are specifying `TS_NODE_PROJECT` for whatever reason.
 */
// delete process.env.TS_NODE_PROJECT;

module.exports = {
  context: __dirname,
  mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
  entry: slsw.lib.entries,
  devtool: slsw.lib.webpack.isLocal ? 'eval-cheap-module-source-map' : 'source-map',
  resolve: {
    extensions: ['.mjs', '.json', '.ts'],
    symlinks: false,
    cacheWithContext: false, …
Run Code Online (Sandbox Code Playgroud)

node.js typescript webpack typeorm serverless

5
推荐指数
1
解决办法
1446
查看次数

有没有办法将我的 Google Cloud Functions 支出分解为每一项功能?

我想知道我的每一项云功能的支出是多少(分解我的结算控制台中显示的 Google Cloud Functions 的总成本)。我知道我也许可以通过查看每个函数的指标和配置来估计它,但是逐个进行会很麻烦(我有很多函数)。

我找不到任何单独使用计费控制台的方法,它只显示总成本。

如果有一个工具/脚本,我们也将不胜感激。

cost-management google-cloud-functions serverless google-cloud-billing

5
推荐指数
1
解决办法
1209
查看次数

Swagger UI 和 Vercel:JSON 中位置 1 处出现意外标记 &lt;

代码

完整的代码库和文件夹结构可以在GitHub中查看

这是 Swagger 相关路由(必须使其成为独立服务器)

// api/v1.ts

import express = require("express");
import swaggerJSDoc = require("swagger-jsdoc");
import swaggerUi = require("swagger-ui-express");
import packageJSON = require("../package.json");
import path = require("path");

const app = express();
app.use(express.json());
app.use(express.static(path.resolve(__dirname, "../", "public")));

const swaggerSpec = swaggerJSDoc({
  swaggerDefinition: some_spec,
  apis: ["api/*"]
});

const cssOpts = some_css_override;

app.use("/api/v1", swaggerUi.serve, swaggerUi.setup(swaggerSpec, cssOpts));

module.exports = app;
Run Code Online (Sandbox Code Playgroud)

问题

当我运行vercel dev(本地主机:3000/api/v1)时,我按预期看到文档: 本地(vercel 开发)

但是,当我将代码推送到触发vercel build 的分支时,我看到以下内容: 维塞尔构建

检查控制台,我看到:

DevTools failed to load source map: Could not parse content for https://colormaster-1unjfn63b-lbragile.vercel.app/api/v1/swagger-ui-bundle.js.map: Unexpected token …
Run Code Online (Sandbox Code Playgroud)

swagger swagger-ui serverless vercel

5
推荐指数
1
解决办法
2074
查看次数

AWS Lambda函数错误:找不到模块“lambda”

我正在尝试使用无服务器在 AWS 中部署 REST API。节点版本 14.17.5。

我的目录结构:

在此输入图像描述

当我成功部署上述内容时,我在尝试访问 api 时收到以下错误。

2021-09-28T18:32:27.576Z    undefined   ERROR   Uncaught Exception  {
    "errorType": "Error",
    "errorMessage": "Must use import to load ES Module: /var/task/lambda.js\nrequire() of ES modules is not supported.\nrequire() of /var/task/lambda.js from /var/runtime/UserFunction.js is an ES module file as it is a .js file whose nearest parent package.json contains \"type\": \"module\" which defines all .js files in that package scope as ES modules.\nInstead rename lambda.js to end in .cjs, change the requiring code to use import(), …
Run Code Online (Sandbox Code Playgroud)

node.js aws-lambda serverless

5
推荐指数
1
解决办法
1万
查看次数

Vercel Edge Functions 在本地工作,并在尝试使用“主机名重写”时在生产中给出 404

我正在尝试使用 Vercel next.js 中的新边缘函数,但它在生产中不起作用,总是给出 404 错误。

我按照这个例子做了,但没有任何效果。

也许是因为我使用的是 i18n?

我的_middleware.js文件如下所示:

import {NextResponse } from 'next/server'

export default function middleware(req) {
    const { pathname } = req.nextUrl
    let hostname = req.headers.get('host')


if (
    !pathname.includes('.') && // exclude all files in the public folder
    !pathname.startsWith('/api') // exclude all API routes
) {
    if (
        !hostname.includes('example.vercel.app') &&
        hostname !== 'example.com' &&
        hostname !== 'localhost:3000'
    ) {
        return NextResponse.rewrite(`/_sites/${hostname}${pathname}`)
    }
}
}
Run Code Online (Sandbox Code Playgroud)

在页面下我使用这个结构:

/pages
 /_sites
  [sites]
    index.js
Run Code Online (Sandbox Code Playgroud)

请帮我找到解决方案。

url-rewriting next.js serverless vercel

5
推荐指数
0
解决办法
1546
查看次数

无服务器框架无服务器离线启动获取请求时出现错误找不到模块“node:url”(Lambda AWS)

我有以下问题...事实证明我无法通过 Nodejs 的无服务器框架中的无服务器离线插件在本地测试我的 aws lambda 函数。无法执行简单的 GET 请求。

我可以使用serverless deploy命令部署到 aws lambda,但为了团队获得更好的开发体验,我们需要使用 serverless-offline 进行本地部署,我遵循了本指南https://fauna.com/blog/develop-using-serverless -offline,但是当我在成功运行命令后执行简单的获取请求时卡住了serverless offlineserverless offline这是我成功 运行时得到的无服务器离线命令输出

但是当我通过邮递员对链接执行获取请求时,http://localhost:3000/出现以下错误

获取请求错误

它基本上说找不到节点:url 模块,但我做了 npm install url,但仍然抛出同样的错误。我将在下面插入项目树和文件:

无服务器 hello world 树结构

处理程序.js

//"use strict";

console.log("AJAJAJAJAJAJAJAJAJAAJAJ");

console.log("Printing module");
console.log(module.exports);

module.exports.hello = async (event) => {
  console.log("whaaaaat");
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: "Go Serverless v3.0! Your function executed successfully!",
        input: event,
      },
      null,
      2
    ),
  };
};
Run Code Online (Sandbox Code Playgroud)

无服务器.yml

org: ortizjorge97
app: aws-node-http-api-project
service: aws-node-http-api-project
frameworkVersion: '3' …
Run Code Online (Sandbox Code Playgroud)

node.js aws-lambda serverless-framework serverless serverless-offline

5
推荐指数
1
解决办法
3715
查看次数

调用 Vercel 无服务器函数时 Stripe Webhook 返回 308 错误

我已经使用 Stripe 设置了一个 Webhook,它在触发时调用无服务器函数。

该函数的目的是在调用时更新我的​​数据库中的条目,表明用户已注册高级帐户。

当我在本地运行它时,网络钩子工作得很好。它触发 API、更新用户并处理付款。

然而,当它上线运行时,我不断收到 308 错误:

重定向到 my-app-url.com

这是我的函数的代码:

import { buffer } from "micro"
import { createClient } from "@supabase/supabase-js";

require("dotenv").config();

const stripe = require("stripe")(process.env.STRIPE_LIVE_KEY)

const endpointSecret = process.env.STRIPE_WEBHOOK_SECRET

const supabaseUrl = process.env.REACT_APP_SUPABASE_URL
const supabaseAnonKey = process.env.REACT_APP_SUPABASE_ANON_KEY

const supabase = createClient(supabaseUrl, supabaseAnonKey)

module.exports = async (req, res) => {

   const signature = req.headers["stripe-signature"]
   const reqBuffer = await buffer(req)

   let event 

   try {
    event = stripe.webhooks.constructEvent(reqBuffer, signature, endpointSecret)
   } catch (err) {
    console.log(err)
    return res.status(400).send(`Webhook error: …
Run Code Online (Sandbox Code Playgroud)

node.js stripe-payments serverless vercel

5
推荐指数
1
解决办法
1548
查看次数