Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edge routing function #14

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions buildspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ phases:
- echo "[profile account-role]\nrole_arn = ${ACCOUNT_ROLE}\ncredential_source = EcsContainer" >> ~/.aws/config
build:
commands:
- sam build -t edge.yaml
- sam deploy --config-env $ENV --s3-bucket $LAMBDA_BUCKET_NAME --s3-prefix $ENV-$STACK_NAME --stack-name $STACK_NAME-edge --capabilities CAPABILITY_IAM --profile account-role --region us-east-1 -t edge.yaml

- (cd api; npm install)
- (cd api; npm run build)
- sam build
Expand Down
38 changes: 38 additions & 0 deletions edge.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Edge Lambda
Resources:
EdgeFunction:
Type: AWS::Serverless::Function
Properties:
Architectures:
- x86_64
Timeout: 5
CodeUri: serve/
Runtime: nodejs18.x
Handler: index.handler
AutoPublishAlias: edge
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
- edgelambda.amazonaws.com
Action:
- sts:AssumeRole
Metadata:
BuildMethod: esbuild
BuildProperties:
EntryPoints:
- index.ts
Minify: true
Sourcemap: true
Target: "es2020"
Outputs:
EdgeLambdaVersion:
Description: ARN Of the Lambda at the latest version
Value: !Ref EdgeFunction.Version
Export:
Name: !Sub "${AWS::StackName}-EdgeLambdaVersion"
11 changes: 11 additions & 0 deletions serve/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { CloudFrontRequestHandler } from "aws-lambda";

const hasExtension = /^\/.*\..+$/;

export const handler: CloudFrontRequestHandler = async (event) => {
const { request } = event.Records[0].cf;
if (!request.uri.match(hasExtension)) {
request.uri = `${request.uri}.html`;
}
return request
};
35 changes: 35 additions & 0 deletions serve/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions serve/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "serve",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"devDependencies": {
"@types/aws-lambda": "^8.10.119",
"typescript": "^5.2.2"
}
}
10 changes: 10 additions & 0 deletions serve/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "ES2022",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
}
}
35 changes: 35 additions & 0 deletions template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ Parameters:
SubnetIds:
Type: List<AWS::EC2::Subnet::Id>
Description: VPC subnets
EdgeStackName:
Type: String
Description: Name of the edge stack containing the routing lambda in us-east-1
Conditions:
CreateARecords: !Not [ !Equals [ !Ref HostedZoneName, "" ] ]
IsDev: !Equals [ !Ref Environment, "dev" ]
Expand Down Expand Up @@ -158,6 +161,10 @@ Resources:
ViewerProtocolPolicy: redirect-to-https
ForwardedValues:
QueryString: True
LambdaFunctionAssociations:
- EventType: viewer-request
LambdaFunctionARN:
Fn::ImportValue: !Sub "EdgeStackName-EdgeLambdaVersion"
PriceClass: PriceClass_All
Logging:
IncludeCookies: false
Expand Down Expand Up @@ -244,6 +251,34 @@ Resources:
Sourcemap: true
EntryPoints:
- src/api/apiHandler.ts
ServeFunction:
Type: AWS::Serverless::Function
Properties:
Architectures:
- x86_64
Timeout: 5
CodeUri: serve/
Runtime: nodejs18.x
Handler: index.handler
AutoPublishAlias: edge
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
- edgelambda.amazonaws.com
Action:
- sts:AssumeRole
Metadata:
BuildMethod: esbuild
BuildProperties:
EntryPoints:
- index.ts
Minify: true
Sourcemap: true
Target: "es2020"
TranscriptionJobStartFunction:
Type: AWS::Serverless::Function
Properties:
Expand Down