├── .npmrc ├── .eslintignore ├── .prettierignore ├── jest.config.js ├── .npmignore ├── COPYRIGHT ├── prettier.config.js ├── CONTRIBUTING.md ├── .gitignore ├── lib ├── createOriginAccessIdentity.js ├── grantCloudFrontBucketAccess.js ├── addLambdaAtEdgeToCacheBehavior.js ├── getOriginConfig.js ├── getCacheBehavior.js ├── parseInputOrigins.js ├── getDefaultCacheBehavior.js ├── cacheBahaviorUtils.js └── index.js ├── package.json ├── test-utils.js ├── .eslintrc.js ├── __mocks__ └── aws-sdk.js ├── __tests__ ├── cache-behavior-options.test.js ├── general-options.test.js ├── origin-with-path-pattern.test.js ├── custom-url-origin.test.js ├── lambda-at-edge.test.js ├── __snapshots__ │ ├── lambda-at-edge.test.js.snap │ ├── custom-url-origin.test.js.snap │ ├── cache-behavior-options.test.js.snap │ ├── origin-with-path-pattern.test.js.snap │ └── s3-origin.test.js.snap └── s3-origin.test.js ├── serverless.js ├── CODE_OF_CONDUCT.md ├── README.md └── LICENSE /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | coverage 2 | dist 3 | node_modules -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | coverage 2 | dist 3 | node_modules -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | clearMocks: true 3 | } 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | components 2 | examples 3 | .idea 4 | .serverless 5 | coverage 6 | .env* 7 | env.js 8 | tmp 9 | test 10 | -------------------------------------------------------------------------------- /COPYRIGHT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2019 Serverless, Inc. https://serverless.com 2 | 3 | Serverless Components may be freely distributed under the Apache 2.0 license. 4 | -------------------------------------------------------------------------------- /prettier.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | arrowParens: 'always', 3 | printWidth: 100, 4 | semi: false, 5 | singleQuote: true, 6 | tabWidth: 2, 7 | trailingComma: 'none' 8 | } 9 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | When contributing to this repository, please first discuss the change you wish to make via issue, 4 | email, or any other method with the owners of this repository before making a change. 5 | 6 | Please note we have a [code of conduct](./CODE_OF_CONDUCT.md), please follow it in all your interactions with the project. 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.sublime-project 3 | *.sublime-workspace 4 | *.log 5 | .serverless 6 | v8-compile-cache-* 7 | jest/* 8 | coverage 9 | .serverless_plugins 10 | testProjects/*/package-lock.json 11 | testProjects/*/yarn.lock 12 | .serverlessUnzipped 13 | node_modules 14 | .vscode/ 15 | .eslintcache 16 | dist 17 | .idea 18 | build/ 19 | .env* 20 | env.js 21 | package-lock.json 22 | test -------------------------------------------------------------------------------- /lib/createOriginAccessIdentity.js: -------------------------------------------------------------------------------- 1 | module.exports = async (cf) => { 2 | const { 3 | CloudFrontOriginAccessIdentity: { Id, S3CanonicalUserId } 4 | } = await cf 5 | .createCloudFrontOriginAccessIdentity({ 6 | CloudFrontOriginAccessIdentityConfig: { 7 | CallerReference: 'serverless-managed-cloudfront-access-identity', 8 | Comment: 'CloudFront Origin Access Identity created to allow serving private S3 content' 9 | } 10 | }) 11 | .promise() 12 | 13 | return { originAccessIdentityId: Id, s3CanonicalUserId: S3CanonicalUserId } 14 | } 15 | -------------------------------------------------------------------------------- /lib/grantCloudFrontBucketAccess.js: -------------------------------------------------------------------------------- 1 | module.exports = (s3, bucketName, s3CanonicalUserId) => { 2 | const policy = ` 3 | { 4 | "Version":"2012-10-17", 5 | "Id":"PolicyForCloudFrontPrivateContent", 6 | "Statement":[ 7 | { 8 | "Sid":" Grant a CloudFront Origin Identity access to support private content", 9 | "Effect":"Allow", 10 | "Principal":{"CanonicalUser":"${s3CanonicalUserId}"}, 11 | "Action":"s3:GetObject", 12 | "Resource":"arn:aws:s3:::${bucketName}/*" 13 | } 14 | ] 15 | } 16 | ` 17 | 18 | return s3 19 | .putBucketPolicy({ 20 | Bucket: bucketName, 21 | Policy: policy.replace(/(\r\n|\n|\r|\t)/gm, '') 22 | }) 23 | .promise() 24 | } 25 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@serverless/aws-cloudfront", 3 | "version": "6.0.0", 4 | "main": "./serverless.js", 5 | "publishConfig": { 6 | "access": "public" 7 | }, 8 | "scripts": { 9 | "test": "jest", 10 | "lint": "eslint . --fix --cache" 11 | }, 12 | "author": "Serverless, Inc.", 13 | "license": "Apache", 14 | "dependencies": { 15 | "@serverless/core": "^1.0.0", 16 | "aws-sdk": "^2.499.0", 17 | "ramda": "^0.26.1" 18 | }, 19 | "devDependencies": { 20 | "babel-eslint": "9.0.0", 21 | "eslint": "5.6.0", 22 | "eslint-config-prettier": "^3.6.0", 23 | "eslint-plugin-import": "^2.14.0", 24 | "eslint-plugin-prettier": "^3.0.1", 25 | "fs-extra": "^8.1.0", 26 | "jest": "^24.9.0", 27 | "prettier": "^1.15.3" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /lib/addLambdaAtEdgeToCacheBehavior.js: -------------------------------------------------------------------------------- 1 | const validLambdaTriggers = [ 2 | 'viewer-request', 3 | 'origin-request', 4 | 'origin-response', 5 | 'viewer-response' 6 | ] 7 | 8 | // adds lambda@edge to cache behavior passed 9 | module.exports = (cacheBehavior, lambdaAtEdgeConfig = {}) => { 10 | Object.keys(lambdaAtEdgeConfig).forEach((eventType) => { 11 | if (!validLambdaTriggers.includes(eventType)) { 12 | throw new Error( 13 | `"${eventType}" is not a valid lambda trigger. See https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-cloudfront-trigger-events.html for valid event types.` 14 | ) 15 | } 16 | 17 | cacheBehavior.LambdaFunctionAssociations.Quantity = 18 | cacheBehavior.LambdaFunctionAssociations.Quantity + 1 19 | cacheBehavior.LambdaFunctionAssociations.Items.push({ 20 | EventType: eventType, 21 | LambdaFunctionARN: lambdaAtEdgeConfig[eventType], 22 | IncludeBody: true 23 | }) 24 | }) 25 | } 26 | -------------------------------------------------------------------------------- /lib/getOriginConfig.js: -------------------------------------------------------------------------------- 1 | const url = require('url') 2 | 3 | module.exports = (origin, { originAccessIdentityId = '' }) => { 4 | const originUrl = typeof origin === 'string' ? origin : origin.url 5 | 6 | const { hostname, pathname } = url.parse(originUrl) 7 | 8 | const originConfig = { 9 | Id: hostname, 10 | DomainName: hostname, 11 | CustomHeaders: { 12 | Quantity: 0, 13 | Items: [] 14 | }, 15 | OriginPath: pathname === '/' ? '' : pathname 16 | } 17 | 18 | if (originUrl.includes('s3')) { 19 | const bucketName = hostname.split('.')[0] 20 | originConfig.Id = bucketName 21 | originConfig.DomainName = `${bucketName}.s3.amazonaws.com` 22 | originConfig.S3OriginConfig = { 23 | OriginAccessIdentity: originAccessIdentityId 24 | ? `origin-access-identity/cloudfront/${originAccessIdentityId}` 25 | : '' 26 | } 27 | } else { 28 | originConfig.CustomOriginConfig = { 29 | HTTPPort: 80, 30 | HTTPSPort: 443, 31 | OriginProtocolPolicy: 'https-only', 32 | OriginSslProtocols: { 33 | Quantity: 1, 34 | Items: ['TLSv1.2'] 35 | }, 36 | OriginReadTimeout: 30, 37 | OriginKeepaliveTimeout: 5 38 | } 39 | } 40 | 41 | return originConfig 42 | } 43 | -------------------------------------------------------------------------------- /lib/getCacheBehavior.js: -------------------------------------------------------------------------------- 1 | const { getForwardedValues } = require('./cacheBahaviorUtils') 2 | 3 | module.exports = (pathPattern, pathPatternConfig, originId) => { 4 | const { 5 | allowedHttpMethods = ['GET', 'HEAD'], 6 | ttl, 7 | compress = true, 8 | smoothStreaming = false, 9 | viewerProtocolPolicy = 'https-only', 10 | fieldLevelEncryptionId = '' 11 | } = pathPatternConfig 12 | 13 | return { 14 | ForwardedValues: getForwardedValues(pathPatternConfig.forward, { 15 | cookies: 'all', 16 | queryString: true 17 | }), 18 | MinTTL: ttl, 19 | PathPattern: pathPattern, 20 | TargetOriginId: originId, 21 | TrustedSigners: { 22 | Enabled: false, 23 | Quantity: 0 24 | }, 25 | ViewerProtocolPolicy: viewerProtocolPolicy, 26 | AllowedMethods: { 27 | Quantity: allowedHttpMethods.length, 28 | Items: allowedHttpMethods, 29 | CachedMethods: { 30 | Items: ['GET', 'HEAD'], 31 | Quantity: 2 32 | } 33 | }, 34 | Compress: compress, 35 | SmoothStreaming: smoothStreaming, 36 | DefaultTTL: ttl, 37 | MaxTTL: ttl, 38 | FieldLevelEncryptionId: fieldLevelEncryptionId, 39 | LambdaFunctionAssociations: { 40 | Quantity: 0, 41 | Items: [] 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /test-utils.js: -------------------------------------------------------------------------------- 1 | const fse = require('fs-extra') 2 | const os = require('os') 3 | const path = require('path') 4 | const CloudFrontComponent = require('./serverless') 5 | 6 | module.exports = { 7 | createComponent: async () => { 8 | // mock to prevent jest snapshots changing every time 9 | Date.now = () => 1566599541192 10 | 11 | // create tmp folder to avoid state collisions between tests 12 | const tmpFolder = await fse.mkdtemp(path.join(os.tmpdir(), 'test-')) 13 | 14 | const component = new CloudFrontComponent('TestCloudFront', { 15 | stateRoot: tmpFolder 16 | }) 17 | 18 | await component.init() 19 | 20 | return component 21 | }, 22 | 23 | assertHasCacheBehavior: (spy, cacheBehavior) => { 24 | expect(spy).toBeCalledWith( 25 | expect.objectContaining({ 26 | DistributionConfig: expect.objectContaining({ 27 | CacheBehaviors: expect.objectContaining({ 28 | Items: [expect.objectContaining(cacheBehavior)] 29 | }) 30 | }) 31 | }) 32 | ) 33 | }, 34 | 35 | assertHasOrigin: (spy, origin) => { 36 | expect(spy).toBeCalledWith( 37 | expect.objectContaining({ 38 | DistributionConfig: expect.objectContaining({ 39 | Origins: expect.objectContaining({ 40 | Items: [expect.objectContaining(origin)] 41 | }) 42 | }) 43 | }) 44 | ) 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /lib/parseInputOrigins.js: -------------------------------------------------------------------------------- 1 | const getOriginConfig = require('./getOriginConfig') 2 | const getCacheBehavior = require('./getCacheBehavior') 3 | const addLambdaAtEdgeToCacheBehavior = require('./addLambdaAtEdgeToCacheBehavior') 4 | 5 | module.exports = (origins, options) => { 6 | const distributionOrigins = { 7 | Quantity: 0, 8 | Items: [] 9 | } 10 | 11 | const distributionCacheBehaviors = { 12 | Quantity: 0, 13 | Items: [] 14 | } 15 | 16 | for (const origin of origins) { 17 | const originConfig = getOriginConfig(origin, options) 18 | 19 | distributionOrigins.Quantity = distributionOrigins.Quantity + 1 20 | distributionOrigins.Items.push(originConfig) 21 | 22 | if (typeof origin === 'object') { 23 | // add any cache behaviors 24 | for (const pathPattern in origin.pathPatterns) { 25 | const pathPatternConfig = origin.pathPatterns[pathPattern] 26 | const cacheBehavior = getCacheBehavior(pathPattern, pathPatternConfig, originConfig.Id) 27 | 28 | addLambdaAtEdgeToCacheBehavior(cacheBehavior, pathPatternConfig['lambda@edge']) 29 | 30 | distributionCacheBehaviors.Quantity = distributionCacheBehaviors.Quantity + 1 31 | distributionCacheBehaviors.Items.push(cacheBehavior) 32 | } 33 | } 34 | } 35 | 36 | return { 37 | Origins: distributionOrigins, 38 | CacheBehaviors: distributionCacheBehaviors 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /lib/getDefaultCacheBehavior.js: -------------------------------------------------------------------------------- 1 | const addLambdaAtEdgeToCacheBehavior = require('./addLambdaAtEdgeToCacheBehavior') 2 | const { getForwardedValues } = require('./cacheBahaviorUtils') 3 | 4 | module.exports = (originId, defaults = {}) => { 5 | const { 6 | allowedHttpMethods = ['HEAD', 'GET'], 7 | forward = {}, 8 | ttl = 86400, 9 | compress = false, 10 | smoothStreaming = false, 11 | viewerProtocolPolicy = 'redirect-to-https', 12 | fieldLevelEncryptionId = '' 13 | } = defaults 14 | 15 | const defaultCacheBehavior = { 16 | TargetOriginId: originId, 17 | ForwardedValues: getForwardedValues(forward), 18 | TrustedSigners: { 19 | Enabled: false, 20 | Quantity: 0, 21 | Items: [] 22 | }, 23 | ViewerProtocolPolicy: viewerProtocolPolicy, 24 | MinTTL: 0, 25 | AllowedMethods: { 26 | Quantity: allowedHttpMethods.length, 27 | Items: allowedHttpMethods, 28 | CachedMethods: { 29 | Quantity: 2, 30 | Items: ['HEAD', 'GET'] 31 | } 32 | }, 33 | SmoothStreaming: smoothStreaming, 34 | DefaultTTL: ttl, 35 | MaxTTL: 31536000, 36 | Compress: compress, 37 | LambdaFunctionAssociations: { 38 | Quantity: 0, 39 | Items: [] 40 | }, 41 | FieldLevelEncryptionId: fieldLevelEncryptionId 42 | } 43 | 44 | addLambdaAtEdgeToCacheBehavior(defaultCacheBehavior, defaults['lambda@edge']) 45 | 46 | return defaultCacheBehavior 47 | } 48 | -------------------------------------------------------------------------------- /lib/cacheBahaviorUtils.js: -------------------------------------------------------------------------------- 1 | const forwardDefaults = { 2 | cookies: 'none', 3 | queryString: false 4 | } 5 | 6 | /** 7 | * @param config User-defined config 8 | * @param defaults Default framework values (default cache behavior and custom cache behavior have different default values) 9 | * @returns Object 10 | */ 11 | function getForwardedValues(config = {}, defaults = {}) { 12 | const defaultValues = { ...forwardDefaults, ...defaults } 13 | const { cookies, queryString = defaultValues.queryString, headers, queryStringCacheKeys } = config 14 | 15 | // Cookies 16 | const forwardCookies = { 17 | Forward: defaultValues.cookies 18 | } 19 | 20 | if (typeof cookies === 'string') { 21 | forwardCookies.Forward = cookies 22 | } else if (Array.isArray(cookies)) { 23 | forwardCookies.Forward = 'whitelist' 24 | forwardCookies.WhitelistedNames = { 25 | Quantity: cookies.length, 26 | Items: cookies 27 | } 28 | } 29 | 30 | // Headers 31 | const forwardHeaders = { 32 | Quantity: 0, 33 | Items: [] 34 | } 35 | 36 | if (typeof headers === 'string' && headers === 'all') { 37 | forwardHeaders.Quantity = 1 38 | forwardHeaders.Items = ['*'] 39 | } else if (Array.isArray(headers)) { 40 | forwardHeaders.Quantity = headers.length 41 | forwardHeaders.Items = headers 42 | } 43 | 44 | // QueryStringCacheKeys 45 | const forwardQueryKeys = { 46 | Quantity: 0, 47 | Items: [] 48 | } 49 | 50 | if (Array.isArray(queryStringCacheKeys)) { 51 | forwardQueryKeys.Quantity = queryStringCacheKeys.length 52 | forwardQueryKeys.Items = queryStringCacheKeys 53 | } 54 | 55 | return { 56 | QueryString: queryString, 57 | Cookies: forwardCookies, 58 | Headers: forwardHeaders, 59 | QueryStringCacheKeys: forwardQueryKeys 60 | } 61 | } 62 | 63 | module.exports = { getForwardedValues } 64 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | extends: ['prettier'], 4 | plugins: ['import', 'prettier'], 5 | env: { 6 | es6: true, 7 | jest: true, 8 | node: true 9 | }, 10 | parser: 'babel-eslint', 11 | parserOptions: { 12 | ecmaVersion: 2018, 13 | sourceType: 'module', 14 | ecmaFeatures: { 15 | jsx: true 16 | } 17 | }, 18 | globals: { 19 | on: true // for the Socket file 20 | }, 21 | rules: { 22 | 'array-bracket-spacing': [ 23 | 'error', 24 | 'never', 25 | { 26 | objectsInArrays: false, 27 | arraysInArrays: false 28 | } 29 | ], 30 | 'arrow-parens': ['error', 'always'], 31 | 'arrow-spacing': ['error', { before: true, after: true }], 32 | 'comma-dangle': ['error', 'never'], 33 | curly: 'error', 34 | 'eol-last': 'error', 35 | 'func-names': 'off', 36 | 'id-length': [ 37 | 'error', 38 | { 39 | min: 2, 40 | max: 50, 41 | properties: 'never', 42 | exceptions: ['e', 'i', 'n', 't', 'x', 'y', 'z', '_', '$'] 43 | } 44 | ], 45 | 'no-alert': 'error', 46 | 'no-console': 'error', 47 | 'no-const-assign': 'error', 48 | 'no-else-return': 'error', 49 | 'no-empty': 'off', 50 | 'no-shadow': 'error', 51 | 'no-undef': 'error', 52 | 'no-unused-vars': 'error', 53 | 'no-use-before-define': 'error', 54 | 'no-useless-constructor': 'error', 55 | 'object-curly-newline': 'off', 56 | 'object-shorthand': 'off', 57 | 'prefer-const': 'error', 58 | 'prefer-destructuring': ['error', { object: true, array: false }], 59 | quotes: [ 60 | 'error', 61 | 'single', 62 | { 63 | allowTemplateLiterals: true, 64 | avoidEscape: true 65 | } 66 | ], 67 | semi: ['error', 'never'], 68 | 'spaced-comment': 'error', 69 | strict: ['error', 'never'], 70 | 'prettier/prettier': 'error' 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /__mocks__/aws-sdk.js: -------------------------------------------------------------------------------- 1 | const promisifyMock = (mockFn) => { 2 | const promise = jest.fn() 3 | mockFn.mockImplementation(() => ({ 4 | promise 5 | })) 6 | 7 | return promise 8 | } 9 | 10 | const mockCreateDistribution = jest.fn() 11 | const mockCreateDistributionPromise = promisifyMock(mockCreateDistribution) 12 | 13 | const mockUpdateDistribution = jest.fn() 14 | const mockUpdateDistributionPromise = promisifyMock(mockUpdateDistribution) 15 | 16 | const mockGetDistributionConfig = jest.fn() 17 | const mockGetDistributionConfigPromise = promisifyMock(mockGetDistributionConfig) 18 | 19 | const mockDeleteDistribution = jest.fn() 20 | const mockDeleteDistributionPromise = promisifyMock(mockDeleteDistribution) 21 | 22 | const mockCreateCloudFrontOriginAccessIdentity = jest.fn() 23 | const mockCreateCloudFrontOriginAccessIdentityPromise = promisifyMock( 24 | mockCreateCloudFrontOriginAccessIdentity 25 | ) 26 | 27 | const mockPutBucketPolicy = jest.fn() 28 | const mockPutBucketPolicyPromise = promisifyMock(mockPutBucketPolicy) 29 | 30 | module.exports = { 31 | mockCreateDistribution, 32 | mockUpdateDistribution, 33 | mockGetDistributionConfig, 34 | mockDeleteDistribution, 35 | mockCreateCloudFrontOriginAccessIdentity, 36 | mockPutBucketPolicy, 37 | 38 | mockPutBucketPolicyPromise, 39 | mockCreateDistributionPromise, 40 | mockUpdateDistributionPromise, 41 | mockGetDistributionConfigPromise, 42 | mockDeleteDistributionPromise, 43 | mockCreateCloudFrontOriginAccessIdentityPromise, 44 | 45 | CloudFront: jest.fn(() => ({ 46 | createDistribution: mockCreateDistribution, 47 | updateDistribution: mockUpdateDistribution, 48 | getDistributionConfig: mockGetDistributionConfig, 49 | deleteDistribution: mockDeleteDistribution, 50 | createCloudFrontOriginAccessIdentity: mockCreateCloudFrontOriginAccessIdentity 51 | })), 52 | 53 | S3: jest.fn(() => ({ 54 | putBucketPolicy: mockPutBucketPolicy 55 | })) 56 | } 57 | -------------------------------------------------------------------------------- /__tests__/cache-behavior-options.test.js: -------------------------------------------------------------------------------- 1 | const { createComponent } = require('../test-utils') 2 | 3 | const { mockCreateDistribution, mockCreateDistributionPromise } = require('aws-sdk') 4 | 5 | describe('Input origin as a custom url', () => { 6 | let component 7 | 8 | beforeEach(async () => { 9 | mockCreateDistributionPromise.mockResolvedValueOnce({ 10 | Distribution: { 11 | Id: 'distribution123' 12 | } 13 | }) 14 | 15 | component = await createComponent() 16 | }) 17 | 18 | it('creates distribution with custom default behavior options', async () => { 19 | await component.default({ 20 | defaults: { 21 | ttl: 0, 22 | forward: { 23 | headers: ['Accept', 'Accept-Language'], 24 | cookies: 'all', 25 | queryString: true, 26 | queryStringCacheKeys: ['query'] 27 | }, 28 | allowedHttpMethods: ['GET', 'HEAD', 'OPTIONS', 'PUT', 'POST', 'PATCH', 'DELETE'], 29 | viewerProtocolPolicy: 'https-only', 30 | smoothStreaming: true, 31 | compress: true, 32 | fieldLevelEncryptionId: '123' 33 | }, 34 | origins: ['https://mycustomorigin.com'] 35 | }) 36 | 37 | expect(mockCreateDistribution.mock.calls[0][0]).toMatchSnapshot() 38 | }) 39 | 40 | it('creates distribution with custom behavior options', async () => { 41 | await component.default({ 42 | defaults: { 43 | ttl: 0 44 | }, 45 | origins: [ 46 | { 47 | url: 'https://mycustomorigin.com', 48 | pathPatterns: { 49 | '/sample/path': { 50 | ttl: 0, 51 | forward: { 52 | headers: 'all', 53 | cookies: ['auth-token'], 54 | queryString: true 55 | }, 56 | allowedHttpMethods: ['GET', 'HEAD'], 57 | viewerProtocolPolicy: 'redirect-to-https', 58 | compress: false, 59 | fieldLevelEncryptionId: '321' 60 | } 61 | } 62 | } 63 | ] 64 | }) 65 | 66 | expect(mockCreateDistribution.mock.calls[0][0]).toMatchSnapshot() 67 | }) 68 | }) 69 | -------------------------------------------------------------------------------- /__tests__/general-options.test.js: -------------------------------------------------------------------------------- 1 | const { createComponent } = require('../test-utils') 2 | 3 | const { 4 | mockCreateDistribution, 5 | mockUpdateDistribution, 6 | mockCreateDistributionPromise, 7 | mockGetDistributionConfigPromise, 8 | mockUpdateDistributionPromise 9 | } = require('aws-sdk') 10 | 11 | describe('General options propagation', () => { 12 | let component 13 | 14 | // sample origins 15 | const origins = ['https://exampleorigin.com'] 16 | 17 | beforeEach(async () => { 18 | mockCreateDistributionPromise.mockResolvedValueOnce({ 19 | Distribution: { 20 | Id: 'distribution123' 21 | } 22 | }) 23 | 24 | mockGetDistributionConfigPromise.mockResolvedValueOnce({ 25 | ETag: 'etag', 26 | DistributionConfig: { 27 | Origins: { 28 | Items: [] 29 | } 30 | } 31 | }) 32 | mockUpdateDistributionPromise.mockResolvedValueOnce({ 33 | Distribution: { 34 | Id: 'xyz' 35 | } 36 | }) 37 | 38 | component = await createComponent() 39 | }) 40 | 41 | it('create distribution with comment and update it', async () => { 42 | await component.default({ 43 | comment: 'test comment', 44 | origins 45 | }) 46 | 47 | expect(mockCreateDistribution).toBeCalledWith( 48 | expect.objectContaining({ 49 | DistributionConfig: expect.objectContaining({ 50 | Comment: 'test comment' 51 | }) 52 | }) 53 | ) 54 | 55 | await component.default({ 56 | comment: 'updated comment', 57 | origins 58 | }) 59 | 60 | expect(mockUpdateDistribution).toBeCalledWith( 61 | expect.objectContaining({ 62 | DistributionConfig: expect.objectContaining({ 63 | Comment: 'updated comment' 64 | }) 65 | }) 66 | ) 67 | }) 68 | 69 | it('create disabled distribution and update it', async () => { 70 | await component.default({ 71 | enabled: false, 72 | origins 73 | }) 74 | 75 | expect(mockCreateDistribution).toBeCalledWith( 76 | expect.objectContaining({ 77 | DistributionConfig: expect.objectContaining({ 78 | Enabled: false 79 | }) 80 | }) 81 | ) 82 | 83 | await component.default({ 84 | enabled: true, 85 | origins 86 | }) 87 | 88 | expect(mockUpdateDistribution).toBeCalledWith( 89 | expect.objectContaining({ 90 | DistributionConfig: expect.objectContaining({ 91 | Enabled: true 92 | }) 93 | }) 94 | ) 95 | }) 96 | }) 97 | -------------------------------------------------------------------------------- /serverless.js: -------------------------------------------------------------------------------- 1 | const aws = require('aws-sdk') 2 | const { equals } = require('ramda') 3 | const { Component } = require('@serverless/core') 4 | const { 5 | createCloudFrontDistribution, 6 | updateCloudFrontDistribution, 7 | deleteCloudFrontDistribution 8 | } = require('./lib') 9 | 10 | /* 11 | * Website 12 | */ 13 | 14 | class CloudFront extends Component { 15 | /* 16 | * Default 17 | */ 18 | 19 | async default(inputs = {}) { 20 | this.context.status('Deploying') 21 | 22 | inputs.region = inputs.region || 'us-east-1' 23 | inputs.enabled = inputs.enabled === false ? false : true 24 | inputs.comment = 25 | inputs.comment === null || inputs.comment === undefined ? '' : String(inputs.comment) 26 | 27 | this.context.debug( 28 | `Starting deployment of CloudFront distribution to the ${inputs.region} region.` 29 | ) 30 | 31 | const cf = new aws.CloudFront({ 32 | credentials: this.context.credentials.aws, 33 | region: inputs.region 34 | }) 35 | 36 | const s3 = new aws.S3({ 37 | credentials: this.context.credentials.aws, 38 | region: inputs.region 39 | }) 40 | 41 | if (this.state.id) { 42 | if ( 43 | !equals(this.state.origins, inputs.origins) || 44 | !equals(this.state.defaults, inputs.defaults) || 45 | !equals(this.state.enabled, inputs.enabled) || 46 | !equals(this.state.comment, inputs.comment) 47 | ) { 48 | this.context.debug(`Updating CloudFront distribution of ID ${this.state.id}.`) 49 | this.state = await updateCloudFrontDistribution(cf, s3, this.state.id, inputs) 50 | } 51 | } else { 52 | this.context.debug(`Creating CloudFront distribution in the ${inputs.region} region.`) 53 | this.state = await createCloudFrontDistribution(cf, s3, inputs) 54 | } 55 | 56 | this.state.region = inputs.region 57 | this.state.enabled = inputs.enabled 58 | this.state.comment = inputs.comment 59 | this.state.origins = inputs.origins 60 | this.state.defaults = inputs.defaults 61 | await this.save() 62 | 63 | this.context.debug(`CloudFront deployed successfully with URL: ${this.state.url}.`) 64 | 65 | return this.state 66 | } 67 | 68 | /** 69 | * Remove 70 | */ 71 | 72 | async remove() { 73 | this.context.status(`Removing`) 74 | 75 | if (!this.state.id) { 76 | return 77 | } 78 | 79 | const cf = new aws.CloudFront({ 80 | credentials: this.context.credentials.aws, 81 | region: this.state.region 82 | }) 83 | 84 | await deleteCloudFrontDistribution(cf, this.state.id) 85 | 86 | this.state = {} 87 | await this.save() 88 | 89 | this.context.debug(`CloudFront distribution was successfully removed.`) 90 | return {} 91 | } 92 | } 93 | 94 | module.exports = CloudFront 95 | -------------------------------------------------------------------------------- /__tests__/origin-with-path-pattern.test.js: -------------------------------------------------------------------------------- 1 | const { createComponent, assertHasCacheBehavior, assertHasOrigin } = require('../test-utils') 2 | 3 | const { 4 | mockCreateDistribution, 5 | mockUpdateDistribution, 6 | mockCreateDistributionPromise, 7 | mockGetDistributionConfigPromise, 8 | mockUpdateDistributionPromise 9 | } = require('aws-sdk') 10 | 11 | describe('Input origin with path pattern', () => { 12 | let component 13 | 14 | beforeEach(async () => { 15 | mockCreateDistributionPromise.mockResolvedValueOnce({ 16 | Distribution: { 17 | Id: 'xyz' 18 | } 19 | }) 20 | 21 | component = await createComponent() 22 | }) 23 | 24 | it('creates distribution with custom url origin', async () => { 25 | await component.default({ 26 | origins: [ 27 | { 28 | url: 'https://exampleorigin.com', 29 | pathPatterns: { 30 | '/some/path': { 31 | ttl: 10, 32 | allowedHttpMethods: ['GET', 'HEAD', 'POST'] 33 | } 34 | } 35 | } 36 | ] 37 | }) 38 | 39 | assertHasOrigin(mockCreateDistribution, { 40 | Id: 'exampleorigin.com', 41 | DomainName: 'exampleorigin.com' 42 | }) 43 | 44 | assertHasCacheBehavior(mockCreateDistribution, { 45 | PathPattern: '/some/path', 46 | MinTTL: 10, 47 | TargetOriginId: 'exampleorigin.com' 48 | }) 49 | 50 | expect(mockCreateDistribution.mock.calls[0][0]).toMatchSnapshot() 51 | }) 52 | 53 | it('updates distribution', async () => { 54 | mockGetDistributionConfigPromise.mockResolvedValueOnce({ 55 | ETag: 'etag', 56 | DistributionConfig: { 57 | Origins: { 58 | Items: [] 59 | } 60 | } 61 | }) 62 | mockUpdateDistributionPromise.mockResolvedValueOnce({ 63 | Distribution: { 64 | Id: 'xyz' 65 | } 66 | }) 67 | 68 | await component.default({ 69 | origins: [ 70 | { 71 | url: 'https://exampleorigin.com', 72 | pathPatterns: { 73 | '/some/path': { 74 | ttl: 10 75 | } 76 | } 77 | } 78 | ] 79 | }) 80 | 81 | await component.default({ 82 | origins: [ 83 | { 84 | url: 'https://exampleorigin.com', 85 | pathPatterns: { 86 | '/some/other/path': { 87 | ttl: 10 88 | } 89 | } 90 | } 91 | ] 92 | }) 93 | 94 | assertHasOrigin(mockUpdateDistribution, { 95 | Id: 'exampleorigin.com', 96 | DomainName: 'exampleorigin.com' 97 | }) 98 | 99 | assertHasCacheBehavior(mockUpdateDistribution, { 100 | PathPattern: '/some/other/path', 101 | MinTTL: 10, 102 | TargetOriginId: 'exampleorigin.com' 103 | }) 104 | 105 | expect(mockUpdateDistribution.mock.calls[0][0]).toMatchSnapshot() 106 | }) 107 | }) 108 | -------------------------------------------------------------------------------- /__tests__/custom-url-origin.test.js: -------------------------------------------------------------------------------- 1 | const { createComponent, assertHasOrigin } = require('../test-utils') 2 | 3 | const { 4 | mockCreateDistribution, 5 | mockUpdateDistribution, 6 | mockCreateDistributionPromise, 7 | mockGetDistributionConfigPromise, 8 | mockUpdateDistributionPromise 9 | } = require('aws-sdk') 10 | 11 | describe('Input origin as a custom url', () => { 12 | let component 13 | 14 | beforeEach(async () => { 15 | mockCreateDistributionPromise.mockResolvedValueOnce({ 16 | Distribution: { 17 | Id: 'distribution123' 18 | } 19 | }) 20 | 21 | component = await createComponent() 22 | }) 23 | 24 | it('creates distribution with custom url origin and sets defaults', async () => { 25 | await component.default({ 26 | defaults: { 27 | allowedHttpMethods: ['HEAD', 'DELETE', 'POST', 'GET', 'OPTIONS', 'PUT', 'PATCH'], 28 | ttl: 10, 29 | 'lambda@edge': { 30 | 'origin-request': 'arn:aws:lambda:us-east-1:123:function:originRequestFunction' 31 | } 32 | }, 33 | origins: ['https://mycustomorigin.com'] 34 | }) 35 | 36 | assertHasOrigin(mockCreateDistribution, { 37 | Id: 'mycustomorigin.com', 38 | DomainName: 'mycustomorigin.com', 39 | CustomOriginConfig: { 40 | HTTPPort: 80, 41 | HTTPSPort: 443, 42 | OriginProtocolPolicy: 'https-only', 43 | OriginSslProtocols: { 44 | Quantity: 1, 45 | Items: ['TLSv1.2'] 46 | }, 47 | OriginReadTimeout: 30, 48 | OriginKeepaliveTimeout: 5 49 | }, 50 | CustomHeaders: { 51 | Quantity: 0, 52 | Items: [] 53 | }, 54 | OriginPath: '' 55 | }) 56 | expect(mockCreateDistribution.mock.calls[0][0]).toMatchSnapshot() 57 | }) 58 | 59 | it('updates distribution', async () => { 60 | mockGetDistributionConfigPromise.mockResolvedValueOnce({ 61 | ETag: 'etag', 62 | DistributionConfig: { 63 | Origins: { 64 | Items: [] 65 | } 66 | } 67 | }) 68 | mockUpdateDistributionPromise.mockResolvedValueOnce({ 69 | Distribution: { 70 | Id: 'xyz' 71 | } 72 | }) 73 | 74 | await component.default({ 75 | origins: ['https://mycustomorigin.com'] 76 | }) 77 | 78 | await component.default({ 79 | origins: ['https://mycustomoriginupdated.com'] 80 | }) 81 | 82 | assertHasOrigin(mockUpdateDistribution, { 83 | Id: 'mycustomoriginupdated.com', 84 | DomainName: 'mycustomoriginupdated.com', 85 | CustomOriginConfig: { 86 | HTTPPort: 80, 87 | HTTPSPort: 443, 88 | OriginProtocolPolicy: 'https-only', 89 | OriginSslProtocols: { 90 | Quantity: 1, 91 | Items: ['TLSv1.2'] 92 | }, 93 | OriginReadTimeout: 30, 94 | OriginKeepaliveTimeout: 5 95 | }, 96 | CustomHeaders: { 97 | Quantity: 0, 98 | Items: [] 99 | }, 100 | OriginPath: '' 101 | }) 102 | expect(mockUpdateDistribution.mock.calls[0][0]).toMatchSnapshot() 103 | }) 104 | }) 105 | -------------------------------------------------------------------------------- /__tests__/lambda-at-edge.test.js: -------------------------------------------------------------------------------- 1 | const { createComponent, assertHasCacheBehavior } = require('../test-utils') 2 | 3 | const { mockCreateDistribution, mockCreateDistributionPromise } = require('aws-sdk') 4 | 5 | describe('Input origin as a custom url', () => { 6 | let component 7 | 8 | beforeEach(async () => { 9 | mockCreateDistributionPromise.mockResolvedValueOnce({ 10 | Distribution: { 11 | Id: 'distribution123' 12 | } 13 | }) 14 | 15 | component = await createComponent() 16 | }) 17 | 18 | it('creates distribution with lambda associations for each event type', async () => { 19 | await component.default({ 20 | origins: [ 21 | { 22 | url: 'https://exampleorigin.com', 23 | pathPatterns: { 24 | '/some/path': { 25 | ttl: 10, 26 | 'lambda@edge': { 27 | 'viewer-request': 'arn:aws:lambda:us-east-1:123:function:viewerRequestFunction', 28 | 'origin-request': 'arn:aws:lambda:us-east-1:123:function:originRequestFunction', 29 | 'origin-response': 'arn:aws:lambda:us-east-1:123:function:originResponseFunction', 30 | 'viewer-response': 'arn:aws:lambda:us-east-1:123:function:viewerResponseFunction' 31 | } 32 | } 33 | } 34 | } 35 | ] 36 | }) 37 | 38 | assertHasCacheBehavior(mockCreateDistribution, { 39 | PathPattern: '/some/path', 40 | LambdaFunctionAssociations: { 41 | Quantity: 4, 42 | Items: [ 43 | { 44 | EventType: 'viewer-request', 45 | LambdaFunctionARN: 'arn:aws:lambda:us-east-1:123:function:viewerRequestFunction', 46 | IncludeBody: true 47 | }, 48 | { 49 | EventType: 'origin-request', 50 | LambdaFunctionARN: 'arn:aws:lambda:us-east-1:123:function:originRequestFunction', 51 | IncludeBody: true 52 | }, 53 | { 54 | EventType: 'origin-response', 55 | LambdaFunctionARN: 'arn:aws:lambda:us-east-1:123:function:originResponseFunction', 56 | IncludeBody: true 57 | }, 58 | { 59 | EventType: 'viewer-response', 60 | LambdaFunctionARN: 'arn:aws:lambda:us-east-1:123:function:viewerResponseFunction', 61 | IncludeBody: true 62 | } 63 | ] 64 | } 65 | }) 66 | 67 | expect(mockCreateDistribution.mock.calls[0][0]).toMatchSnapshot() 68 | }) 69 | 70 | it('throws error when event type provided is not valid', async () => { 71 | expect.assertions(1) 72 | 73 | try { 74 | await component.default({ 75 | origins: [ 76 | { 77 | url: 'https://exampleorigin.com', 78 | pathPatterns: { 79 | '/some/path': { 80 | ttl: 10, 81 | 'lambda@edge': { 82 | 'invalid-eventtype': 'arn:aws:lambda:us-east-1:123:function:viewerRequestFunction' 83 | } 84 | } 85 | } 86 | } 87 | ] 88 | }) 89 | } catch (err) { 90 | expect(err.message).toEqual( 91 | '"invalid-eventtype" is not a valid lambda trigger. See https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-cloudfront-trigger-events.html for valid event types.' 92 | ) 93 | } 94 | }) 95 | }) 96 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting our team at **hello@serverless.com**. As an alternative 59 | feel free to reach out to any of us personally. All 60 | complaints will be reviewed and investigated and will result in a response that 61 | is deemed necessary and appropriate to the circumstances. The project team is 62 | obligated to maintain confidentiality with regard to the reporter of an incident. 63 | Further details of specific enforcement policies may be posted separately. 64 | 65 | Project maintainers who do not follow or enforce the Code of Conduct in good 66 | faith may face temporary or permanent repercussions as determined by other 67 | members of the project's leadership. 68 | 69 | ## Attribution 70 | 71 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 72 | available at [http://contributor-covenant.org/version/1/4][version] 73 | 74 | [homepage]: http://contributor-covenant.org 75 | [version]: http://contributor-covenant.org/version/1/4/ 76 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # aws-cloudfront 2 | 3 |   4 | 5 | Deploy an AWS CloudFront distribution for the provided origins using [Serverless Components](https://github.com/serverless/components). 6 | 7 |   8 | 9 | 1. [Install](#1-install) 10 | 2. [Create](#2-create) 11 | 3. [Configure](#3-configure) 12 | 4. [Deploy](#4-deploy) 13 | 14 |   15 | 16 | ### 1. Install 17 | 18 | ```console 19 | $ npm install -g serverless 20 | ``` 21 | 22 | ### 2. Create 23 | 24 | ```console 25 | $ mkdir cdn 26 | $ cd cdn 27 | ``` 28 | 29 | the directory should look something like this: 30 | 31 | ``` 32 | |- serverless.yml 33 | |- .env # your AWS api keys 34 | 35 | ``` 36 | 37 | ``` 38 | # .env 39 | AWS_ACCESS_KEY_ID=XXX 40 | AWS_SECRET_ACCESS_KEY=XXX 41 | ``` 42 | 43 | ### 3. Configure 44 | 45 | ```yml 46 | # serverless.yml 47 | 48 | distribution: 49 | component: '@serverless/aws-cloudfront' 50 | inputs: 51 | region: us-east-1 52 | enabled: true # optional 53 | comment: 'My distribution' # optional 54 | defaults: # optional 55 | ttl: 15 56 | allowedHttpMethods: ['HEAD', 'GET'] 57 | forward: # optional 58 | # array of header names, 'none' or 'all' 59 | headers: ['Accept', 'Accept-Language'] 60 | # array of cookie names, 'none' or 'all' 61 | cookies: ['my-cookie] 62 | queryString: true 63 | queryStringCacheKeys: ['queryKey'] 64 | viewerProtocolPolicy: 'https-only' # optional 65 | smoothStreaming: true # optional 66 | compress: true # optional 67 | fieldLevelEncryptionId: '123' # optional 68 | lambda@edge: # added to cloudfront default cache behavior 69 | viewer-request: arn:aws:lambda:us-east-1:123:function:myFunc:version 70 | origins: 71 | - https://my-bucket.s3.amazonaws.com 72 | ``` 73 | 74 | #### Custom cache behavior 75 | Custom cache behaviors support the same config parameters as the default cache behavior (see the example above). 76 | ```yml 77 | # serverless.yml 78 | 79 | distribution: 80 | component: '@serverless/aws-cloudfront' 81 | inputs: 82 | origins: 83 | - url: https://my-assets.com 84 | pathPatterns: 85 | /static/images: # route any /static/images requests to https://my-assets.com 86 | ttl: 10 87 | allowedHttpMethods: ['GET', 'HEAD'] # optional 88 | forward: # optional 89 | headers: 'all' 90 | cookies: ['auth-token'] 91 | queryString: true 92 | compress: false # optional 93 | # ... 94 | ``` 95 | 96 | #### Lambda@Edge 97 | 98 | ```yml 99 | # serverless.yml 100 | 101 | distribution: 102 | component: '@serverless/aws-cloudfront' 103 | inputs: 104 | origins: 105 | - url: https://sampleorigin.com 106 | pathPatterns: 107 | /sample/path: 108 | ttl: 10 109 | lambda@edge: 110 | viewer-request: arn:aws:lambda:us-east-1:123:function:myFunc:version # lambda ARN including version 111 | ``` 112 | 113 | #### Private S3 Content 114 | 115 | To restrict access to content that you serve from S3 you can mark as `private` your S3 origins: 116 | 117 | ```yml 118 | # serverless.yml 119 | 120 | distribution: 121 | component: '@serverless/aws-cloudfront' 122 | inputs: 123 | origins: 124 | - url: https://my-private-bucket.s3.amazonaws.com 125 | private: true 126 | ``` 127 | 128 | A bucket policy will be added that grants CloudFront with access to the bucket objects. Note that it doesn't remove any existing permissions on the bucket. If users currently have permission to access the files in your bucket using Amazon S3 URLs you will need to manually remove those. 129 | 130 | This is documented in more detail here: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html 131 | 132 | ### 4. Deploy 133 | 134 | ```console 135 | $ serverless 136 | ``` 137 | 138 |   139 | 140 | ### New to Components? 141 | 142 | Checkout the [Serverless Components](https://github.com/serverless/components) repo for more information. 143 | -------------------------------------------------------------------------------- /__tests__/__snapshots__/lambda-at-edge.test.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`Input origin as a custom url creates distribution with lambda associations for each event type 1`] = ` 4 | Object { 5 | "DistributionConfig": Object { 6 | "Aliases": Object { 7 | "Items": Array [], 8 | "Quantity": 0, 9 | }, 10 | "CacheBehaviors": Object { 11 | "Items": Array [ 12 | Object { 13 | "AllowedMethods": Object { 14 | "CachedMethods": Object { 15 | "Items": Array [ 16 | "GET", 17 | "HEAD", 18 | ], 19 | "Quantity": 2, 20 | }, 21 | "Items": Array [ 22 | "GET", 23 | "HEAD", 24 | ], 25 | "Quantity": 2, 26 | }, 27 | "Compress": true, 28 | "DefaultTTL": 10, 29 | "FieldLevelEncryptionId": "", 30 | "ForwardedValues": Object { 31 | "Cookies": Object { 32 | "Forward": "all", 33 | }, 34 | "Headers": Object { 35 | "Items": Array [], 36 | "Quantity": 0, 37 | }, 38 | "QueryString": true, 39 | "QueryStringCacheKeys": Object { 40 | "Items": Array [], 41 | "Quantity": 0, 42 | }, 43 | }, 44 | "LambdaFunctionAssociations": Object { 45 | "Items": Array [ 46 | Object { 47 | "EventType": "viewer-request", 48 | "IncludeBody": true, 49 | "LambdaFunctionARN": "arn:aws:lambda:us-east-1:123:function:viewerRequestFunction", 50 | }, 51 | Object { 52 | "EventType": "origin-request", 53 | "IncludeBody": true, 54 | "LambdaFunctionARN": "arn:aws:lambda:us-east-1:123:function:originRequestFunction", 55 | }, 56 | Object { 57 | "EventType": "origin-response", 58 | "IncludeBody": true, 59 | "LambdaFunctionARN": "arn:aws:lambda:us-east-1:123:function:originResponseFunction", 60 | }, 61 | Object { 62 | "EventType": "viewer-response", 63 | "IncludeBody": true, 64 | "LambdaFunctionARN": "arn:aws:lambda:us-east-1:123:function:viewerResponseFunction", 65 | }, 66 | ], 67 | "Quantity": 4, 68 | }, 69 | "MaxTTL": 10, 70 | "MinTTL": 10, 71 | "PathPattern": "/some/path", 72 | "SmoothStreaming": false, 73 | "TargetOriginId": "exampleorigin.com", 74 | "TrustedSigners": Object { 75 | "Enabled": false, 76 | "Quantity": 0, 77 | }, 78 | "ViewerProtocolPolicy": "https-only", 79 | }, 80 | ], 81 | "Quantity": 1, 82 | }, 83 | "CallerReference": "1566599541192", 84 | "Comment": "", 85 | "DefaultCacheBehavior": Object { 86 | "AllowedMethods": Object { 87 | "CachedMethods": Object { 88 | "Items": Array [ 89 | "HEAD", 90 | "GET", 91 | ], 92 | "Quantity": 2, 93 | }, 94 | "Items": Array [ 95 | "HEAD", 96 | "GET", 97 | ], 98 | "Quantity": 2, 99 | }, 100 | "Compress": false, 101 | "DefaultTTL": 86400, 102 | "FieldLevelEncryptionId": "", 103 | "ForwardedValues": Object { 104 | "Cookies": Object { 105 | "Forward": "none", 106 | }, 107 | "Headers": Object { 108 | "Items": Array [], 109 | "Quantity": 0, 110 | }, 111 | "QueryString": false, 112 | "QueryStringCacheKeys": Object { 113 | "Items": Array [], 114 | "Quantity": 0, 115 | }, 116 | }, 117 | "LambdaFunctionAssociations": Object { 118 | "Items": Array [], 119 | "Quantity": 0, 120 | }, 121 | "MaxTTL": 31536000, 122 | "MinTTL": 0, 123 | "SmoothStreaming": false, 124 | "TargetOriginId": "exampleorigin.com", 125 | "TrustedSigners": Object { 126 | "Enabled": false, 127 | "Items": Array [], 128 | "Quantity": 0, 129 | }, 130 | "ViewerProtocolPolicy": "redirect-to-https", 131 | }, 132 | "Enabled": true, 133 | "HttpVersion": "http2", 134 | "Origins": Object { 135 | "Items": Array [ 136 | Object { 137 | "CustomHeaders": Object { 138 | "Items": Array [], 139 | "Quantity": 0, 140 | }, 141 | "CustomOriginConfig": Object { 142 | "HTTPPort": 80, 143 | "HTTPSPort": 443, 144 | "OriginKeepaliveTimeout": 5, 145 | "OriginProtocolPolicy": "https-only", 146 | "OriginReadTimeout": 30, 147 | "OriginSslProtocols": Object { 148 | "Items": Array [ 149 | "TLSv1.2", 150 | ], 151 | "Quantity": 1, 152 | }, 153 | }, 154 | "DomainName": "exampleorigin.com", 155 | "Id": "exampleorigin.com", 156 | "OriginPath": "", 157 | }, 158 | ], 159 | "Quantity": 1, 160 | }, 161 | "PriceClass": "PriceClass_All", 162 | }, 163 | } 164 | `; 165 | -------------------------------------------------------------------------------- /__tests__/s3-origin.test.js: -------------------------------------------------------------------------------- 1 | const { 2 | mockCreateDistribution, 3 | mockUpdateDistribution, 4 | mockCreateDistributionPromise, 5 | mockGetDistributionConfigPromise, 6 | mockUpdateDistributionPromise, 7 | mockCreateCloudFrontOriginAccessIdentityPromise, 8 | mockPutBucketPolicy 9 | } = require('aws-sdk') 10 | 11 | const { createComponent, assertHasOrigin } = require('../test-utils') 12 | 13 | describe('S3 origins', () => { 14 | let component 15 | 16 | beforeEach(async () => { 17 | mockCreateDistributionPromise.mockResolvedValueOnce({ 18 | Distribution: { 19 | Id: 'distributionwithS3origin' 20 | } 21 | }) 22 | 23 | component = await createComponent() 24 | }) 25 | 26 | describe('When origin is an S3 bucket URL', () => { 27 | it('creates distribution', async () => { 28 | await component.default({ 29 | origins: ['https://mybucket.s3.amazonaws.com'] 30 | }) 31 | 32 | assertHasOrigin(mockCreateDistribution, { 33 | Id: 'mybucket', 34 | DomainName: 'mybucket.s3.amazonaws.com', 35 | S3OriginConfig: { 36 | OriginAccessIdentity: '' 37 | }, 38 | CustomHeaders: { 39 | Quantity: 0, 40 | Items: [] 41 | }, 42 | OriginPath: '' 43 | }) 44 | 45 | expect(mockCreateDistribution.mock.calls[0][0]).toMatchSnapshot() 46 | }) 47 | 48 | it('updates distribution', async () => { 49 | mockGetDistributionConfigPromise.mockResolvedValueOnce({ 50 | ETag: 'etag', 51 | DistributionConfig: { 52 | Origins: { 53 | Items: [] 54 | } 55 | } 56 | }) 57 | mockUpdateDistributionPromise.mockResolvedValueOnce({ 58 | Distribution: { 59 | Id: 'distributionwithS3originupdated' 60 | } 61 | }) 62 | 63 | await component.default({ 64 | origins: ['https://mybucket.s3.amazonaws.com'] 65 | }) 66 | 67 | await component.default({ 68 | origins: ['https://anotherbucket.s3.amazonaws.com'] 69 | }) 70 | 71 | assertHasOrigin(mockUpdateDistribution, { 72 | Id: 'anotherbucket', 73 | DomainName: 'anotherbucket.s3.amazonaws.com' 74 | }) 75 | 76 | expect(mockUpdateDistribution.mock.calls[0][0]).toMatchSnapshot() 77 | }) 78 | }) 79 | 80 | describe('When origin is an S3 URL only accessible via CloudFront', () => { 81 | it('creates distribution', async () => { 82 | mockCreateCloudFrontOriginAccessIdentityPromise.mockResolvedValueOnce({ 83 | CloudFrontOriginAccessIdentity: { 84 | Id: 'access-identity-xyz', 85 | S3CanonicalUserId: 's3-canonical-user-id-xyz' 86 | } 87 | }) 88 | 89 | await component.default({ 90 | origins: [ 91 | { 92 | url: 'https://mybucket.s3.amazonaws.com', 93 | private: true 94 | } 95 | ] 96 | }) 97 | 98 | expect(mockPutBucketPolicy).toBeCalledWith({ 99 | Bucket: 'mybucket', 100 | Policy: expect.stringContaining('"CanonicalUser":"s3-canonical-user-id-xyz"') 101 | }) 102 | 103 | assertHasOrigin(mockCreateDistribution, { 104 | Id: 'mybucket', 105 | DomainName: 'mybucket.s3.amazonaws.com', 106 | S3OriginConfig: { 107 | OriginAccessIdentity: 'origin-access-identity/cloudfront/access-identity-xyz' 108 | }, 109 | CustomHeaders: { 110 | Quantity: 0, 111 | Items: [] 112 | }, 113 | OriginPath: '' 114 | }) 115 | 116 | expect(mockCreateDistribution.mock.calls[0][0]).toMatchSnapshot() 117 | }) 118 | 119 | it('updates distribution', async () => { 120 | mockCreateCloudFrontOriginAccessIdentityPromise.mockResolvedValue({ 121 | CloudFrontOriginAccessIdentity: { 122 | Id: 'access-identity-xyz', 123 | S3CanonicalUserId: 's3-canonical-user-id-xyz' 124 | } 125 | }) 126 | 127 | mockGetDistributionConfigPromise.mockResolvedValueOnce({ 128 | ETag: 'etag', 129 | DistributionConfig: { 130 | Origins: { 131 | Items: [] 132 | } 133 | } 134 | }) 135 | 136 | mockUpdateDistributionPromise.mockResolvedValueOnce({ 137 | Distribution: { 138 | Id: 'distributionwithS3originupdated' 139 | } 140 | }) 141 | 142 | await component.default({ 143 | origins: [ 144 | { 145 | url: 'https://mybucket.s3.amazonaws.com', 146 | private: true 147 | } 148 | ] 149 | }) 150 | 151 | await component.default({ 152 | origins: [ 153 | { 154 | url: 'https://anotherbucket.s3.amazonaws.com', 155 | private: true 156 | } 157 | ] 158 | }) 159 | 160 | expect(mockPutBucketPolicy).toBeCalledWith({ 161 | Bucket: 'anotherbucket', 162 | Policy: expect.stringContaining('"CanonicalUser":"s3-canonical-user-id-xyz"') 163 | }) 164 | 165 | assertHasOrigin(mockUpdateDistribution, { 166 | Id: 'anotherbucket', 167 | DomainName: 'anotherbucket.s3.amazonaws.com', 168 | S3OriginConfig: { 169 | OriginAccessIdentity: 'origin-access-identity/cloudfront/access-identity-xyz' 170 | }, 171 | OriginPath: '', 172 | CustomHeaders: { Items: [], Quantity: 0 } 173 | }) 174 | 175 | expect(mockCreateDistribution.mock.calls[0][0]).toMatchSnapshot() 176 | }) 177 | }) 178 | }) 179 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | const parseInputOrigins = require('./parseInputOrigins') 2 | const getDefaultCacheBehavior = require('./getDefaultCacheBehavior') 3 | const createOriginAccessIdentity = require('./createOriginAccessIdentity') 4 | const grantCloudFrontBucketAccess = require('./grantCloudFrontBucketAccess') 5 | 6 | const servePrivateContentEnabled = (inputs) => 7 | inputs.origins.some((origin) => { 8 | return origin && origin.private === true 9 | }) 10 | 11 | const updateBucketsPolicies = async (s3, origins, s3CanonicalUserId) => { 12 | // update bucket policies with cloudfront access 13 | const bucketNames = origins.Items.filter((origin) => origin.S3OriginConfig).map( 14 | (origin) => origin.Id 15 | ) 16 | 17 | return Promise.all( 18 | bucketNames.map((bucketName) => grantCloudFrontBucketAccess(s3, bucketName, s3CanonicalUserId)) 19 | ) 20 | } 21 | 22 | const createCloudFrontDistribution = async (cf, s3, inputs) => { 23 | const params = { 24 | DistributionConfig: { 25 | CallerReference: String(Date.now()), 26 | Comment: inputs.comment, 27 | Aliases: { 28 | Quantity: 0, 29 | Items: [] 30 | }, 31 | Origins: { 32 | Quantity: 0, 33 | Items: [] 34 | }, 35 | PriceClass: 'PriceClass_All', 36 | Enabled: inputs.enabled, 37 | HttpVersion: 'http2' 38 | } 39 | } 40 | 41 | const distributionConfig = params.DistributionConfig 42 | 43 | let originAccessIdentityId 44 | let s3CanonicalUserId 45 | 46 | if (servePrivateContentEnabled(inputs)) { 47 | ;({ originAccessIdentityId, s3CanonicalUserId } = await createOriginAccessIdentity(cf)) 48 | } 49 | 50 | const { Origins, CacheBehaviors } = parseInputOrigins(inputs.origins, { originAccessIdentityId }) 51 | 52 | if (s3CanonicalUserId) { 53 | await updateBucketsPolicies(s3, Origins, s3CanonicalUserId) 54 | } 55 | 56 | distributionConfig.Origins = Origins 57 | 58 | // set first origin declared as the default cache behavior 59 | distributionConfig.DefaultCacheBehavior = getDefaultCacheBehavior( 60 | Origins.Items[0].Id, 61 | inputs.defaults 62 | ) 63 | 64 | if (CacheBehaviors) { 65 | distributionConfig.CacheBehaviors = CacheBehaviors 66 | } 67 | 68 | const res = await cf.createDistribution(params).promise() 69 | 70 | return { 71 | id: res.Distribution.Id, 72 | arn: res.Distribution.ARN, 73 | url: `https://${res.Distribution.DomainName}` 74 | } 75 | } 76 | 77 | const updateCloudFrontDistribution = async (cf, s3, distributionId, inputs) => { 78 | // Update logic is a bit weird... 79 | // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudFront.html#updateDistribution-property 80 | 81 | // 1. we gotta get the config first... 82 | // todo what if id does not exist? 83 | const params = await cf.getDistributionConfig({ Id: distributionId }).promise() 84 | 85 | // 2. then add this property 86 | params.IfMatch = params.ETag 87 | 88 | // 3. then delete this property 89 | delete params.ETag 90 | 91 | // 4. then set this property 92 | params.Id = distributionId 93 | 94 | // 5. then make our changes 95 | 96 | params.DistributionConfig.Enabled = inputs.enabled 97 | params.DistributionConfig.Comment = inputs.comment 98 | 99 | let s3CanonicalUserId 100 | let originAccessIdentityId 101 | 102 | if (servePrivateContentEnabled(inputs)) { 103 | // presumably it's ok to call create origin access identity again 104 | // aws api returns cached copy of what was previously created 105 | // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudFront.html#createCloudFrontOriginAccessIdentity-property 106 | ;({ originAccessIdentityId, s3CanonicalUserId } = await createOriginAccessIdentity(cf)) 107 | } 108 | 109 | const { Origins, CacheBehaviors } = parseInputOrigins(inputs.origins, { originAccessIdentityId }) 110 | 111 | if (s3CanonicalUserId) { 112 | await updateBucketsPolicies(s3, Origins, s3CanonicalUserId) 113 | } 114 | 115 | params.DistributionConfig.DefaultCacheBehavior = getDefaultCacheBehavior( 116 | Origins.Items[0].Id, 117 | inputs.defaults 118 | ) 119 | params.DistributionConfig.Origins = Origins 120 | 121 | if (CacheBehaviors) { 122 | params.DistributionConfig.CacheBehaviors = CacheBehaviors 123 | } 124 | 125 | // 6. then finally update! 126 | const res = await cf.updateDistribution(params).promise() 127 | 128 | return { 129 | id: res.Distribution.Id, 130 | arn: res.Distribution.ARN, 131 | url: `https://${res.Distribution.DomainName}` 132 | } 133 | } 134 | 135 | const disableCloudFrontDistribution = async (cf, distributionId) => { 136 | const params = await cf.getDistributionConfig({ Id: distributionId }).promise() 137 | 138 | params.IfMatch = params.ETag 139 | 140 | delete params.ETag 141 | 142 | params.Id = distributionId 143 | 144 | params.DistributionConfig.Enabled = false 145 | 146 | const res = await cf.updateDistribution(params).promise() 147 | 148 | return { 149 | id: res.Distribution.Id, 150 | arn: res.Distribution.ARN, 151 | url: `https://${res.Distribution.DomainName}` 152 | } 153 | } 154 | 155 | const deleteCloudFrontDistribution = async (cf, distributionId) => { 156 | try { 157 | const res = await cf.getDistributionConfig({ Id: distributionId }).promise() 158 | 159 | const params = { Id: distributionId, IfMatch: res.ETag } 160 | await cf.deleteDistribution(params).promise() 161 | } catch (e) { 162 | if (e.code === 'DistributionNotDisabled') { 163 | await disableCloudFrontDistribution(cf, distributionId) 164 | } else { 165 | throw e 166 | } 167 | } 168 | } 169 | 170 | module.exports = { 171 | createCloudFrontDistribution, 172 | updateCloudFrontDistribution, 173 | deleteCloudFrontDistribution 174 | } 175 | -------------------------------------------------------------------------------- /__tests__/__snapshots__/custom-url-origin.test.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`Input origin as a custom url creates distribution with custom url origin and sets defaults 1`] = ` 4 | Object { 5 | "DistributionConfig": Object { 6 | "Aliases": Object { 7 | "Items": Array [], 8 | "Quantity": 0, 9 | }, 10 | "CacheBehaviors": Object { 11 | "Items": Array [], 12 | "Quantity": 0, 13 | }, 14 | "CallerReference": "1566599541192", 15 | "Comment": "", 16 | "DefaultCacheBehavior": Object { 17 | "AllowedMethods": Object { 18 | "CachedMethods": Object { 19 | "Items": Array [ 20 | "HEAD", 21 | "GET", 22 | ], 23 | "Quantity": 2, 24 | }, 25 | "Items": Array [ 26 | "HEAD", 27 | "DELETE", 28 | "POST", 29 | "GET", 30 | "OPTIONS", 31 | "PUT", 32 | "PATCH", 33 | ], 34 | "Quantity": 7, 35 | }, 36 | "Compress": false, 37 | "DefaultTTL": 10, 38 | "FieldLevelEncryptionId": "", 39 | "ForwardedValues": Object { 40 | "Cookies": Object { 41 | "Forward": "none", 42 | }, 43 | "Headers": Object { 44 | "Items": Array [], 45 | "Quantity": 0, 46 | }, 47 | "QueryString": false, 48 | "QueryStringCacheKeys": Object { 49 | "Items": Array [], 50 | "Quantity": 0, 51 | }, 52 | }, 53 | "LambdaFunctionAssociations": Object { 54 | "Items": Array [ 55 | Object { 56 | "EventType": "origin-request", 57 | "IncludeBody": true, 58 | "LambdaFunctionARN": "arn:aws:lambda:us-east-1:123:function:originRequestFunction", 59 | }, 60 | ], 61 | "Quantity": 1, 62 | }, 63 | "MaxTTL": 31536000, 64 | "MinTTL": 0, 65 | "SmoothStreaming": false, 66 | "TargetOriginId": "mycustomorigin.com", 67 | "TrustedSigners": Object { 68 | "Enabled": false, 69 | "Items": Array [], 70 | "Quantity": 0, 71 | }, 72 | "ViewerProtocolPolicy": "redirect-to-https", 73 | }, 74 | "Enabled": true, 75 | "HttpVersion": "http2", 76 | "Origins": Object { 77 | "Items": Array [ 78 | Object { 79 | "CustomHeaders": Object { 80 | "Items": Array [], 81 | "Quantity": 0, 82 | }, 83 | "CustomOriginConfig": Object { 84 | "HTTPPort": 80, 85 | "HTTPSPort": 443, 86 | "OriginKeepaliveTimeout": 5, 87 | "OriginProtocolPolicy": "https-only", 88 | "OriginReadTimeout": 30, 89 | "OriginSslProtocols": Object { 90 | "Items": Array [ 91 | "TLSv1.2", 92 | ], 93 | "Quantity": 1, 94 | }, 95 | }, 96 | "DomainName": "mycustomorigin.com", 97 | "Id": "mycustomorigin.com", 98 | "OriginPath": "", 99 | }, 100 | ], 101 | "Quantity": 1, 102 | }, 103 | "PriceClass": "PriceClass_All", 104 | }, 105 | } 106 | `; 107 | 108 | exports[`Input origin as a custom url updates distribution 1`] = ` 109 | Object { 110 | "DistributionConfig": Object { 111 | "CacheBehaviors": Object { 112 | "Items": Array [], 113 | "Quantity": 0, 114 | }, 115 | "Comment": "", 116 | "DefaultCacheBehavior": Object { 117 | "AllowedMethods": Object { 118 | "CachedMethods": Object { 119 | "Items": Array [ 120 | "HEAD", 121 | "GET", 122 | ], 123 | "Quantity": 2, 124 | }, 125 | "Items": Array [ 126 | "HEAD", 127 | "GET", 128 | ], 129 | "Quantity": 2, 130 | }, 131 | "Compress": false, 132 | "DefaultTTL": 86400, 133 | "FieldLevelEncryptionId": "", 134 | "ForwardedValues": Object { 135 | "Cookies": Object { 136 | "Forward": "none", 137 | }, 138 | "Headers": Object { 139 | "Items": Array [], 140 | "Quantity": 0, 141 | }, 142 | "QueryString": false, 143 | "QueryStringCacheKeys": Object { 144 | "Items": Array [], 145 | "Quantity": 0, 146 | }, 147 | }, 148 | "LambdaFunctionAssociations": Object { 149 | "Items": Array [], 150 | "Quantity": 0, 151 | }, 152 | "MaxTTL": 31536000, 153 | "MinTTL": 0, 154 | "SmoothStreaming": false, 155 | "TargetOriginId": "mycustomoriginupdated.com", 156 | "TrustedSigners": Object { 157 | "Enabled": false, 158 | "Items": Array [], 159 | "Quantity": 0, 160 | }, 161 | "ViewerProtocolPolicy": "redirect-to-https", 162 | }, 163 | "Enabled": true, 164 | "Origins": Object { 165 | "Items": Array [ 166 | Object { 167 | "CustomHeaders": Object { 168 | "Items": Array [], 169 | "Quantity": 0, 170 | }, 171 | "CustomOriginConfig": Object { 172 | "HTTPPort": 80, 173 | "HTTPSPort": 443, 174 | "OriginKeepaliveTimeout": 5, 175 | "OriginProtocolPolicy": "https-only", 176 | "OriginReadTimeout": 30, 177 | "OriginSslProtocols": Object { 178 | "Items": Array [ 179 | "TLSv1.2", 180 | ], 181 | "Quantity": 1, 182 | }, 183 | }, 184 | "DomainName": "mycustomoriginupdated.com", 185 | "Id": "mycustomoriginupdated.com", 186 | "OriginPath": "", 187 | }, 188 | ], 189 | "Quantity": 1, 190 | }, 191 | }, 192 | "Id": "distribution123", 193 | "IfMatch": "etag", 194 | } 195 | `; 196 | -------------------------------------------------------------------------------- /__tests__/__snapshots__/cache-behavior-options.test.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`Input origin as a custom url creates distribution with custom behavior options 1`] = ` 4 | Object { 5 | "DistributionConfig": Object { 6 | "Aliases": Object { 7 | "Items": Array [], 8 | "Quantity": 0, 9 | }, 10 | "CacheBehaviors": Object { 11 | "Items": Array [ 12 | Object { 13 | "AllowedMethods": Object { 14 | "CachedMethods": Object { 15 | "Items": Array [ 16 | "GET", 17 | "HEAD", 18 | ], 19 | "Quantity": 2, 20 | }, 21 | "Items": Array [ 22 | "GET", 23 | "HEAD", 24 | ], 25 | "Quantity": 2, 26 | }, 27 | "Compress": false, 28 | "DefaultTTL": 0, 29 | "FieldLevelEncryptionId": "321", 30 | "ForwardedValues": Object { 31 | "Cookies": Object { 32 | "Forward": "whitelist", 33 | "WhitelistedNames": Object { 34 | "Items": Array [ 35 | "auth-token", 36 | ], 37 | "Quantity": 1, 38 | }, 39 | }, 40 | "Headers": Object { 41 | "Items": Array [ 42 | "*", 43 | ], 44 | "Quantity": 1, 45 | }, 46 | "QueryString": true, 47 | "QueryStringCacheKeys": Object { 48 | "Items": Array [], 49 | "Quantity": 0, 50 | }, 51 | }, 52 | "LambdaFunctionAssociations": Object { 53 | "Items": Array [], 54 | "Quantity": 0, 55 | }, 56 | "MaxTTL": 0, 57 | "MinTTL": 0, 58 | "PathPattern": "/sample/path", 59 | "SmoothStreaming": false, 60 | "TargetOriginId": "mycustomorigin.com", 61 | "TrustedSigners": Object { 62 | "Enabled": false, 63 | "Quantity": 0, 64 | }, 65 | "ViewerProtocolPolicy": "redirect-to-https", 66 | }, 67 | ], 68 | "Quantity": 1, 69 | }, 70 | "CallerReference": "1566599541192", 71 | "Comment": "", 72 | "DefaultCacheBehavior": Object { 73 | "AllowedMethods": Object { 74 | "CachedMethods": Object { 75 | "Items": Array [ 76 | "HEAD", 77 | "GET", 78 | ], 79 | "Quantity": 2, 80 | }, 81 | "Items": Array [ 82 | "HEAD", 83 | "GET", 84 | ], 85 | "Quantity": 2, 86 | }, 87 | "Compress": false, 88 | "DefaultTTL": 0, 89 | "FieldLevelEncryptionId": "", 90 | "ForwardedValues": Object { 91 | "Cookies": Object { 92 | "Forward": "none", 93 | }, 94 | "Headers": Object { 95 | "Items": Array [], 96 | "Quantity": 0, 97 | }, 98 | "QueryString": false, 99 | "QueryStringCacheKeys": Object { 100 | "Items": Array [], 101 | "Quantity": 0, 102 | }, 103 | }, 104 | "LambdaFunctionAssociations": Object { 105 | "Items": Array [], 106 | "Quantity": 0, 107 | }, 108 | "MaxTTL": 31536000, 109 | "MinTTL": 0, 110 | "SmoothStreaming": false, 111 | "TargetOriginId": "mycustomorigin.com", 112 | "TrustedSigners": Object { 113 | "Enabled": false, 114 | "Items": Array [], 115 | "Quantity": 0, 116 | }, 117 | "ViewerProtocolPolicy": "redirect-to-https", 118 | }, 119 | "Enabled": true, 120 | "HttpVersion": "http2", 121 | "Origins": Object { 122 | "Items": Array [ 123 | Object { 124 | "CustomHeaders": Object { 125 | "Items": Array [], 126 | "Quantity": 0, 127 | }, 128 | "CustomOriginConfig": Object { 129 | "HTTPPort": 80, 130 | "HTTPSPort": 443, 131 | "OriginKeepaliveTimeout": 5, 132 | "OriginProtocolPolicy": "https-only", 133 | "OriginReadTimeout": 30, 134 | "OriginSslProtocols": Object { 135 | "Items": Array [ 136 | "TLSv1.2", 137 | ], 138 | "Quantity": 1, 139 | }, 140 | }, 141 | "DomainName": "mycustomorigin.com", 142 | "Id": "mycustomorigin.com", 143 | "OriginPath": "", 144 | }, 145 | ], 146 | "Quantity": 1, 147 | }, 148 | "PriceClass": "PriceClass_All", 149 | }, 150 | } 151 | `; 152 | 153 | exports[`Input origin as a custom url creates distribution with custom default behavior options 1`] = ` 154 | Object { 155 | "DistributionConfig": Object { 156 | "Aliases": Object { 157 | "Items": Array [], 158 | "Quantity": 0, 159 | }, 160 | "CacheBehaviors": Object { 161 | "Items": Array [], 162 | "Quantity": 0, 163 | }, 164 | "CallerReference": "1566599541192", 165 | "Comment": "", 166 | "DefaultCacheBehavior": Object { 167 | "AllowedMethods": Object { 168 | "CachedMethods": Object { 169 | "Items": Array [ 170 | "HEAD", 171 | "GET", 172 | ], 173 | "Quantity": 2, 174 | }, 175 | "Items": Array [ 176 | "GET", 177 | "HEAD", 178 | "OPTIONS", 179 | "PUT", 180 | "POST", 181 | "PATCH", 182 | "DELETE", 183 | ], 184 | "Quantity": 7, 185 | }, 186 | "Compress": true, 187 | "DefaultTTL": 0, 188 | "FieldLevelEncryptionId": "123", 189 | "ForwardedValues": Object { 190 | "Cookies": Object { 191 | "Forward": "all", 192 | }, 193 | "Headers": Object { 194 | "Items": Array [ 195 | "Accept", 196 | "Accept-Language", 197 | ], 198 | "Quantity": 2, 199 | }, 200 | "QueryString": true, 201 | "QueryStringCacheKeys": Object { 202 | "Items": Array [ 203 | "query", 204 | ], 205 | "Quantity": 1, 206 | }, 207 | }, 208 | "LambdaFunctionAssociations": Object { 209 | "Items": Array [], 210 | "Quantity": 0, 211 | }, 212 | "MaxTTL": 31536000, 213 | "MinTTL": 0, 214 | "SmoothStreaming": true, 215 | "TargetOriginId": "mycustomorigin.com", 216 | "TrustedSigners": Object { 217 | "Enabled": false, 218 | "Items": Array [], 219 | "Quantity": 0, 220 | }, 221 | "ViewerProtocolPolicy": "https-only", 222 | }, 223 | "Enabled": true, 224 | "HttpVersion": "http2", 225 | "Origins": Object { 226 | "Items": Array [ 227 | Object { 228 | "CustomHeaders": Object { 229 | "Items": Array [], 230 | "Quantity": 0, 231 | }, 232 | "CustomOriginConfig": Object { 233 | "HTTPPort": 80, 234 | "HTTPSPort": 443, 235 | "OriginKeepaliveTimeout": 5, 236 | "OriginProtocolPolicy": "https-only", 237 | "OriginReadTimeout": 30, 238 | "OriginSslProtocols": Object { 239 | "Items": Array [ 240 | "TLSv1.2", 241 | ], 242 | "Quantity": 1, 243 | }, 244 | }, 245 | "DomainName": "mycustomorigin.com", 246 | "Id": "mycustomorigin.com", 247 | "OriginPath": "", 248 | }, 249 | ], 250 | "Quantity": 1, 251 | }, 252 | "PriceClass": "PriceClass_All", 253 | }, 254 | } 255 | `; 256 | -------------------------------------------------------------------------------- /__tests__/__snapshots__/origin-with-path-pattern.test.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`Input origin with path pattern creates distribution with custom url origin 1`] = ` 4 | Object { 5 | "DistributionConfig": Object { 6 | "Aliases": Object { 7 | "Items": Array [], 8 | "Quantity": 0, 9 | }, 10 | "CacheBehaviors": Object { 11 | "Items": Array [ 12 | Object { 13 | "AllowedMethods": Object { 14 | "CachedMethods": Object { 15 | "Items": Array [ 16 | "GET", 17 | "HEAD", 18 | ], 19 | "Quantity": 2, 20 | }, 21 | "Items": Array [ 22 | "GET", 23 | "HEAD", 24 | "POST", 25 | ], 26 | "Quantity": 3, 27 | }, 28 | "Compress": true, 29 | "DefaultTTL": 10, 30 | "FieldLevelEncryptionId": "", 31 | "ForwardedValues": Object { 32 | "Cookies": Object { 33 | "Forward": "all", 34 | }, 35 | "Headers": Object { 36 | "Items": Array [], 37 | "Quantity": 0, 38 | }, 39 | "QueryString": true, 40 | "QueryStringCacheKeys": Object { 41 | "Items": Array [], 42 | "Quantity": 0, 43 | }, 44 | }, 45 | "LambdaFunctionAssociations": Object { 46 | "Items": Array [], 47 | "Quantity": 0, 48 | }, 49 | "MaxTTL": 10, 50 | "MinTTL": 10, 51 | "PathPattern": "/some/path", 52 | "SmoothStreaming": false, 53 | "TargetOriginId": "exampleorigin.com", 54 | "TrustedSigners": Object { 55 | "Enabled": false, 56 | "Quantity": 0, 57 | }, 58 | "ViewerProtocolPolicy": "https-only", 59 | }, 60 | ], 61 | "Quantity": 1, 62 | }, 63 | "CallerReference": "1566599541192", 64 | "Comment": "", 65 | "DefaultCacheBehavior": Object { 66 | "AllowedMethods": Object { 67 | "CachedMethods": Object { 68 | "Items": Array [ 69 | "HEAD", 70 | "GET", 71 | ], 72 | "Quantity": 2, 73 | }, 74 | "Items": Array [ 75 | "HEAD", 76 | "GET", 77 | ], 78 | "Quantity": 2, 79 | }, 80 | "Compress": false, 81 | "DefaultTTL": 86400, 82 | "FieldLevelEncryptionId": "", 83 | "ForwardedValues": Object { 84 | "Cookies": Object { 85 | "Forward": "none", 86 | }, 87 | "Headers": Object { 88 | "Items": Array [], 89 | "Quantity": 0, 90 | }, 91 | "QueryString": false, 92 | "QueryStringCacheKeys": Object { 93 | "Items": Array [], 94 | "Quantity": 0, 95 | }, 96 | }, 97 | "LambdaFunctionAssociations": Object { 98 | "Items": Array [], 99 | "Quantity": 0, 100 | }, 101 | "MaxTTL": 31536000, 102 | "MinTTL": 0, 103 | "SmoothStreaming": false, 104 | "TargetOriginId": "exampleorigin.com", 105 | "TrustedSigners": Object { 106 | "Enabled": false, 107 | "Items": Array [], 108 | "Quantity": 0, 109 | }, 110 | "ViewerProtocolPolicy": "redirect-to-https", 111 | }, 112 | "Enabled": true, 113 | "HttpVersion": "http2", 114 | "Origins": Object { 115 | "Items": Array [ 116 | Object { 117 | "CustomHeaders": Object { 118 | "Items": Array [], 119 | "Quantity": 0, 120 | }, 121 | "CustomOriginConfig": Object { 122 | "HTTPPort": 80, 123 | "HTTPSPort": 443, 124 | "OriginKeepaliveTimeout": 5, 125 | "OriginProtocolPolicy": "https-only", 126 | "OriginReadTimeout": 30, 127 | "OriginSslProtocols": Object { 128 | "Items": Array [ 129 | "TLSv1.2", 130 | ], 131 | "Quantity": 1, 132 | }, 133 | }, 134 | "DomainName": "exampleorigin.com", 135 | "Id": "exampleorigin.com", 136 | "OriginPath": "", 137 | }, 138 | ], 139 | "Quantity": 1, 140 | }, 141 | "PriceClass": "PriceClass_All", 142 | }, 143 | } 144 | `; 145 | 146 | exports[`Input origin with path pattern updates distribution 1`] = ` 147 | Object { 148 | "DistributionConfig": Object { 149 | "CacheBehaviors": Object { 150 | "Items": Array [ 151 | Object { 152 | "AllowedMethods": Object { 153 | "CachedMethods": Object { 154 | "Items": Array [ 155 | "GET", 156 | "HEAD", 157 | ], 158 | "Quantity": 2, 159 | }, 160 | "Items": Array [ 161 | "GET", 162 | "HEAD", 163 | ], 164 | "Quantity": 2, 165 | }, 166 | "Compress": true, 167 | "DefaultTTL": 10, 168 | "FieldLevelEncryptionId": "", 169 | "ForwardedValues": Object { 170 | "Cookies": Object { 171 | "Forward": "all", 172 | }, 173 | "Headers": Object { 174 | "Items": Array [], 175 | "Quantity": 0, 176 | }, 177 | "QueryString": true, 178 | "QueryStringCacheKeys": Object { 179 | "Items": Array [], 180 | "Quantity": 0, 181 | }, 182 | }, 183 | "LambdaFunctionAssociations": Object { 184 | "Items": Array [], 185 | "Quantity": 0, 186 | }, 187 | "MaxTTL": 10, 188 | "MinTTL": 10, 189 | "PathPattern": "/some/other/path", 190 | "SmoothStreaming": false, 191 | "TargetOriginId": "exampleorigin.com", 192 | "TrustedSigners": Object { 193 | "Enabled": false, 194 | "Quantity": 0, 195 | }, 196 | "ViewerProtocolPolicy": "https-only", 197 | }, 198 | ], 199 | "Quantity": 1, 200 | }, 201 | "Comment": "", 202 | "DefaultCacheBehavior": Object { 203 | "AllowedMethods": Object { 204 | "CachedMethods": Object { 205 | "Items": Array [ 206 | "HEAD", 207 | "GET", 208 | ], 209 | "Quantity": 2, 210 | }, 211 | "Items": Array [ 212 | "HEAD", 213 | "GET", 214 | ], 215 | "Quantity": 2, 216 | }, 217 | "Compress": false, 218 | "DefaultTTL": 86400, 219 | "FieldLevelEncryptionId": "", 220 | "ForwardedValues": Object { 221 | "Cookies": Object { 222 | "Forward": "none", 223 | }, 224 | "Headers": Object { 225 | "Items": Array [], 226 | "Quantity": 0, 227 | }, 228 | "QueryString": false, 229 | "QueryStringCacheKeys": Object { 230 | "Items": Array [], 231 | "Quantity": 0, 232 | }, 233 | }, 234 | "LambdaFunctionAssociations": Object { 235 | "Items": Array [], 236 | "Quantity": 0, 237 | }, 238 | "MaxTTL": 31536000, 239 | "MinTTL": 0, 240 | "SmoothStreaming": false, 241 | "TargetOriginId": "exampleorigin.com", 242 | "TrustedSigners": Object { 243 | "Enabled": false, 244 | "Items": Array [], 245 | "Quantity": 0, 246 | }, 247 | "ViewerProtocolPolicy": "redirect-to-https", 248 | }, 249 | "Enabled": true, 250 | "Origins": Object { 251 | "Items": Array [ 252 | Object { 253 | "CustomHeaders": Object { 254 | "Items": Array [], 255 | "Quantity": 0, 256 | }, 257 | "CustomOriginConfig": Object { 258 | "HTTPPort": 80, 259 | "HTTPSPort": 443, 260 | "OriginKeepaliveTimeout": 5, 261 | "OriginProtocolPolicy": "https-only", 262 | "OriginReadTimeout": 30, 263 | "OriginSslProtocols": Object { 264 | "Items": Array [ 265 | "TLSv1.2", 266 | ], 267 | "Quantity": 1, 268 | }, 269 | }, 270 | "DomainName": "exampleorigin.com", 271 | "Id": "exampleorigin.com", 272 | "OriginPath": "", 273 | }, 274 | ], 275 | "Quantity": 1, 276 | }, 277 | }, 278 | "Id": "xyz", 279 | "IfMatch": "etag", 280 | } 281 | `; 282 | -------------------------------------------------------------------------------- /__tests__/__snapshots__/s3-origin.test.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`S3 origins When origin is an S3 URL only accessible via CloudFront creates distribution 1`] = ` 4 | Object { 5 | "DistributionConfig": Object { 6 | "Aliases": Object { 7 | "Items": Array [], 8 | "Quantity": 0, 9 | }, 10 | "CacheBehaviors": Object { 11 | "Items": Array [], 12 | "Quantity": 0, 13 | }, 14 | "CallerReference": "1566599541192", 15 | "Comment": "", 16 | "DefaultCacheBehavior": Object { 17 | "AllowedMethods": Object { 18 | "CachedMethods": Object { 19 | "Items": Array [ 20 | "HEAD", 21 | "GET", 22 | ], 23 | "Quantity": 2, 24 | }, 25 | "Items": Array [ 26 | "HEAD", 27 | "GET", 28 | ], 29 | "Quantity": 2, 30 | }, 31 | "Compress": false, 32 | "DefaultTTL": 86400, 33 | "FieldLevelEncryptionId": "", 34 | "ForwardedValues": Object { 35 | "Cookies": Object { 36 | "Forward": "none", 37 | }, 38 | "Headers": Object { 39 | "Items": Array [], 40 | "Quantity": 0, 41 | }, 42 | "QueryString": false, 43 | "QueryStringCacheKeys": Object { 44 | "Items": Array [], 45 | "Quantity": 0, 46 | }, 47 | }, 48 | "LambdaFunctionAssociations": Object { 49 | "Items": Array [], 50 | "Quantity": 0, 51 | }, 52 | "MaxTTL": 31536000, 53 | "MinTTL": 0, 54 | "SmoothStreaming": false, 55 | "TargetOriginId": "mybucket", 56 | "TrustedSigners": Object { 57 | "Enabled": false, 58 | "Items": Array [], 59 | "Quantity": 0, 60 | }, 61 | "ViewerProtocolPolicy": "redirect-to-https", 62 | }, 63 | "Enabled": true, 64 | "HttpVersion": "http2", 65 | "Origins": Object { 66 | "Items": Array [ 67 | Object { 68 | "CustomHeaders": Object { 69 | "Items": Array [], 70 | "Quantity": 0, 71 | }, 72 | "DomainName": "mybucket.s3.amazonaws.com", 73 | "Id": "mybucket", 74 | "OriginPath": "", 75 | "S3OriginConfig": Object { 76 | "OriginAccessIdentity": "origin-access-identity/cloudfront/access-identity-xyz", 77 | }, 78 | }, 79 | ], 80 | "Quantity": 1, 81 | }, 82 | "PriceClass": "PriceClass_All", 83 | }, 84 | } 85 | `; 86 | 87 | exports[`S3 origins When origin is an S3 URL only accessible via CloudFront updates distribution 1`] = ` 88 | Object { 89 | "DistributionConfig": Object { 90 | "Aliases": Object { 91 | "Items": Array [], 92 | "Quantity": 0, 93 | }, 94 | "CacheBehaviors": Object { 95 | "Items": Array [], 96 | "Quantity": 0, 97 | }, 98 | "CallerReference": "1566599541192", 99 | "Comment": "", 100 | "DefaultCacheBehavior": Object { 101 | "AllowedMethods": Object { 102 | "CachedMethods": Object { 103 | "Items": Array [ 104 | "HEAD", 105 | "GET", 106 | ], 107 | "Quantity": 2, 108 | }, 109 | "Items": Array [ 110 | "HEAD", 111 | "GET", 112 | ], 113 | "Quantity": 2, 114 | }, 115 | "Compress": false, 116 | "DefaultTTL": 86400, 117 | "FieldLevelEncryptionId": "", 118 | "ForwardedValues": Object { 119 | "Cookies": Object { 120 | "Forward": "none", 121 | }, 122 | "Headers": Object { 123 | "Items": Array [], 124 | "Quantity": 0, 125 | }, 126 | "QueryString": false, 127 | "QueryStringCacheKeys": Object { 128 | "Items": Array [], 129 | "Quantity": 0, 130 | }, 131 | }, 132 | "LambdaFunctionAssociations": Object { 133 | "Items": Array [], 134 | "Quantity": 0, 135 | }, 136 | "MaxTTL": 31536000, 137 | "MinTTL": 0, 138 | "SmoothStreaming": false, 139 | "TargetOriginId": "mybucket", 140 | "TrustedSigners": Object { 141 | "Enabled": false, 142 | "Items": Array [], 143 | "Quantity": 0, 144 | }, 145 | "ViewerProtocolPolicy": "redirect-to-https", 146 | }, 147 | "Enabled": true, 148 | "HttpVersion": "http2", 149 | "Origins": Object { 150 | "Items": Array [ 151 | Object { 152 | "CustomHeaders": Object { 153 | "Items": Array [], 154 | "Quantity": 0, 155 | }, 156 | "DomainName": "mybucket.s3.amazonaws.com", 157 | "Id": "mybucket", 158 | "OriginPath": "", 159 | "S3OriginConfig": Object { 160 | "OriginAccessIdentity": "origin-access-identity/cloudfront/access-identity-xyz", 161 | }, 162 | }, 163 | ], 164 | "Quantity": 1, 165 | }, 166 | "PriceClass": "PriceClass_All", 167 | }, 168 | } 169 | `; 170 | 171 | exports[`S3 origins When origin is an S3 bucket URL creates distribution 1`] = ` 172 | Object { 173 | "DistributionConfig": Object { 174 | "Aliases": Object { 175 | "Items": Array [], 176 | "Quantity": 0, 177 | }, 178 | "CacheBehaviors": Object { 179 | "Items": Array [], 180 | "Quantity": 0, 181 | }, 182 | "CallerReference": "1566599541192", 183 | "Comment": "", 184 | "DefaultCacheBehavior": Object { 185 | "AllowedMethods": Object { 186 | "CachedMethods": Object { 187 | "Items": Array [ 188 | "HEAD", 189 | "GET", 190 | ], 191 | "Quantity": 2, 192 | }, 193 | "Items": Array [ 194 | "HEAD", 195 | "GET", 196 | ], 197 | "Quantity": 2, 198 | }, 199 | "Compress": false, 200 | "DefaultTTL": 86400, 201 | "FieldLevelEncryptionId": "", 202 | "ForwardedValues": Object { 203 | "Cookies": Object { 204 | "Forward": "none", 205 | }, 206 | "Headers": Object { 207 | "Items": Array [], 208 | "Quantity": 0, 209 | }, 210 | "QueryString": false, 211 | "QueryStringCacheKeys": Object { 212 | "Items": Array [], 213 | "Quantity": 0, 214 | }, 215 | }, 216 | "LambdaFunctionAssociations": Object { 217 | "Items": Array [], 218 | "Quantity": 0, 219 | }, 220 | "MaxTTL": 31536000, 221 | "MinTTL": 0, 222 | "SmoothStreaming": false, 223 | "TargetOriginId": "mybucket", 224 | "TrustedSigners": Object { 225 | "Enabled": false, 226 | "Items": Array [], 227 | "Quantity": 0, 228 | }, 229 | "ViewerProtocolPolicy": "redirect-to-https", 230 | }, 231 | "Enabled": true, 232 | "HttpVersion": "http2", 233 | "Origins": Object { 234 | "Items": Array [ 235 | Object { 236 | "CustomHeaders": Object { 237 | "Items": Array [], 238 | "Quantity": 0, 239 | }, 240 | "DomainName": "mybucket.s3.amazonaws.com", 241 | "Id": "mybucket", 242 | "OriginPath": "", 243 | "S3OriginConfig": Object { 244 | "OriginAccessIdentity": "", 245 | }, 246 | }, 247 | ], 248 | "Quantity": 1, 249 | }, 250 | "PriceClass": "PriceClass_All", 251 | }, 252 | } 253 | `; 254 | 255 | exports[`S3 origins When origin is an S3 bucket URL updates distribution 1`] = ` 256 | Object { 257 | "DistributionConfig": Object { 258 | "CacheBehaviors": Object { 259 | "Items": Array [], 260 | "Quantity": 0, 261 | }, 262 | "Comment": "", 263 | "DefaultCacheBehavior": Object { 264 | "AllowedMethods": Object { 265 | "CachedMethods": Object { 266 | "Items": Array [ 267 | "HEAD", 268 | "GET", 269 | ], 270 | "Quantity": 2, 271 | }, 272 | "Items": Array [ 273 | "HEAD", 274 | "GET", 275 | ], 276 | "Quantity": 2, 277 | }, 278 | "Compress": false, 279 | "DefaultTTL": 86400, 280 | "FieldLevelEncryptionId": "", 281 | "ForwardedValues": Object { 282 | "Cookies": Object { 283 | "Forward": "none", 284 | }, 285 | "Headers": Object { 286 | "Items": Array [], 287 | "Quantity": 0, 288 | }, 289 | "QueryString": false, 290 | "QueryStringCacheKeys": Object { 291 | "Items": Array [], 292 | "Quantity": 0, 293 | }, 294 | }, 295 | "LambdaFunctionAssociations": Object { 296 | "Items": Array [], 297 | "Quantity": 0, 298 | }, 299 | "MaxTTL": 31536000, 300 | "MinTTL": 0, 301 | "SmoothStreaming": false, 302 | "TargetOriginId": "anotherbucket", 303 | "TrustedSigners": Object { 304 | "Enabled": false, 305 | "Items": Array [], 306 | "Quantity": 0, 307 | }, 308 | "ViewerProtocolPolicy": "redirect-to-https", 309 | }, 310 | "Enabled": true, 311 | "Origins": Object { 312 | "Items": Array [ 313 | Object { 314 | "CustomHeaders": Object { 315 | "Items": Array [], 316 | "Quantity": 0, 317 | }, 318 | "DomainName": "anotherbucket.s3.amazonaws.com", 319 | "Id": "anotherbucket", 320 | "OriginPath": "", 321 | "S3OriginConfig": Object { 322 | "OriginAccessIdentity": "", 323 | }, 324 | }, 325 | ], 326 | "Quantity": 1, 327 | }, 328 | }, 329 | "Id": "distributionwithS3origin", 330 | "IfMatch": "etag", 331 | } 332 | `; 333 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | https://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | Copyright 2020 Serverless, Inc. 179 | 180 | Licensed under the Apache License, Version 2.0 (the "License"); 181 | you may not use this file except in compliance with the License. 182 | You may obtain a copy of the License at 183 | 184 | https://www.apache.org/licenses/LICENSE-2.0 185 | 186 | Unless required by applicable law or agreed to in writing, software 187 | distributed under the License is distributed on an "AS IS" BASIS, 188 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 189 | See the License for the specific language governing permissions and 190 | limitations under the License. 191 | --------------------------------------------------------------------------------