我正在使用AWS Lambda构建一个相当大的REST API。语言是node.js。有超过200个函数,而且还会有更多的函数出现。这些函数中的每一个都可以连接到RDS数据库,获取数据或保存数据。
我正在使用aws sam工具部署它。下面是template.yaml。请注意,我只发布了一个方法,因为从外部看,除了它们指向的端点之外,所有的方法看起来都是一样的。
WSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
xxx-restapi
Sample SAM Template for xxx-restapi
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 3
VpcConfig:
SecurityGroupIds:
- sg-041f2459dcd921e8e
SubnetIds:
- subnet-038xxx2d
- subnet-c4dxxxcb
- subnet-af5xxxc8
Resources:
GetAllAccountingTypesFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: xxx-restapi/
Handler: source/accounting-types/accountingtypes-getall.getallaccountingtypes
Runtime: nodejs14.x
Events:
GetAllAccountingTypesAPIEvent:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /accountingtypes/getall
Method: get
GetAccountingTypeByIDFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: xxx-restapi/
Handler: source/accounting-types/accountingtypes-byid.getbyid
Runtime: nodejs14.x
Events:
GetAllAccountingTypesAPIEvent:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /accountingtypes/getbyid
Method: get
LambdaRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: root
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- ec2:DescribeNetworkInterfaces
- ec2:CreateNetworkInterface
- ec2:DeleteNetworkInterface
- ec2:DescribeInstances
- ec2:AttachNetworkInterface
Resource: '*'
Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod stage for functions"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"我的所有方法都很好,它们按预期工作。然而,当我尝试部署时,这在CREATE_IN_PROGRESS上卡住了。但是,如果我减少函数的数量并尝试,它就会起作用。
我检查了aws cloud trail日志,发现了类似下面的内容。
ErrorCode: Client.RequestLimitExceeded
Resources: [{"resourceType":"AWS::EC2::SecurityGroup","resourceName":"sg-041f245xxxxd921e8e"},{"resourceType":"AWS::EC2::Subnet","resourceName":"subnet-af5xxxc8"}]和
ErrorCode: Client.DryRunOperation
Resources: [{"resourceType":"AWS::EC2::SecurityGroup","resourceName":"sg-041f2459xxxx1e8e"},{"resourceType":"AWS::EC2::Subnet","resourceName":"subnet-axxxx3c8"}]上面有多个类似的事件。我该如何解决这个问题呢?
发布于 2021-08-24 11:33:40
CloudFormation可能一次创建了太多的函数,因此达到了限制。您可能会将此归类为CloudFormation中的错误,所以我认为您应该将此报告给亚马逊网络服务或CloudFormation团队。
话虽如此,一种可能的解决方法是分步部署。在每次更新中添加一些Lambda函数。这将是一件非常麻烦的事情,但我看不到另一种方法。
您总是可以通过使用嵌套堆栈(然后可以逐个取消注释)来简化此过程。也许您甚至可以使用它绕过整个限制,这取决于CloudFormation如何处理它。但我对此不太确定。
如果您在单个堆栈中管理如此多的资源,那么您还面临着达到其他CloudFormation限制的危险(特别是因为SAM将多个资源抽象到单个类型之后)。因此,使用嵌套堆栈也可以防止您在(不久的)将来达到这些限制。
https://stackoverflow.com/questions/68899633
复制相似问题