Skip to content
Merged
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
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ distribution:
- https://my-bucket.s3.amazonaws.com
```

#### Complex origin objects

You can extend your origins configuration by declaring them as objects. For example, to add cache behaviors:
#### Custom cache behavior

```yml
# serverless.yml
Expand All @@ -71,6 +69,23 @@ distribution:
ttl: 10
```

#### Lambda@Edge

```yml
# serverless.yml

distribution:
component: '@serverless/aws-cloudfront'
inputs:
origins:
- url: https://sampleorigin.com
pathPatterns:
/sample/path:
ttl: 10
lambda@edge:
viewer-request: arn:aws:lambda:us-east-1:123:function:myFunc:version # lambda ARN including version
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the version a strict requirement for Lambda@Edge? I don't think we support versioning in our lambda component 🤔 ... In that case, this component wouldn't be composable with a lambda/function component cause they output a simple versionless ARN.

This effectively means that users of this component could only use it with an already deployed lambda via another tool, which kinda defeats the purpose of the whole thing. What you think? 🤔

Copy link
Contributor Author

@danielcondemarin danielcondemarin Aug 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately it must be the ARN of a function version and $LATEST can't be used either. How difficult would it be to add versioning onto the aws-lambda component? Or maybe a higher level component that uses aws-lambda but also manages versioning?

```

### 4. Deploy

```console
Expand Down
164 changes: 164 additions & 0 deletions __tests__/__snapshots__/lambda-at-edge.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Input origin as a custom url creates distribution with lambda associations for each event type 1`] = `
Object {
"DistributionConfig": Object {
"Aliases": Object {
"Items": Array [],
"Quantity": 0,
},
"CacheBehaviors": Object {
"Items": Array [
Object {
"AllowedMethods": Object {
"CachedMethods": Object {
"Items": Array [
"GET",
"HEAD",
],
"Quantity": 2,
},
"Items": Array [
"GET",
"HEAD",
],
"Quantity": 2,
},
"Compress": true,
"DefaultTTL": 10,
"FieldLevelEncryptionId": "",
"ForwardedValues": Object {
"Cookies": Object {
"Forward": "all",
},
"Headers": Object {
"Items": Array [],
"Quantity": 0,
},
"QueryString": true,
"QueryStringCacheKeys": Object {
"Items": Array [],
"Quantity": 0,
},
},
"LambdaFunctionAssociations": Object {
"Items": Array [
Object {
"EventType": "viewer-request",
"IncludeBody": true,
"LambdaFunctionARN": "arn:aws:lambda:us-east-1:123:function:viewerRequestFunction",
},
Object {
"EventType": "origin-request",
"IncludeBody": true,
"LambdaFunctionARN": "arn:aws:lambda:us-east-1:123:function:originRequestFunction",
},
Object {
"EventType": "origin-response",
"IncludeBody": true,
"LambdaFunctionARN": "arn:aws:lambda:us-east-1:123:function:originResponseFunction",
},
Object {
"EventType": "viewer-response",
"IncludeBody": true,
"LambdaFunctionARN": "arn:aws:lambda:us-east-1:123:function:viewerResponseFunction",
},
],
"Quantity": 4,
},
"MaxTTL": 10,
"MinTTL": 10,
"PathPattern": "/some/path",
"SmoothStreaming": false,
"TargetOriginId": "exampleorigin.com",
"TrustedSigners": Object {
"Enabled": false,
"Quantity": 0,
},
"ViewerProtocolPolicy": "https-only",
},
],
"Quantity": 1,
},
"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": "exampleorigin.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": "exampleorigin.com",
"Id": "exampleorigin.com",
"OriginPath": "",
},
],
"Quantity": 1,
},
"PriceClass": "PriceClass_All",
},
}
`;
103 changes: 41 additions & 62 deletions __tests__/custom-url-origin.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createComponent } = require('../test-utils')
const { createComponent, assertHasOrigin } = require('../test-utils')

const {
mockCreateDistribution,
Expand Down Expand Up @@ -26,36 +26,26 @@ describe('Input origin as a custom url', () => {
origins: ['https://mycustomorigin.com']
})

expect(mockCreateDistribution).toBeCalledWith(
expect.objectContaining({
DistributionConfig: expect.objectContaining({
Origins: expect.objectContaining({
Items: [
{
Id: 'mycustomorigin.com',
DomainName: 'mycustomorigin.com',
CustomOriginConfig: {
HTTPPort: 80,
HTTPSPort: 443,
OriginProtocolPolicy: 'https-only',
OriginSslProtocols: {
Quantity: 1,
Items: ['TLSv1.2']
},
OriginReadTimeout: 30,
OriginKeepaliveTimeout: 5
},
CustomHeaders: {
Quantity: 0,
Items: []
},
OriginPath: ''
}
]
})
})
})
)
assertHasOrigin(mockCreateDistribution, {
Id: 'mycustomorigin.com',
DomainName: 'mycustomorigin.com',
CustomOriginConfig: {
HTTPPort: 80,
HTTPSPort: 443,
OriginProtocolPolicy: 'https-only',
OriginSslProtocols: {
Quantity: 1,
Items: ['TLSv1.2']
},
OriginReadTimeout: 30,
OriginKeepaliveTimeout: 5
},
CustomHeaders: {
Quantity: 0,
Items: []
},
OriginPath: ''
})
expect(mockCreateDistribution.mock.calls[0][0]).toMatchSnapshot()
})

Expand All @@ -82,37 +72,26 @@ describe('Input origin as a custom url', () => {
origins: ['https://mycustomoriginupdated.com']
})

expect(mockUpdateDistribution).toBeCalledWith(
expect.objectContaining({
DistributionConfig: expect.objectContaining({
Origins: expect.objectContaining({
Items: [
{
Id: 'mycustomoriginupdated.com',
DomainName: 'mycustomoriginupdated.com',
CustomOriginConfig: {
HTTPPort: 80,
HTTPSPort: 443,
OriginProtocolPolicy: 'https-only',
OriginSslProtocols: {
Quantity: 1,
Items: ['TLSv1.2']
},
OriginReadTimeout: 30,
OriginKeepaliveTimeout: 5
},
CustomHeaders: {
Quantity: 0,
Items: []
},
OriginPath: ''
}
]
})
})
})
)

assertHasOrigin(mockUpdateDistribution, {
Id: 'mycustomoriginupdated.com',
DomainName: 'mycustomoriginupdated.com',
CustomOriginConfig: {
HTTPPort: 80,
HTTPSPort: 443,
OriginProtocolPolicy: 'https-only',
OriginSslProtocols: {
Quantity: 1,
Items: ['TLSv1.2']
},
OriginReadTimeout: 30,
OriginKeepaliveTimeout: 5
},
CustomHeaders: {
Quantity: 0,
Items: []
},
OriginPath: ''
})
expect(mockUpdateDistribution.mock.calls[0][0]).toMatchSnapshot()
})
})
Loading