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

Improved handling of origins and lambda triggers #19

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ distribution:
ttl: 10
lambda@edge:
viewer-request: arn:aws:lambda:us-east-1:123:function:myFunc:version # lambda ARN including version
response-request: # can also send object to not include body
arn: lambda-arn
includeBody: false
```

#### Private S3 Content
Expand Down
10 changes: 5 additions & 5 deletions __tests__/__snapshots__/cache-behavior-options.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Object {
"MinTTL": 0,
"PathPattern": "/sample/path",
"SmoothStreaming": false,
"TargetOriginId": "mycustomorigin.com",
"TargetOriginId": "mycustomorigin.com/path",
"TrustedSigners": Object {
"Enabled": false,
"Quantity": 0,
Expand Down Expand Up @@ -108,7 +108,7 @@ Object {
"MaxTTL": 31536000,
"MinTTL": 0,
"SmoothStreaming": false,
"TargetOriginId": "mycustomorigin.com",
"TargetOriginId": "mycustomorigin.com/path",
"TrustedSigners": Object {
"Enabled": false,
"Items": Array [],
Expand All @@ -129,7 +129,7 @@ Object {
"HTTPPort": 80,
"HTTPSPort": 443,
"OriginKeepaliveTimeout": 5,
"OriginProtocolPolicy": "https-only",
"OriginProtocolPolicy": "http-only",
"OriginReadTimeout": 30,
"OriginSslProtocols": Object {
"Items": Array [
Expand All @@ -139,8 +139,8 @@ Object {
},
},
"DomainName": "mycustomorigin.com",
"Id": "mycustomorigin.com",
"OriginPath": "",
"Id": "mycustomorigin.com/path",
"OriginPath": "/path",
},
],
"Quantity": 1,
Expand Down
6 changes: 3 additions & 3 deletions __tests__/__snapshots__/lambda-at-edge.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ Object {
},
Object {
"EventType": "origin-request",
"IncludeBody": true,
"IncludeBody": false,
"LambdaFunctionARN": "arn:aws:lambda:us-east-1:123:function:originRequestFunction",
},
Object {
"EventType": "origin-response",
"IncludeBody": true,
"IncludeBody": false,
"LambdaFunctionARN": "arn:aws:lambda:us-east-1:123:function:originResponseFunction",
},
Object {
"EventType": "viewer-response",
"IncludeBody": true,
"IncludeBody": false,
"LambdaFunctionARN": "arn:aws:lambda:us-east-1:123:function:viewerResponseFunction",
},
],
Expand Down
178 changes: 178 additions & 0 deletions __tests__/__snapshots__/s3-origin.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,181 @@ Object {
"IfMatch": "etag",
}
`;

exports[`S3 origins When origin is an S3 bucket URL with path creates distribution 1`] = `
Object {
"DistributionConfig": Object {
"Aliases": Object {
"Items": Array [],
"Quantity": 0,
},
"CacheBehaviors": Object {
"Items": Array [],
"Quantity": 0,
},
"CallerReference": "1566599541192",
"Comment": "",
"DefaultCacheBehavior": Object {
"AllowedMethods": Object {
"CachedMethods": Object {
"Items": Array [
"HEAD",
"GET",
],
"Quantity": 2,
},
"Items": Array [
"HEAD",
"GET",
],
"Quantity": 2,
},
"Compress": false,
"DefaultTTL": 86400,
"FieldLevelEncryptionId": "",
"ForwardedValues": Object {
"Cookies": Object {
"Forward": "none",
},
"Headers": Object {
"Items": Array [],
"Quantity": 0,
},
"QueryString": false,
"QueryStringCacheKeys": Object {
"Items": Array [],
"Quantity": 0,
},
},
"LambdaFunctionAssociations": Object {
"Items": Array [],
"Quantity": 0,
},
"MaxTTL": 31536000,
"MinTTL": 0,
"SmoothStreaming": false,
"TargetOriginId": "mybucket/static",
"TrustedSigners": Object {
"Enabled": false,
"Items": Array [],
"Quantity": 0,
},
"ViewerProtocolPolicy": "redirect-to-https",
},
"Enabled": true,
"HttpVersion": "http2",
"Origins": Object {
"Items": Array [
Object {
"CustomHeaders": Object {
"Items": Array [],
"Quantity": 0,
},
"DomainName": "mybucket.s3.amazonaws.com",
"Id": "mybucket/static",
"OriginPath": "/static",
"S3OriginConfig": Object {
"OriginAccessIdentity": "",
},
},
],
"Quantity": 1,
},
"PriceClass": "PriceClass_All",
},
}
`;

