-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtemplate.yaml
239 lines (212 loc) · 7.58 KB
/
template.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
AWS Lambda SpringBoot 3.4 with Spring Cloud Function AWS Adapter
Sample SAM Template
Parameters:
Stage:
Type: String
Default: prod
Globals:
Function:
Handler: org.springframework.cloud.function.adapter.aws.FunctionInvoker::handleRequest
CodeUri: target/aws-lambda-spring-boot-3.4-spring-cloud-function-aws-1.0.0-SNAPSHOT.jar
Runtime: java21
SnapStart:
ApplyOn: PublishedVersions
Timeout: 30
MemorySize: 1024
Environment:
Variables:
JAVA_TOOL_OPTIONS: "-XX:+TieredCompilation -XX:TieredStopAtLevel=1"
MAIN_CLASS: software.amazonaws.Application
PRODUCT_TABLE_NAME: !Ref ProductsTable
Resources:
MyApi:
Type: AWS::Serverless::Api
DependsOn: MyApiCWLRoleArn
Properties:
StageName: !Ref Stage
Name: AWSLambdaSpringBoot34SpringCloudFunctionAWSAPI
Description: Product API using AWS Lambda with Spring Boot 3.4 and Spring Cloud Function AWS Adapter
AccessLogSetting:
DestinationArn: !Sub ${MyApiLogGroup.Arn} # This Log Group is already created within our SAM Template
Format: "{'integrationLatency':'$context.integrationLatency', 'latency':'$context.responseLatency', 'requestId':'$context.requestId', 'ip': '$context.identity.sourceIp', 'caller':'$context.identity.caller', 'user':'$context.identity.user','requestTime':'$context.requestTime', 'xrayTraceId':'$context.xrayTraceId', 'wafResponseCode':'$context.wafResponseCode', 'httpMethod':'$context.httpMethod','resourcePath':'$context.resourcePath', 'status':'$context.status','protocol':'$context.protocol', 'responseLength':'$context.responseLength' }"
#MethodSettings:
# - MetricsEnabled: True
# ResourcePath: '/*'
# HttpMethod: '*'
Auth:
ApiKeyRequired: true # sets for all methods
MyApiCWLRoleArn:
Type: AWS::ApiGateway::Account
Properties:
CloudWatchRoleArn: !GetAtt CloudWatchRole.Arn
# IAM Role for API GW + CWL
CloudWatchRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
Action: 'sts:AssumeRole'
Effect: Allow
Principal:
Service: apigateway.amazonaws.com
Path: /
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs'
MyApiLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/apigateway/${MyApi}"
RetentionInDays: 7
MyApiUsagePlan:
Type: AWS::ApiGateway::UsagePlan
DependsOn:
- MyApiStage
Properties:
ApiStages:
- ApiId: !Ref MyApi
Stage: !Ref Stage
UsagePlanName: AWSLambdaSpringBoot34SpringCloudFunctionAWSAPIUsagePlan
Description: Usage plan for AWSLambdaSpringBoot34SpringCloudFunctionAWSAPIUsagePlan
Quota:
Limit: 2000000
Period: DAY
Throttle:
RateLimit: 10000
BurstLimit: 50000
MyApiKey:
Type: AWS::ApiGateway::ApiKey
DependsOn:
- MyApiStage
Properties:
Name: "AWSLambdaSpringBoot34SpringCloudFunctionAWSApiKey"
Description: "AWSLambdaSpringBoot34SpringCloudFunctionAWS API Key"
Enabled: true
GenerateDistinctId: false
Value: a6ZbcDefQW12BN56WEJ34
StageKeys:
- RestApiId: !Ref MyApi
StageName: !Ref Stage
MyApiUsagePlanKey:
Type: "AWS::ApiGateway::UsagePlanKey"
DependsOn:
- MyApi
Properties:
KeyId: !Ref MyApiKey
KeyType: API_KEY
UsagePlanId: !Ref MyApiUsagePlan
GetProductByIdFunction:
Type: AWS::Serverless::Function
Properties:
Environment:
Variables:
SPRING_CLOUD_FUNCTION_DEFINITION: getProductByIdHandler
FunctionName: GetProductByIdWithSpringBoot34WithSpringCloudFunctionAWS
AutoPublishAlias: liveVersion
Policies:
- DynamoDBReadPolicy:
TableName: !Ref ProductsTable
Events:
GetRequestById:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /products/{id}
Method: get
GetProductByIdFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${GetProductByIdFunction}"
RetentionInDays: 7
GetProductByIdWithDynamoDBRequestPrimingFunction:
Type: AWS::Serverless::Function
Properties:
Environment:
Variables:
SPRING_CLOUD_FUNCTION_DEFINITION: getProductByIdWithDynamoDBRequestPrimingHandler
FunctionName: GetProductByIdWithSpringBoot34WithSpringCloudFunctionAWSDPriming
AutoPublishAlias: liveVersion
Policies:
- DynamoDBReadPolicy:
TableName: !Ref ProductsTable
Events:
GetRequestById:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /productsWithDynamoDBPriming/{id}
Method: get
GetProductByIdWithDynamoDBRequestPrimingFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${GetProductByIdWithDynamoDBRequestPrimingFunction}"
RetentionInDays: 7
GetProductByIdWithWebRequestPrimingFunction:
Type: AWS::Serverless::Function
Properties:
Environment:
Variables:
SPRING_CLOUD_FUNCTION_DEFINITION: getProductByIdWithWebRequestPrimingHandler
FunctionName: GetProductByIdWithSpringBoot34WithSpringCloudFunctionAWSWPriming
AutoPublishAlias: liveVersion
Policies:
- DynamoDBReadPolicy:
TableName: !Ref ProductsTable
Events:
GetRequestById:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /productsWithWebRequestPriming/{id}
Method: get
GetProductByIdWithWebRequestPrimingFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${GetProductByIdWithWebRequestPrimingFunction}"
RetentionInDays: 7
PutProductFunction:
Type: AWS::Serverless::Function
Properties:
Environment:
Variables:
SPRING_CLOUD_FUNCTION_DEFINITION: createProductHandler
FunctionName: PutProductWithSpringBoot34WithSpringCloudFunctionAWS
AutoPublishAlias: liveVersion
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref ProductsTable
Events:
PutRequest:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /products
Method: put
PutProductFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${PutProductFunction}"
RetentionInDays: 7
ProductsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: "AWSLambdaSpringBoot34WithSpringCloudFunctionAWSProductsTable"
PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: true
AttributeDefinitions:
- AttributeName: 'PK'
AttributeType: 'S'
KeySchema:
- AttributeName: 'PK'
KeyType: 'HASH'
BillingMode: PAY_PER_REQUEST
#ProvisionedThroughput:
#ReadCapacityUnits: "1"
#WriteCapacityUnits: "1"
Outputs:
AWSLambdaSpringBoot34WithSpringCloudFunctionAWSApiEndpoint:
Description: "API Gateway endpoint URL for Prod stage for AWS Lambda Spring Boot 3.4 with Spring Cloud Function AWS Adapter"
Value: !Sub "https://${MyApi}.execute-api.${AWS::Region}.amazonaws.com/prod/products"