-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserverless-appv3-lambda-edge.yml
More file actions
152 lines (135 loc) · 4.96 KB
/
serverless-appv3-lambda-edge.yml
File metadata and controls
152 lines (135 loc) · 4.96 KB
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
## deprecated
service: ${env:STACK_NAME}-lambda-edge-${env:ROOTSTACK}
frameworkVersion: "2"
useDotenv: true
provider:
logRetentionInDays: 90 # Set the default RetentionInDays for a CloudWatch LogGroup
name: aws
runtime: nodejs14.x
region: us-east-1
stage: ${env:ENV}
deploymentBucket:
maxPreviousDeploymentArtifacts: 10 # On every deployment the framework prunes the bucket to remove artifacts older than this limit. The default is 5
blockPublicAccess: true
serverSideEncryption: AES256
tags:
StackName: ${env:STACK_NAME}
Env: ${env:ENV}
RootStack: ${env:ROOTSTACK}
stackTags: # Optional CF stack tags
StackName: ${env:STACK_NAME}
Env: ${env:ENV}
RootStack: ${env:ROOTSTACK}
tags: # Optional service wide function tags
StackName: ${env:STACK_NAME}
Env: ${env:ENV}
RootStack: ${env:ROOTSTACK}
resources:
Resources:
LambdaFunction:
DependsOn: [LambdaFunctionRole]
Type: "AWS::Lambda::Function"
Properties:
FunctionName: ${env:ROOTSTACK}-forwarder-${env:ENV}
Handler: index.handler
Runtime: nodejs14.x
Role: !GetAtt LambdaFunctionRole.Arn
MemorySize: 1024
Code:
ZipFile: |
'use strict';
const path = require("path");
exports.handler = async (evt, context, cb) => {
const { request } = evt.Records[0].cf;
console.log(`Original Uri: ${request.uri}`);
const uriParts = request.uri.split("/");
const locale = uriParts.length > 1 ? uriParts[1] : "";
const locales = ["en-US", "ja", "ms", "es"];
const lastPartUrl = uriParts[uriParts.length - 1];
// whitelisted version.json request
console.log('trailingURL::', lastPartUrl);
if (lastPartUrl.match(/^version\.json(?:\?t=\d+)?$/) !== null) {
return cb(null, request);
}
if (locale === "" || locale === "index.html" || !locales.includes(locale)) {
request.uri = "/en-US/index.html";
console.log('Go to default page and locale.');
return cb(null, request);
}
const fileExt = path.extname(lastPartUrl);
if (!fileExt) request.uri = `/${locale}/index.html`;
console.log(`New Uri: ${request.uri}`);
return cb(null, request);
};
LambdaFunctionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
- edgelambda.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
LambdaVersion:
Type: Custom::LatestLambdaVersion
Properties:
ServiceToken: !GetAtt PublishLambdaVersion.Arn
FunctionName: !Ref LambdaFunction
Nonce: ${env:Nonce}
PublishLambdaVersion:
Type: AWS::Lambda::Function
Properties:
Handler: index.handler
Runtime: nodejs14.x
Role: !GetAtt PublishLambdaVersionRole.Arn
Code:
ZipFile: |
const {Lambda} = require('aws-sdk')
const {send, SUCCESS, FAILED} = require('cfn-response')
const lambda = new Lambda()
exports.handler = (event, context) => {
const {RequestType, ResourceProperties: {FunctionName}} = event
if (RequestType == 'Delete') return send(event, context, SUCCESS)
lambda.publishVersion({FunctionName}, (err, {FunctionArn}) => {
err
? send(event, context, FAILED, err)
: send(event, context, SUCCESS, {FunctionArn})
})
}
PublishLambdaVersionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: PublishVersion
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: lambda:PublishVersion
Resource: "*"
Outputs:
LambdaFunctionArn:
Description: "Lambda Function ARN"
Value: !GetAtt LambdaFunction.Arn
Export:
Name: ${env:STACK_NAME}-LambdaFunctionArn-${env:ENV}
IndexLambdaVersionArnVersion:
Description: "Arn Version for Lambda function to associate unto CDN"
Value: !GetAtt LambdaVersion.FunctionArn
Export:
Name: ${env:STACK_NAME}-IndexLambdaVersionArnVersion-${env:ENV}