如何使用 AWS 无服务器应用程序模型部署具有 API 网关 REST 接口的 lambda Java 函数(包括缓存 POST 方法)?
答案1
ExampleFunction:
# More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Type: AWS::Serverless::Function
Properties:
CodeUri: path/to/libs/example.jar
Handler: com.example.Example::handleRequest
VpcConfig:
SecurityGroupIds:
- sg-abc
SubnetIds:
- subnet-1234
- subnet-1235
- subnet-1236
Runtime: java11
MemorySize: 1024
Events:
Example:
# More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Type: Api
Properties:
Path: /example
Method: post
RestApiId:
Ref: ExampleApi
ExampleApi:
Type: AWS::Serverless::Api
Properties:
Description: Example Private API with cache and logging disabled
StageName: prod
# Cache size is in GB
CacheClusterSize: '0.5'
CacheClusterEnabled: true
# First method is for default / GET, second is for POST
MethodSettings:
- CachingEnabled: true
CacheDataEncrypted: true
CacheTtlInSeconds: 3600
LoggingLevel: 'Off'
HttpMethod: '*'
ResourcePath: '/*'
MetricsEnabled: true
- CachingEnabled: true
CacheDataEncrypted: true
CacheTtlInSeconds: 3600
LoggingLevel: 'Off'
HttpMethod: 'POST'
# For encoding see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apitgateway-stage-methodsetting.html#cfn-apigateway-stage-methodsetting-resourcepath
ResourcePath: '/~1example'
MetricsEnabled: true
ThrottlingRateLimit: 1000
ThrottlingBurstLimit: 500
Auth:
ResourcePolicy:
CustomStatements: [
{
"Effect": "Allow",
"Principal": "*",
"Action": "execute-api:Invoke",
"Resource": "execute-api:/*"
}
]
EndpointConfiguration:
Type: PRIVATE