【发布时间】:2020-12-11 07:11:27
【问题描述】:
我目前有一个堆栈部署到 AWS,它有很多 REST 端点(Lambda 函数)、一些用于维护操作的其他 Lambda,以及一个 DynamoDB、Cognito 用户池、Elastic Search Domain、IAM 角色等。所有这些都部署了无服务器框架,使用 serverless.yml 定义堆栈。
为了避免 200 个资源的限制(并获得更好的结构),我正在尝试将当前堆栈拆分为多个堆栈。
计划是为所有具有持久数据的资源(DynamoDB、Elastic Search、Cognito、IAM 等)保留当前堆栈,然后为 lambda 函数定义新堆栈。一个用于维护功能,另外几个堆栈用于不同类型的 HTTP 通过 API Gateway 调用的功能。
现在问题来了:我已经注释掉了 serverless.yml 的整个 functions: 部分。
我有一个包含资源的部分,如下所示:
resources:
- ${file(resources/dynamoDb.yml)}
- ${file(resources/cognito.yml)}
- ${file(resources/iam.yml)}
- ${file(resources/elasticsearch.yml)}
当我现在尝试部署堆栈时(所有函数均已注释掉),我收到此错误:
Error: The CloudFormation template is invalid: Template format error: Unresolved resource dependencies [ApiGatewayRestApi] in the Resources block of the template
我收到此错误的原因可能是因为我在resources/iam.yml 中引用了ApiGatewayRestApi:
GetVehicleByLicensePlatePolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
ManagedPolicyName: GetVehicleByLicensePlate
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: execute-api:Invoke
Resource:
Fn::Join:
- ""
- - "arn:aws:execute-api"
- ":"
- Ref: AWS::Region
- ":"
- Ref: AWS::AccountId
- ":"
- Ref: ApiGatewayRestApi
- "/*/GET/vehicles/licenseplate/*"
我了解到,当我删除由 HTTP 触发的所有函数时,ApiGatewayRestApi 引用无法解析,因为此堆栈中没有部署 API 网关。
我将在其他几个堆栈中包含 HTTP lambda 函数,但这些堆栈将依赖于这个堆栈。 (而且我当然不想要循环依赖。)
那么如何让我的“主”堆栈能够引用子堆栈使用的 API 网关?
解决此问题的常用/最佳实践方法是什么?
【问题讨论】:
-
作为一种解决方法,只需创建一个虚拟 lambda/http 代理集成
标签: aws-lambda amazon-cloudformation aws-api-gateway serverless-framework aws-serverless