From f9edea3047593808f9d0e2b2ba15650bf3acc442 Mon Sep 17 00:00:00 2001 From: Markus Tacker Date: Tue, 5 Dec 2017 20:11:36 +0100 Subject: [PATCH] [aws-lambda] add CloudFront request and response events See http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html --- types/aws-lambda/aws-lambda-tests.ts | 125 +++++++++++++++++++++++++++ types/aws-lambda/index.d.ts | 50 +++++++++++ 2 files changed, 175 insertions(+) diff --git a/types/aws-lambda/aws-lambda-tests.ts b/types/aws-lambda/aws-lambda-tests.ts index d21f477be0..c50daef248 100644 --- a/types/aws-lambda/aws-lambda-tests.ts +++ b/types/aws-lambda/aws-lambda-tests.ts @@ -315,6 +315,131 @@ function customAuthorizerCallback(cb: AWSLambda.CustomAuthorizerCallback) { cb(null, authResponse); } +/* CloudFront events, see http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html */ +var CloudFrontRequestEvent: AWSLambda.CloudFrontRequestEvent = { + "Records": [ + { + "cf": { + "config": { + "distributionId": "EDFDVBD6EXAMPLE", + "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": { + "custom": { + "customHeaders": { + "my-origin-custom-header": [ + { + "key": "My-Origin-Custom-Header", + "value": "Test" + } + ] + }, + "domainName": "example.com", + "keepaliveTimeout": 5, + "path": "/custom_path", + "port": 443, + "protocol": "https", + "readTimeout": 5, + "sslProtocols": [ + "TLSv1", + "TLSv1.1" + ] + }, + "s3": { + "authMethod": "origin-access-identity", + "customHeaders": { + "my-origin-custom-header": [ + { + "key": "My-Origin-Custom-Header", + "value": "Test" + } + ] + }, + "domainName": "my-bucket.s3.amazonaws.com", + "path": "/s3_path", + "region": "us-east-1" + } + } + } + } + } + ] +}; + +var CloudFrontResponseEvent: AWSLambda.CloudFrontResponseEvent = { + "Records": [ + { + "cf": { + "config": { + "distributionId": "EDFDVBD6EXAMPLE", + "requestId": "xGN7KWpVEmB9Dp7ctcVFQC4E-nrcOcEKS3QyAez--06dV7TEXAMPLE==" + }, + "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.18.1" + } + ] + } + }, + "response": { + "status": "200", + "statusDescription": "OK", + "headers": { + "server": [ + { + "key": "Server", + "value": "MyCustomOrigin" + } + ], + "set-cookie": [ + { + "key": "Set-Cookie", + "value": "theme=light" + }, + { + "key": "Set-Cookie", + "value": "sessionToken=abc123; Expires=Wed, 09 Jun 2021 10:18:14 GMT" + } + ] + } + } + } + } + ] +}; + /* Compatibility functions */ context.done(); context.done(error); diff --git a/types/aws-lambda/index.d.ts b/types/aws-lambda/index.d.ts index d6b50ff5e6..29fd267522 100644 --- a/types/aws-lambda/index.d.ts +++ b/types/aws-lambda/index.d.ts @@ -10,6 +10,7 @@ // Ishaan Malhi // Daniel Cottone // Kostya Misura +// Markus Tacker // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 @@ -346,6 +347,55 @@ interface AuthResponseContext { [name: string]: string | number | boolean; } +/** + * CloudFront events + * http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html + */ +type CloudFrontHeaders = { + [name: string]: { + key: string; + value: string; + }[] +}; + +type CloudFrontResponse = { + status: string; + statusDescription: string; + headers: CloudFrontHeaders; +}; + +type CloudFrontRequest = { + clientIp: string; + method: string; + uri: string; + querystring: string; + headers: CloudFrontHeaders; +}; + +type CloudFrontEvent = { + config: { + distributionId: string; + requestId: string; + } +} + +export type CloudFrontResponseEvent = { + Records: { + cf: CloudFrontEvent & { + request: CloudFrontRequest; + response: CloudFrontResponse; + } + }[] +}; + +export type CloudFrontRequestEvent = { + Records: { + cf: CloudFrontEvent & { + request: CloudFrontRequest; + } + }[] +}; + /** * AWS Lambda handler function. * http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html