-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.yaml
178 lines (169 loc) · 5.09 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
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
java-lambda-s3
Sample SAM Template for java-lambda-s3
Globals:
Function:
Timeout: 20
Parameters:
AppEnv:
Type: String
Default: stage
Description: Application environment
NotificationSender:
Type: String
Default: vanducld@gmail.com
Description: Sender email for SES
NotificationReceiver:
Type: String
Default: vanducld@icloud.com
Description: Receiver email for SES
UploadFileBucketName:
Type: String
Default: receipt-listing-2022-04-18-17-48-ap-southeast-1
Description: S3 bucket that's used for the Lambda event notification
GoogleCloudVisionKey:
Type: AWS::SSM::Parameter::Value<String>
Default: /lambda/upload-receipt/google-vision-api-key
Description: Bearer google vision api key
Resources:
GatewayAPI:
Type: AWS::Serverless::Api
Properties:
StageName: !Ref AppEnv
Cors:
AllowMethods: "'POST,OPTIONS'"
AllowHeaders: "'*'"
AllowOrigin: "'*'"
AllowCredentials: false
MaxAge: "'86400'"
BucketToSNSPermission:
Type: AWS::SNS::TopicPolicy
Properties:
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: s3.amazonaws.com
Action:
- SNS:Publish
Resource: !Ref S3PutNewObjectEventTopic
Condition:
StringEquals:
aws:SourceArn: !Sub "arn:aws:s3:::${UploadFileBucketName}"
Topics:
- !Ref S3PutNewObjectEventTopic
UploadBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref UploadFileBucketName
CorsConfiguration:
CorsRules:
- AllowedHeaders:
- "*"
AllowedMethods:
- GET
- PUT
AllowedOrigins:
- "*"
ExposedHeaders:
- Date
MaxAge: 3600
UploadFileFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: UploadFileFunction
Role: !GetAtt LambdaIAMRole.Arn
Handler: com.github.vanduc1102.uploadfile.App::handleRequest
Runtime: java11
MemorySize: 512
Environment:
Variables:
UPLOAD_BUCKET: !Ref UploadFileBucketName
GOOGLE_APPLICATION_CREDENTIALS: !Ref GoogleCloudVisionKey
JAVA_TOOL_OPTIONS: -XX:+TieredCompilation -XX:TieredStopAtLevel=1
Events:
UploadFile:
Type: Api
Properties:
Path: /upload-file
Method: post
RestApiId: !Ref GatewayAPI
SendNotificationFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: SendNotificationFunction
Role: !GetAtt LambdaIAMRole.Arn
Handler: com.github.vanduc1102.sendnofitication.App::handleRequest
Runtime: java11
MemorySize: 512
Environment:
Variables:
NOTIFICATION_SENDER: !Ref NotificationSender
NOTIFICATION_RECEIVER: !Ref NotificationReceiver
UPLOAD_BUCKET: !Ref UploadFileBucketName
GOOGLE_APPLICATION_CREDENTIALS: !Ref GoogleCloudVisionKey
JAVA_TOOL_OPTIONS: -XX:+TieredCompilation -XX:TieredStopAtLevel=1
Events:
PutUploadFileEvent:
Type: S3
Properties:
Bucket: !Ref UploadBucket
Events: s3:ObjectCreated:Put
S3PutNewObjectEventTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: offline-receipt-s3-put-new-object-event-to-email
Tags:
-
Key: Project
Value: Offline Receipt
S3PutNewObjectEventSubscription:
Type: AWS::SNS::Subscription
Properties:
Endpoint: !Ref NotificationReceiver
Protocol: email
TopicArn: !Ref S3PutNewObjectEventTopic
LambdaIAMRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- "sts:AssumeRole"
Path: /
Policies:
- PolicyName: root
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- "s3:GetObject"
- "s3:PutObject"
Resource: !Sub "arn:aws:s3:::${UploadFileBucketName}/*"
- Effect: Allow
Action:
- "ses:SendEmail"
- "ses:SendRawEmail"
Resource: "*"
- Effect: Allow
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource: "arn:aws:logs:*:*:*"
Outputs:
UploadFileApi:
Description: "API Gateway endpoint URL for UploadFileFunction"
Value: !Sub "https://${GatewayAPI}.execute-api.${AWS::Region}.amazonaws.com/${AppEnv}/upload-file/"
UploadFileFunction:
Description: "UploadFileFunction Lambda Function ARN"
Value: !GetAtt UploadFileFunction.Arn