简体   繁体   English

如何从 AWS Lambda 函数 + 无服务器框架的 URL 中删除阶段?

[英]How to remove stage from URLs for AWS Lambda functions + Serverless framework?

I'm using Serverless Framework to deploy functions in AWS Lambda, but I can't find where/how I can remove the stage specifier from the URL endpoints created.我正在使用无服务器框架在 AWS Lambda 中部署函数,但我找不到在哪里/如何从创建的 URL 端点中删除阶段说明符。 The documentation does not seem to cover this part.文档似乎没有涵盖这部分。

For example, this is my serverless.yml (with irrelevant parts omitted):例如,这是我的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

After a serverless deploy , the following service information is returned: 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

Notice the /dev part in the URL endpoint, and also in the function. That dev is the default value for the stage parameter in the configuration file.请注意 URL 端点以及 function 中的/dev部分。该dev是配置文件中stage参数的默认值。

Specifying stage: something in the serverless.yml file will have that /something as suffix in the URL, and as part of the function.指定stage: something serverless.yml文件中的某些内容将以/something作为 URL 中的后缀,并作为 function 的一部分。

Question: how can I remove the stage specification from the generated URL endpoints, or: how can I prevent that stage specification to become part of the generated URLs?问题:如何从生成的 URL 端点中删除阶段规范,或者:如何防止该阶段规范成为生成的 URL 的一部分?

(That the stage is part of the function, is fine. That will be easy to separate staging and production functions in the AWS Lambda dashboard.) (该阶段是 function 的一部分,这很好。这将很容易在 AWS Lambda 仪表板中分离stagingproduction功能。)

One thing you can do is use a custom domain that you own (eg mycompany.com ) and map that to your API Gateway.您可以做的一件事是使用您拥有的自定义域(例如mycompany.com )并将其映射到您的 API 网关。 This way, rather than making a request to https://ab1cd2ef3g.execute-api.eu-west-1.amazonaws.com/dev/ , you would make a request to https://api.mycompany.com/ .通过这种方式,而不是让一个请求https://ab1cd2ef3g.execute-api.eu-west-1.amazonaws.com/dev/ ,你会作出一个请求https://api.mycompany.com/

There's a plugin called serverless-domain-manager that makes it much easier to set up this custom domains.有一个名为serverless-domain-manager的插件,可以更轻松地设置此自定义域。 Check out this blog post for a full walkthrough on how to use it.查看此博客文章,了解如何使用它的完整演练。

This is an API Gateway feature/convention NOT from Serverless Framework so serverless can't do anything about it.这是一个 API 网关功能/约定,不是来自无服务器框架,因此serverless对此无能为力。

API Gateway requires you with a stage and it is appended at the end of your endpoint. API Gateway 需要一个阶段,它附加在端点的末尾。

API Gateway endpoints are meant for developers though so it is not meant to be user-friendly. API Gateway 端点是为开发人员设计的,所以它并不意味着用户友好。

If you want it to be user-friendly, you can add a custom domain for it.如果您希望它对用户友好,可以为其添加自定义域。 Different stages can have different custom subdomains.不同的阶段可以有不同的自定义子域。

In the local environment, we may use the flag --noPrependStageInUrl when running the dev server: sls offline start --noPrependStageInUrl when using serverless offline.在本地环境中,我们可以在运行 dev server 时使用标志 --noPrependStageInUrl: sls offline start --noPrependStageInUrl当使用sls offline start --noPrependStageInUrl offline 时。 Online, we may set up a CloudFront or custom domain.在线,我们可能会设置 CloudFront 或自定义域。

This can be solved by using httpApi , which does not prepend the stage to the URL path, instead of http which does.这可以通过使用httpApi来解决,它不会将阶段添加到 URL 路径,而不是http

Instead of代替

functions:
  index:
    handler: handler.index
    events:
      - http: # <-- change this
          path: /
          method: get

use this用这个

functions:
  index:
    handler: handler.index
    events:
      - httpApi: # <-- to this
          path: /
          method: get

The http key creates an API Gateway "REST" (aka v1) endpoint whereas httpApi creates an "HTTP" (aka v2) endpoint. http密钥创建一个 API 网关“REST”(又名 v1)端点,而httpApi创建一个“HTTP”(又名 v2)端点。 v1 has more features but v2 is faster and cheaper. v1 具有更多功能,但 v2 更快更便宜。

From the Serverless docs :来自无服务器文档

Despite their confusing name, both versions allow deploying any HTTP API (like REST, GraphQL, etc.).尽管名称令人困惑,但两个版本都允许部署任何 HTTP API(如 REST、GraphQL 等)。

Full comparison https://docs.aws.amazon.com/en_us/apigateway/latest/developerguide/http-api-vs-rest.html全面比较https://docs.aws.amazon.com/en_us/apigateway/latest/developerguide/http-api-vs-rest.html

Triggered by @dashnug's answer "API Gateway requires you with a stage and it is appended at the end of your endpoint" and another reply that I read elsewhere, I 'solved' the problem by making the stage specification a bit less telling (about which stage environment was referred to) by using v1 as a stage.由@dashnug 的回答“API 网关要求你有一个舞台,它被附加在你的端点的末尾”和我在别处读到的另一个回复触发,我通过使舞台规范不那么重要来“解决”了这个问题(关于哪个通过使用v1作为阶段来引用阶段环境)。 That also suggests some sort of API versioning, which is acceptable in my case as well.这也暗示了某种 API 版本控制,这在我的情况下也是可以接受的。

So, my serverless.yml section now contains:所以,我的serverless.yml部分现在包含:

provider:
  name: aws
  runtime: python3.6
  memorySize: 512
  region: ${opt:region, 'eu-west-1'}
  profile: ${opt:profile, 'default'}
  stage: ${opt:stage, 'v1'}  # A trick to don't end up with "production" or "staging" as stage.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 AWS 的无服务器框架获取最新的 Layer 版本 Lambda - How to get latest Layer version with serverless framework for AWS Lambda 使用无服务器框架在 lambda AWS 中授予授权代码 - Authorization code grant in lambda AWS with Serverless Framework AWS Lambda function on Java with Serverless framework and GraalVM - AWS Lambda function on Java with Serverless framework and GraalVM 是否可以从 AWS 无服务器 API 中的其他 lambda 函数调用 lambda function? - Is it possible to call lambda function from other lambda functions in AWS serverless API? 使用无服务器框架的 Lambda Snapstart - Lambda Snapstart with Serverless framework 来自无服务器框架的日志未显示在 AWS CloudWatch 上 - Logs from Serverless framework is not showing on AWS CloudWatch Typeorm 不适用于无服务器框架 AWS Lambda - Typeorm doesn't work with Serverless Framework AWS Lambda 带有 AWS Lambda 错误“找不到模块”的无服务器框架 - Serverless Framework with AWS Lambda error "Cannot find module" 在等待数据库查询之前返回 AWS Lambda 响应 - 无服务器框架 - Returning AWS Lambda response before waiting for DB query - Serverless framework 使用无服务器框架将 Python package 部署到 AWS lambda 时出错 - Error deploying Python package to AWS lambda using Serverless framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM