Skip to content

Commit

Permalink
Migrate CfUrlRewriter Lambda from JavaScript to TypeScript (#1660)
Browse files Browse the repository at this point in the history
* Migrate CfUrlRewriter Lambda from JavaScript to TypeScript

Signed-off-by: Tianle Huang <[email protected]>
  • Loading branch information
tianleh committed Feb 26, 2022
1 parent c490e19 commit 6c5887e
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 16 deletions.
2 changes: 2 additions & 0 deletions deployment/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ node_modules
# CDK asset staging directory
.cdk.staging
cdk.out

package-lock.json
14 changes: 14 additions & 0 deletions deployment/lambdas/cf-url-rewriter/cf-url-rewriter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { CloudFrontRequest, CloudFrontRequestCallback, CloudFrontRequestEvent, Context } from 'aws-lambda';

export const handle = (request: CloudFrontRequest) => {
// Incoming URLs from ci.opensearch.org will have a '/ci/123/' prefix, remove the prefix path from requests into S3.
request.uri = request.uri.replace(/^\/ci\/...\//, '\/');
}

export async function handler(event: CloudFrontRequestEvent, context: Context, callback: CloudFrontRequestCallback) {
const request = event.Records[0].cf.request;

handle(request);

callback(null, request);
}
63 changes: 63 additions & 0 deletions deployment/lambdas/cf-url-rewriter/package-lock.json

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

18 changes: 18 additions & 0 deletions deployment/lambdas/cf-url-rewriter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "cf-url-rewriter",
"version": "1.0.0",
"description": "",
"main": "cf-url-rewriter.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/aws-lambda": "^8.10.68",
"@types/node": "^14.14.19",
"typescript": "^4.1.3"
}
}

22 changes: 11 additions & 11 deletions deployment/lib/artifacts-public-access.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {
CloudFrontAllowedMethods, CloudFrontWebDistribution, LambdaEdgeEventType, OriginAccessIdentity,
CloudFrontAllowedMethods, CloudFrontWebDistribution, LambdaEdgeEventType, OriginAccessIdentity
} from '@aws-cdk/aws-cloudfront';
import { CanonicalUserPrincipal, PolicyStatement } from '@aws-cdk/aws-iam';
import { Code, Function, Runtime } from '@aws-cdk/aws-lambda';
import { Architecture, Runtime } from '@aws-cdk/aws-lambda';
import { NodejsFunction } from '@aws-cdk/aws-lambda-nodejs';
import { IBucket } from '@aws-cdk/aws-s3';
import { CfnOutput } from '@aws-cdk/core';
import { BuildArtifactStack } from './build-artifact-stack';
Expand All @@ -19,16 +20,15 @@ export class ArtifactsPublicAccess {
principals: [new CanonicalUserPrincipal(originAccessIdentity.cloudFrontOriginAccessIdentityS3CanonicalUserId)],
}));

// Incoming URLs from ci.opensearch.org will have a '/ci/123/' prefix, remove the prefix path from requests into S3.
const urlRewriter = new Function(stack, 'CfUrlRewriter', {
code: Code.fromInline(`
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
request.uri = request.uri.replace(/^\\/ci\\/...\\//, '\\/')
callback(null, request);
};`),
handler: 'index.handler',
const urlRewriter = new NodejsFunction(stack, 'CfUrlRewriter', {
runtime: Runtime.NODEJS_14_X,
entry: `${__dirname}/../lambdas/cf-url-rewriter/cf-url-rewriter.ts`,
handler: 'handler',
memorySize: 128,
architecture: Architecture.X86_64,
bundling: {
minify: true
}
});

const distro = new CloudFrontWebDistribution(stack, 'CloudFrontBuildBucket', {
Expand Down
13 changes: 8 additions & 5 deletions deployment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"devDependencies": {
"@aws-cdk/assert": "^1.123.0",
"@types/aws-lambda": "^8.10.92",
"@types/jest": "^26.0.10",
"@types/node": "10.17.27",
"aws-cdk": "^1.123.0",
Expand All @@ -22,13 +23,15 @@
"typescript": "~3.9.7"
},
"dependencies": {
"@aws-cdk/aws-cloudfront": "^1.123.0",
"@aws-cdk/aws-iam": "^1.123.0",
"@aws-cdk/aws-lambda": "^1.123.0",
"@aws-cdk/aws-s3": "^1.123.0",
"@aws-cdk/core": "^1.123.0",
"@aws-cdk/aws-cloudfront": "1.145.0",
"@aws-cdk/aws-iam": "1.145.0",
"@aws-cdk/aws-lambda": "1.145.0",
"@aws-cdk/aws-lambda-nodejs": "1.145.0",
"@aws-cdk/aws-s3": "1.145.0",
"@aws-cdk/core": "1.145.0",
"@typescript-eslint/eslint-plugin": "^4.31.1",
"@typescript-eslint/parser": "^4.31.1",
"esbuild": "^0.14.23",
"eslint": "^7.32.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.24.2",
Expand Down
2 changes: 2 additions & 0 deletions deployment/test/build-artifact-stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test('Fresh BuildArtifact Stack', () => {
expect(stack).to(countResources('AWS::IAM::Role', 4));
expect(stack).to(countResources('AWS::CloudFront::CloudFrontOriginAccessIdentity', 1));
expect(stack).to(countResources('AWS::CloudFront::Distribution', 1));
expect(stack).to(countResources('AWS::Lambda::Function', 1));
expect(stack).to(haveOutput({ outputName: 'BuildDistributionDomainName' }));
expect(stack).to(not(haveOutput({ outputName: 'OriginAccessIdentityS3Identifier' })));
});
Expand All @@ -30,6 +31,7 @@ test('Existing BuildArtifact Stack', () => {
expect(stack).to(countResources('AWS::IAM::Role', 1));
expect(stack).to(countResources('AWS::CloudFront::CloudFrontOriginAccessIdentity', 1));
expect(stack).to(countResources('AWS::CloudFront::Distribution', 1));
expect(stack).to(countResources('AWS::Lambda::Function', 1));
expect(stack).to(haveOutput({ outputName: 'BuildDistributionDomainName' }));
expect(stack).to(haveOutput({ outputName: 'OriginAccessIdentityS3Identifier' }));
});
20 changes: 20 additions & 0 deletions deployment/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { CloudFrontRequest } from 'aws-lambda';
import { handle } from "../../../lambdas/cf-url-rewriter/cf-url-rewriter";

test('Lmabda handle uri with ci string', () => {

let request = { uri: '/ci/dbc/bundle-build-dashboards/1.2.0/428/linux/x64/' } as CloudFrontRequest;

handle(request);

expect(request.uri).toBe('/bundle-build-dashboards/1.2.0/428/linux/x64/');
});

test('Lmabda handle uri without ci string', () => {

let request = { uri: '/bundle-build-dashboards/1.2.0/428/linux/x64/' } as CloudFrontRequest;

handle(request);

expect(request.uri).toBe('/bundle-build-dashboards/1.2.0/428/linux/x64/');
});

0 comments on commit 6c5887e

Please sign in to comment.