问题描述
我正在学习SAM Cli,并正在使用lambda函数的现有模板。我不确定100%是否已使用此方法创建了API网关。但是,如果不是这样,我想通过添加一个api网关(可能带有api密钥的身份验证机制)来扩展它。另外,我还创建了具有私有/公共子网的VPC。我如何/在哪里可以在此模板中定义它们。任何代码示例都将有所帮助。
AWstemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Sample SAM Template
Globals:
Function:
Timeout: 3
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.8
Events:
HelloWorld:
Type: Api # More info about API Event Source:
Properties:
Path: /hello
Method: get
Outputs:
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
HelloWorldFunction:
Description: "Hello World Lambda Function ARN"
Value: !GetAtt HelloWorldFunction.Arn
HelloWorldFunctionIamRole:
Description: "Implicit IAM Role created for Hello World function"
Value: !GetAtt HelloWorldFunctionRole.Arn
解决方法
您尚未提供有关API geteway的任何模板代码。因此,通常,如果要使用 API密钥,则必须在模板中使用三个组件:
- AWS::ApiGateway::UsagePlan为api创建使用计划,
- AWS::ApiGateway::ApiKey创建 API密钥
- AWS::ApiGateway::UsagePlanKey将密钥与计划关联。