-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
148 lines (135 loc) · 5.17 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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
resolution-service
SAM Template for resolution-service
Globals:
Function:
Timeout: 30
###################################################################################################
## Template Parameters ##
###################################################################################################
Parameters:
TargetTableName:
Type: String
Image404:
Type: String
Region:
Type: String
###################################################################################################
## Template Resources ##
###################################################################################################
Resources:
## Lambda Function
NameResolutionFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: apps/
Handler: app.lambda_handler
Runtime: python3.7
Description: An AWS Lambda function with API Gateway that receives a short URL, finds a record in the DynamoDB table, redirects user to the long URL.
MemorySize: 128
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref TargetTableName
Environment:
Variables:
DEBUG: 1
TargetTable: !Ref TargetTableName
Region: !Ref Region
Image404: !Ref Image404
Events:
ResolutionServiceApi:
Type: Api
Properties:
RestApiId: !Ref ResolutionServiceApiGateway
Path: /{arklabel}/53696/{shortid}
Method: GET
## API Gateway
ResolutionServiceApiGateway:
Type: AWS::Serverless::Api
Properties:
Name: !Join [ "-", [ "Resolution Service API", !Ref TargetTableName] ]
StageName: Prod
EndpointConfiguration: Edge
TracingEnabled: true
DefinitionBody:
openapi: "3.0.1"
info:
title: "Resolution Service API"
version: "1.0.3"
x-amazon-apigateway-request-validators:
all:
validateRequestBody: true
validateRequestParameters: true
params:
validateRequestBody: false
validateRequestParameters: true
body:
validateRequestBody: true
validateRequestParameters: false
paths:
/{arklabel}/53696/{shortid}:
## URL redirector
get:
summary: Get a url by ID and redirect
responses:
"301":
description: "301 redirect"
headers:
Location:
type: "string"
## API Gateway Integration
x-amazon-apigateway-integration:
uri:
Fn::Sub: 'arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${NameResolutionFunction.Arn}/invocations' #NameResolutionFunction.Arn
httpMethod: "POST"
requestTemplates:
application/json: "{\"arklabel\": \"ark:\", \"short_id\": \"$input.params('shortid')\"}"
passthroughBehavior: "when_no_templates"
responses:
"":
statusCode: "301"
responseParameters:
method.response.header.Location: "integration.response.body.location"
type: aws
## CloudFront distribution
CloudFrontDistro:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Comment: Name Resolution CDN
DefaultCacheBehavior:
AllowedMethods: ["GET", "HEAD"]
CachedMethods: ["GET", "HEAD"]
Compress: true
DefaultTTL: 0
ForwardedValues:
Headers:
- Origin
QueryString: false
TargetOriginId: "ResolutionServiceAPIGW"
ViewerProtocolPolicy: redirect-to-https
Enabled: true
Origins:
- CustomOriginConfig:
OriginProtocolPolicy: https-only
DomainName: !Sub ${ResolutionServiceApiGateway}.execute-api.${AWS::Region}.amazonaws.com
Id: "ResolutionServiceAPIGW"
OriginPath: /Prod
Outputs:
NameResolutionFunction:
Description: "Name Resolution Lambda Function ARN"
Value: !GetAtt NameResolutionFunction.Arn
NameResolutionFunctionIamRole:
Description: "Implicit IAM Role created for Name Resolution function"
Value: !GetAtt NameResolutionFunctionRole.Arn
ResolutionServiceApiGateway:
Description: 'API Gateway endpoint URL for Prod stage for Name Resolution function'
Value: !Sub 'https://${ResolutionServiceApiGateway}.execute-api.${AWS::Region}.amazonaws.com/Prod/{arklabel}/53696/{shortid}'
ResolutionServiceApiGatewayRestApiId:
Description: 'API Gateway Rest API Id for Name Resolution API Gateway'
Value: !Ref ResolutionServiceApiGateway
ResolutionServiceApiGatewayRootResourceId:
Description: 'API Gateway Root Resource Id for Name Resolution API Gateway'
Value: !GetAtt ResolutionServiceApiGateway.RootResourceId