-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yml
150 lines (150 loc) · 4.73 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
petit-lambda
Petit URL Shortener backend for Lambda and API-Gateway
Parameters:
ServiceBaseUrl:
Description: Please provide the base url for your URL shortener
Type: String
AllowedPattern: ".+"
NotFoundDestination:
Description: >-
Please provide a location to which users will be routed if the shortcode
does not resolve to an address.
Type: String
AllowedPattern: ".+"
Stage:
Description: >-
Please provide the name of the stage to be deployed in API Gateway. This
could be a version "v1, v1.1, v2" or an environment "test, stage, prod"
this will be appended to the end of each related resource's name as well.
Type: String
Default: "test"
CrossOriginDomain:
Description: >-
Certain API Methods are allowed cross origin access, you can specify domain(s)
that are allowed. This will default to '*' which will allow any domain.
Type: String
Default: "*"
ApiBaseUrl:
Description: >-
Please provide the base url for the API. This may be the URL generated by
API Gateway for the specified stage, or may be a custom domain. If this is
not set, or is inaccurate, the "self" links returned in the payload will
be incorrect.
Type: String
Default: "https://APINAME.execute-api.REGION.amazonaws.com/STAGE/"
ArtifactsBucket:
Description: >-
You must provide the bucket name where the petit-api.yml file has been
uploaded.
Type: String
AllowedPattern: ".+"
Resources:
PetitDatabase:
Type: AWS::DynamoDB::Table
Properties:
Tags:
-
Key: "ApplicationName"
Value: "Petit URL Shortener"
TableName: !Sub "Petit-${Stage}"
AttributeDefinitions:
-
AttributeName: "shortcode"
AttributeType: "S"
-
AttributeName: "destination"
AttributeType: "S"
KeySchema:
-
AttributeName: "shortcode"
KeyType: "HASH"
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
GlobalSecondaryIndexes:
-
IndexName: "destinationIndex"
KeySchema:
-
AttributeName: "destination"
KeyType: "HASH"
Projection:
ProjectionType: "ALL"
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
PetitRedirector:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub "PetitRedirectorFunction-${Stage}"
Description: >-
URL shortener function that simply redirects traffic to destinations
stored in dynamodb.
(Note: You must connect this to your load balancer manually. SAM does
not currently provide a way for an application load balancer to be
specified as an event source.)
Tags:
ApplicationName: "Petit URL Shortener"
CodeUri: "./redirector-app"
Handler: index.handler
Runtime: nodejs14.x
MemorySize: 128
Timeout: 3
Policies:
-
DynamoDBReadPolicy:
TableName: !Ref PetitDatabase
Environment:
Variables:
NOT_FOUND_DESTINATION: !Ref NotFoundDestination
TABLE_NAME: !Ref PetitDatabase
PetitAPIFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub "PetitAPIFunction-${Stage}"
Description: >-
URL shortener fucntion that provides an API for crud operations
Tags:
ApplicationName: "Petit URL Shortener"
CodeUri: "./"
Handler: lambda.handler
Runtime: ruby2.7
Environment:
Variables:
DB_TABLE_NAME: !Ref PetitDatabase
SERVICE_BASE_URL: !Ref ServiceBaseUrl
API_BASE_URL: !Ref ApiBaseUrl
CROSS_ORIGIN_DOMAIN: !Ref CrossOriginDomain
APP_ENV: production
MemorySize: 128
Timeout: 30
Policies:
-
DynamoDBCrudPolicy:
TableName: !Ref PetitDatabase
PetitAPI:
Type: AWS::Serverless::Api
Properties:
Name: PetitAPI
StageName: !Sub "${Stage}"
DefinitionBody:
Fn::Transform:
Name: AWS::Include
Parameters:
Location: !Sub s3://${ArtifactsBucket}/petit-api.yml
EndpointConfiguration: "EDGE"
ConfigLambdaPermission:
Type: "AWS::Lambda::Permission"
DependsOn:
- PetitAPIFunction
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref PetitAPIFunction
Principal: apigateway.amazonaws.com
Outputs:
APIEndpoint:
Description: API Endpoint URL
Value: !Sub "https://${PetitAPI}.execute-api.${AWS::Region}.amazonaws.com/${Stage}/"