-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yml
136 lines (128 loc) · 4.61 KB
/
template.yml
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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
# https://github.com/aws/serverless-application-model/blob/master/versions/2016-10-31.md
Description: >
Address API infrastructure. R53, Cert, LambdaFunction, DynamoTable, Scheduled EventRule.
Parameters:
AppName:
Type: String
Default: "AddressPrediction"
DomainName:
Type: String
Default: "address-api.infocruncher.com"
HostedZoneName:
Type: String
Default: "infocruncher.com."
FunctionName:
Type: String
Default: "AddressPredictionInferenceFunction"
TableName:
Type: String
Default: "AddressPredictionInferenceFunctionUsage"
Globals:
Function:
Timeout: 25
MemorySize: 1769 # 1769 = 1 vCPU
ReservedConcurrentExecutions: 20
Environment:
Variables:
TABLE_NAME: !Ref TableName
Resources:
InferenceApiCertificate:
Type: AWS::CertificateManager::Certificate
Properties:
DomainName: !Ref DomainName
ValidationMethod: DNS
InferenceApi:
Type: AWS::Serverless::Api
Properties:
Name: !Ref DomainName
StageName: Prod
TracingEnabled: true
# EndpointConfiguration: REGIONAL
Cors: # https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-api-corsconfiguration.html
AllowMethods: "'POST, GET'"
AllowHeaders: "'X-Forwarded-For'"
AllowOrigin: "'*'"
MaxAge: "'600'"
# AllowCredentials: true
Domain:
DomainName: !Ref DomainName
CertificateArn: !Ref InferenceApiCertificate
Route53:
HostedZoneName: !Ref HostedZoneName
Tags:
App: !Ref AppName
InferenceFunction:
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
# 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:
PackageType: Image
FunctionName: !Ref FunctionName
Description: !Join
- ""
- - "Function for "
- !Ref DomainName
Tags:
App: !Ref AppName
Policies:
DynamoDBWritePolicy:
TableName: !Ref InferenceTable
Events:
Inference:
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-api.html
# More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Type: Api
Properties:
RestApiId: !Ref InferenceApi
Path: /predict
Method: post
ScheduledEvent:
# https://github.com/aws/serverless-application-model/blob/master/versions/2016-10-31.md#schedule
# https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html
Type: Schedule
Properties:
Name: !Join
- ""
- - !Ref AppName
- "ScheduleEvent"
Schedule: cron(0/15 * * * ? *) # (0/15 * * * ? *) is every 15 mins.
Description: !Join
- ""
- - !Ref FunctionName
- "Function Keep Warm Schedule"
Enabled: True
Metadata:
Dockerfile: Dockerfile
DockerContext: ./app
DockerTag: python3.7-v1
InferenceTable:
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-simpletable.html
Type: AWS::Serverless::SimpleTable
Properties:
PrimaryKey:
Name: requestId
Type: String
TableName: !Ref TableName
Tags:
App: !Ref AppName
Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
InferenceApiUrl:
Description: "API Gateway endpoint URL for Prod stage for Inference function"
Value: !Sub "https://${InferenceApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/predict/"
InferenceApiId:
Description: 'API Gateway ARN for InferenceApi'
Value: !Ref InferenceApi
BasicAWSApiGatewayRootResourceId:
Description: 'API Gateway RootResourceId for InferenceApi'
Value: !GetAtt InferenceApi.RootResourceId
InferenceFunction:
Description: "Inference Lambda Function ARN"
Value: !GetAtt InferenceFunction.Arn
TableName:
Description: "Dynamo Table ARN"
Value: !GetAtt InferenceTable.Arn