exports[`S3 origins When origin is an S3 website URL creates custom origin not s3 origin distribution 1`] = `
Object {
"DistributionConfig": Object {
"Aliases": Object {
"Items": Array [],
"Quantity": 0,
},
"CacheBehaviors": Object {
"Items": Array [],
"Quantity": 0,
},
"CallerReference": "1566599541192",
"Comment": "",
"DefaultCacheBehavior": Object {
"AllowedMethods": Object {
"CachedMethods": Object {
"Items": Array [
"HEAD",
"GET",
],
"Quantity": 2,
},
"Items": Array [
"HEAD",
"GET",
],
"Quantity": 2,
},
"Compress": false,
"DefaultTTL": 86400,
"FieldLevelEncryptionId": "",
"ForwardedValues": Object {
"Cookies": Object {
"Forward": "none",
},
"Headers": Object {
"Items": Array [],
"Quantity": 0,
},
"QueryString": false,
"QueryStringCacheKeys": Object {
"Items": Array [],
"Quantity": 0,
},
},
"LambdaFunctionAssociations": Object {
"Items": Array [],
"Quantity": 0,
},
"MaxTTL": 31536000,
"MinTTL": 0,
"SmoothStreaming": false,
"TargetOriginId": "mybucket.s3-website.amazonaws.com",
"TrustedSigners": Object {
"Enabled": false,
"Items": Array [],
"Quantity": 0,
},
"ViewerProtocolPolicy": "redirect-to-https",
},
"Enabled": true,
"HttpVersion": "http2",
"Origins": Object {
"Items": Array [
Object {
"CustomHeaders": Object {
"Items": Array [],
"Quantity": 0,
},
"CustomOriginConfig": Object {
"HTTPPort": 80,
"HTTPSPort": 443,
"OriginKeepaliveTimeout": 5,
"OriginProtocolPolicy": "https-only",
"OriginReadTimeout": 30,
"OriginSslProtocols": Object {
"Items": Array [
"TLSv1.2",
],
"Quantity": 1,
},
},
"DomainName": "mybucket.s3-website.amazonaws.com",
"Id": "mybucket.s3-website.amazonaws.com",
"OriginPath": "",
},
],
"Quantity": 1,
},
"PriceClass": "PriceClass_All",
},
}
`;
3 changes: 2 additions & 1 deletion __tests__/cache-behavior-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ describe('Input origin as a custom url', () => {
},
origins: [
{
url: 'https://mycustomorigin.com',
url: 'https://mycustomorigin.com/path',
protocolPolicy: 'http-only',
pathPatterns: {
'/sample/path': {
ttl: 0,
Expand Down
43 changes: 38 additions & 5 deletions __tests__/lambda-at-edge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ describe('Input origin as a custom url', () => {
'/some/path': {
ttl: 10,
'lambda@edge': {
'viewer-request': 'arn:aws:lambda:us-east-1:123:function:viewerRequestFunction',
'origin-request': 'arn:aws:lambda:us-east-1:123:function:originRequestFunction',
'viewer-request': {
arn: 'arn:aws:lambda:us-east-1:123:function:viewerRequestFunction',
includeBody: true
},
'origin-request': {
arn: 'arn:aws:lambda:us-east-1:123:function:originRequestFunction',
includeBody: false
},
'origin-response': 'arn:aws:lambda:us-east-1:123:function:originResponseFunction',
'viewer-response': 'arn:aws:lambda:us-east-1:123:function:viewerResponseFunction'
}
Expand All @@ -48,17 +54,17 @@ describe('Input origin as a custom url', () => {
{
EventType: 'origin-request',
LambdaFunctionARN: 'arn:aws:lambda:us-east-1:123:function:originRequestFunction',
IncludeBody: true
IncludeBody: false
},
{
EventType: 'origin-response',
LambdaFunctionARN: 'arn:aws:lambda:us-east-1:123:function:originResponseFunction',
IncludeBody: true
IncludeBody: false
},
{
EventType: 'viewer-response',
LambdaFunctionARN: 'arn:aws:lambda:us-east-1:123:function:viewerResponseFunction',
IncludeBody: true
IncludeBody: false
}
]
}
Expand Down Expand Up @@ -92,4 +98,31 @@ describe('Input origin as a custom url', () => {
)
}
})

it('throws error when includeBody given for event types other than request', async () => {
expect.assertions(1)

try {
await component.default({
origins: [
{
url: 'https://exampleorigin.com',
pathPatterns: {
'/some/path': {
ttl: 10,
'lambda@edge': {
'viewer-response': {
arn: 'arn:aws:lambda:us-east-1:123:function:viewerRequestFunction',
includeBody: true
}
}
}
}
}
]
})
} catch (err) {
expect(err.message).toEqual('"includeBody" not allowed for viewer-response lambda triggers.')
}
})
})
54 changes: 54 additions & 0 deletions __tests__/s3-origin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,60 @@ describe('S3 origins', () => {
})
})

describe('When origin is an S3 bucket URL with path', () => {
it('creates distribution', async () => {
await component.default({
origins: ['https://mybucket.s3.amazonaws.com/static']
})

assertHasOrigin(mockCreateDistribution, {
Id: 'mybucket/static',
DomainName: 'mybucket.s3.amazonaws.com',
S3OriginConfig: {
OriginAccessIdentity: ''
},
CustomHeaders: {
Quantity: 0,
Items: []
},
OriginPath: '/static'
})

expect(mockCreateDistribution.mock.calls[0][0]).toMatchSnapshot()
})
})

describe('When origin is an S3 website URL', () => {
it('creates custom origin not s3 origin distribution', async () => {
await component.default({
origins: ['https://mybucket.s3-website.amazonaws.com']
})

assertHasOrigin(mockCreateDistribution, {
Id: 'mybucket.s3-website.amazonaws.com',
DomainName: 'mybucket.s3-website.amazonaws.com',
CustomHeaders: {
Quantity: 0,
Items: []
},
CustomOriginConfig: {
HTTPPort: 80,
HTTPSPort: 443,
OriginProtocolPolicy: 'https-only',
OriginSslProtocols: {
Quantity: 1,
Items: ['TLSv1.2']
},
OriginReadTimeout: 30,
OriginKeepaliveTimeout: 5
},
OriginPath: ''
})

expect(mockCreateDistribution.mock.calls[0][0]).toMatchSnapshot()
})
})

describe('When origin is an S3 URL only accessible via CloudFront', () => {
it('creates distribution', async () => {
mockCreateCloudFrontOriginAccessIdentityPromise.mockResolvedValueOnce({
Expand Down
Loading