Add AWS Lambda CloudFront Event origin fields. (#28675)

* Add AWS Lambda CloudFront Event origin fields.

* Add tests for the different kinds of Lambda CloudFrontRequests.
This commit is contained in:
Oliver Hookins
2018-09-13 13:11:40 -07:00
committed by Ryan Cavanaugh
parent 46e3564b14
commit 24f4e6d721
2 changed files with 117 additions and 2 deletions
+89 -2
View File
@@ -581,7 +581,7 @@ const CodePipelineEvent: AWSLambda.CodePipelineEvent = {
CodePipelineEvent["CodePipeline.job"].data.encryptionKey = { type: 'KMS', id: 'key' };
/* CloudFront events, see http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html */
const CloudFrontRequestEvent: AWSLambda.CloudFrontRequestEvent = {
const CloudFrontRequestWithCustomOriginEvent: AWSLambda.CloudFrontRequestEvent = {
Records: [
{
cf: {
@@ -630,7 +630,44 @@ const CloudFrontRequestEvent: AWSLambda.CloudFrontRequestEvent = {
"TLSv1",
"TLSv1.1"
]
},
}
}
}
}
}
]
};
const CloudFrontRequestWithS3OriginEvent: AWSLambda.CloudFrontRequestEvent = {
Records: [
{
cf: {
config: {
distributionDomainName: "d123.cloudfront.net",
distributionId: "EDFDVBD6EXAMPLE",
eventType: "viewer-request",
requestId: "MRVMF7KydIvxMWfJIglgwHQwZsbG2IhRJ07sn9AkKUFSHS9EXAMPLE=="
},
request: {
clientIp: "2001:0db8:85a3:0:0:8a2e:0370:7334",
method: "GET",
uri: "/picture.jpg",
querystring: "size=large",
headers: {
host: [
{
key: "Host",
value: "d111111abcdef8.cloudfront.net"
}
],
"user-agent": [
{
key: "User-Agent",
value: "curl/7.51.0"
}
]
},
origin: {
s3: {
authMethod: "origin-access-identity",
customHeaders: {
@@ -812,6 +849,56 @@ apiGtwProxyHandler = proxyHandler;
const codePipelineHandler: AWSLambda.CodePipelineHandler = (event: AWSLambda.CodePipelineEvent, context: AWSLambda.Context, cb: AWSLambda.Callback<void>) => {};
const cloudFrontRequestHandler: AWSLambda.CloudFrontRequestHandler = (event: AWSLambda.CloudFrontRequestEvent, context: AWSLambda.Context, cb: AWSLambda.CloudFrontRequestCallback) => {
event = CloudFrontRequestWithCustomOriginEvent;
// $ExpectType CloudFrontRequestEvent
event;
let request = event.Records[0].cf.request;
let s3Origin: AWSLambda.CloudFrontS3Origin = {
authMethod: 'none',
customHeaders: {},
domainName: 'example.com',
path: '/',
region: 'us-east-1'
};
if (request.origin && request.origin.custom) {
request.origin.custom.domainName;
request.origin.custom.domainName = "example2.com";
// $ExpectError
s3Origin = request.origin.s3;
// $ExpectError
request.origin.s3.path = '/';
}
let customOrigin: AWSLambda.CloudFrontCustomOrigin = {
customHeaders: {},
domainName: 'example.com',
keepaliveTimeout: 60,
path: '/',
port: 80,
protocol: 'http',
readTimeout: 30,
sslProtocols: []
};
event = CloudFrontRequestWithS3OriginEvent;
// $ExpectType CloudFrontRequestEvent
event;
request = event.Records[0].cf.request;
if (request.origin && request.origin.s3) {
request.origin.s3.path;
request.origin.s3.path = "/new_path";
// $ExpectError
customOrigin = request.origin.custom;
// $ExpectError
request.origin.custom.path = '/';
}
cb();
cb(null);
cb(new Error(''));
+28
View File
@@ -21,6 +21,7 @@
// Jeremy Nagel <https://github.com/jeznag>
// Louis Larry <https://github.com/louislarry>
// Daniel Papukchiev <https://github.com/dpapukchiev>
// Oliver Hookins <https://github.com/ohookins>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
@@ -561,6 +562,9 @@ export interface CodePipelineEvent {
/**
* CloudFront events
* http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html
* Bear in mind that the "example" event structure in the page above includes
* both an S3 and a Custom origin, which is not strictly allowed. Only one
* of these per event may be present.
*/
export interface CloudFrontHeaders {
[name: string]: Array<{
@@ -569,6 +573,29 @@ export interface CloudFrontHeaders {
}>;
}
export type CloudFrontOrigin =
| { s3: CloudFrontS3Origin, custom?: never }
| { custom: CloudFrontCustomOrigin, s3?: never };
export interface CloudFrontCustomOrigin {
customHeaders: CloudFrontHeaders;
domainName: string;
keepaliveTimeout: number;
path: string;
port: number;
protocol: 'http' | 'https';
readTimeout: number;
sslProtocols: string[];
}
export interface CloudFrontS3Origin {
authMethod: 'origin-access-identity' | 'none';
customHeaders: CloudFrontHeaders;
domainName: string;
path: string;
region: string;
}
export interface CloudFrontResponse {
status: string;
statusDescription: string;
@@ -581,6 +608,7 @@ export interface CloudFrontRequest {
uri: string;
querystring: string;
headers: CloudFrontHeaders;
origin?: CloudFrontOrigin;
}
export interface CloudFrontEvent {