AWS SAM -如何覆盖生成的资源



我试图用SAM构建一个REST API。我的YAML文件看起来像这样:

AWSTemplateFormatVersion: "2010-09-09"
Description: >-
example-rest-api
Transform:
- AWS::Serverless-2016-10-31
Resources:
allEquipmentsFunction:
Type: AWS::Serverless::Function
Properties:
Handler: src/handlers/myModel.getAll
Runtime: nodejs18.x
Architectures:
- x86_64
MemorySize: 128
Timeout: 100
Description: example description
Events:
ApiEvent:
Type: Api
Properties:
Path: /
Method: GET
saveEquipmentFunction:
Type: AWS::Serverless::Function
Properties:
Handler: src/handlers/myModel.save
Runtime: nodejs18.x
Architectures:
- x86_64
MemorySize: 128
Timeout: 100
Description: example description
Events:
Api:
Type: Api
Properties:
Path: /
Method: POST
ApplicationResourceGroup:
Type: AWS::ResourceGroups::Group
Properties:
Name:
Fn::Join:
- ''
- - ApplicationInsights-SAM-
- Ref: AWS::StackName
ResourceQuery:
Type: CLOUDFORMATION_STACK_1_0
ApplicationInsightsMonitoring:
Type: AWS::ApplicationInsights::Application
Properties:
ResourceGroupName:
Fn::Join:
- ''
- - ApplicationInsights-SAM-
- Ref: AWS::StackName
AutoConfigurationEnabled: 'true'
DependsOn: ApplicationResourceGroup
Outputs:
WebEndpoint:
Description: API Gateway endpoint URL for Prod stage
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/{Stage}"
根据我的理解,SAM在后台为我创建了一些资源。例如:
"ServerlessRestApi": {
"Type": "AWS::ApiGateway::RestApi"
...

如何重写这些生成的资源?我想添加API网关验证模型。为此,我需要重写后台生成的资源。AWS资源不够干净。如果我不能重写,我应该采取什么方法?

您不能"覆盖"。属性本身,但如果需要更大的控制,可以直接创建资源。

当您在函数上指定Event: ApiEvent时,SAM将为您创建一个AWS::ApiGateway::RestApi资源。正如文档所说,您可以引用RestApi的逻辑IDServerlessRestApi。这有助于将RestApi传递给另一个资源,但不能配置RestApi本身。

如果你需要更好地控制Api属性,你应该显式地创建一个AWS::Serverless::Api资源。根据需要进行配置。然后将Api引用作为RestApiId传递给函数的事件源配置。

SAM是CloudFormation的超集。如果SAM的抽象不适合您的用例,您也可以将AWS::ApiGateway::资源直接包含在SAM模板中。这给了你最大程度的控制。

最新更新