【发布时间】:2021-05-20 13:55:42
【问题描述】:
我正在为我的电子商务应用程序创建一个设计。它将拥有由 AWS Lambdas 支持的多项服务。 Orderservice、InventoryService、PaymentService、LoggingService、dashboardService 是其中的一些服务。我不能给出确切的服务数量,但肯定会超过 15 个。根据这个链接microservices ,一个好的微服务架构应该有一个网关可以路由到相应的服务。我的 AWS ApiGateway 与 order Lambda 函数的代码如下所示。
我的问题是每个 Orderservice、inventoryservice、paymentservice 等都可以有多个获取、发布、删除、放置的路线。它们中的大多数将具有嵌套资源。在这种情况下,我应该在同一个 SAM 模板中包含所有这些服务的 api 路由和 lambda 函数。如果是,那它不是一个整体模板吗?如果我需要更改任何服务,我必须部署整个模板并打破微服务原则。理想情况下,我想独立部署所有这些服务并共享 ApiGateway。这可能吗 ?。如果没有,我是否应该在不同的 SAM 模板中为每个服务创建单独的 ApiGateway,以便可以单独部署它们。这将导致在所有网关中重复身份验证、授权和监控,这听起来又不像是要走的路。
请提出正确的方法。
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Description: ApiGateway for the Ecommerce
Resources:
EcomApi:
Type: AWS::Serverless::Api
Properties:
DefinitionBody:
swagger: "2.0"
info:
title: Ecommerce Api
schemes:
- "https"
x-amazon-apigateway-request-validator: Validate body and params
paths:
/order:
get:
summary: Get the orders
consumes:
- "application/json"
produces:
- "application/json"
responses:
"200":
description: "200 response"
schema:
$ref: "#/definitions/Empty"
headers:
Access-Control-Allow-Origin:
type: "string"
security:
- Auth: []
x-amazon-apigateway-integration:
type: "AWS_PROXY"
httpMethod: "POST"
uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetOrders.Arn}/invocations
responses:
default:
statusCode: "200"
responseParameters:
method.response.header.Access-Control-Allow-Origin: "'*'"
passthroughBehavior: "when_no_match"
contentHandling: "CONVERT_TO_TEXT"
GetOrders:
Type: AWS::Serverless::Function
Properties:
CodeUri: .
Handler: src/handlers/getOrders.handler
Role:
Fn::GetAtt:
- TestRole
- Arn
Events:
List:
Type: Api
Properties:
Path: /orders
Method: get
RestApiId: !Ref EcomApi
【问题讨论】:
标签: amazon-web-services aws-lambda microservices amazon-cloudformation aws-api-gateway