我正在使用 Cloudformation 和 aws-sam。在构建/部署应用程序时,有什么方法可以计算正在创建的资源数量吗?举个例子,我有一个遵循无服务器架构的 REST API 项目,它在
Resources
元素下只有 193 个函数。但这实际上产生了583个资源。我也知道了这一点,因为它达到了 aws 堆栈资源限制并且 AWS 显示了一条错误消息。
因此我想知道是否有一种方法可以知道正在创建的实际资源的数量。
以下是我制作的示例模板。
模板.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
aws-restapi
Sample SAM Template for aws-restapi
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 5
VpcConfig:
SecurityGroupIds:
- sg-041f2xxxd921e8e
SubnetIds:
- subnet-03xxxb2d
- subnet-c4dxxxcb
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: aws-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
RestApiId:
Ref: ApiGatewayApi
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: aws-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
RestApiId:
Ref: ApiGatewayApi
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:
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod stage for functions"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
您可以使用 sam CLI 通过
sam validate --debug --template yourtempatefile.yaml
转换模板。转换后的模板将与其他调试消息一起输出到 stderr。
这个 bash oneliner 可以帮助我计算资源。
$ sam validate --debug --template mytemplate.yaml 2>&1 | egrep '^ Properties:' | wc -l
据我所知,但可能对您有帮助的是这个SAM无服务器函数参考,它告诉您在未指定某些属性时将隐式生成哪些资源。
考虑到这一点,您可以计算实际创建的资源量。