@serverless/domain
Advanced tools
Comparing version 5.0.0 to 6.0.0
{ | ||
"name": "@serverless/domain", | ||
"version": "5.0.0", | ||
"version": "6.0.0", | ||
"main": "./serverless.js", | ||
@@ -5,0 +5,0 @@ "publishConfig": { |
@@ -21,3 +21,5 @@ const { Component } = require('@serverless/core') | ||
addDomainToCloudfrontDistribution, | ||
removeDomainFromCloudFrontDistribution | ||
removeDomainFromCloudFrontDistribution, | ||
createCloudfrontDistributionForAppSync, | ||
updateCloudfrontDistributionForAppSync | ||
} = require('./utils') | ||
@@ -160,2 +162,39 @@ | ||
) | ||
} else if (subdomain.type === 'awsAppSync') { | ||
this.context.debug(`Configuring domain "${subdomain.domain}" for AppSync API`) | ||
this.context.debug(`Checking CloudFront distribution for domain "${subdomain.domain}"`) | ||
let distribution = await getCloudFrontDistributionByDomain(clients.cf, subdomain.domain) | ||
if (!distribution) { | ||
this.context.debug( | ||
`CloudFront distribution for domain "${subdomain.domain}" not found. Creating...` | ||
) | ||
distribution = await createCloudfrontDistributionForAppSync( | ||
clients.cf, | ||
subdomain, | ||
certificate.CertificateArn | ||
) | ||
} else if (!distribution.origins.includes(subdomain.url)) { | ||
this.context.debug(`Updating distribution "${distribution.url}".`) | ||
distribution = await updateCloudfrontDistributionForAppSync( | ||
clients.cf, | ||
subdomain, | ||
distribution.id | ||
) | ||
} | ||
this.context.debug(`Configuring DNS for distribution "${distribution.url}".`) | ||
await configureDnsForCloudFrontDistribution( | ||
clients.route53, | ||
subdomain, | ||
domainHostedZoneId, | ||
distribution.url | ||
) | ||
this.context.debug(`Invalidating CloudFront distribution ${distribution.url}`) | ||
await invalidateCloudfrontDistribution(clients.cf, distribution.id) | ||
this.context.debug(`Using AWS Cloudfront Distribution with URL: "${subdomain.domain}"`) | ||
} | ||
@@ -162,0 +201,0 @@ |
205
utils.js
@@ -64,2 +64,11 @@ const aws = require('aws-sdk') | ||
// Check if referenced Component is using AppSync... | ||
if (inputs.subdomains[subdomain].url.includes('appsync')) { | ||
domainObj.apiId = inputs.subdomains[subdomain].url.split('.')[0].split('//')[1] | ||
domainObj.url = inputs.subdomains[subdomain].url | ||
.replace('https://', '') // distribution origin does not expect https | ||
.replace('/graphql', '') // distribution lrigin does not expect /graphql | ||
domainObj.type = 'awsAppSync' | ||
} | ||
if (inputs.subdomains[subdomain].url.includes('cloudfront')) { | ||
@@ -454,2 +463,132 @@ domainObj.distributionId = inputs.subdomains[subdomain].id | ||
const createCloudfrontDistributionForAppSync = async (cf, subdomain, certificateArn) => { | ||
const params = { | ||
DistributionConfig: { | ||
CallerReference: String(Date.now()), | ||
Aliases: { | ||
Quantity: 1, | ||
Items: [subdomain.domain] | ||
}, | ||
DefaultRootObject: 'index.html', | ||
Origins: { | ||
Quantity: 1, | ||
Items: [ | ||
{ | ||
Id: `app-sync-${subdomain.apiId}`, | ||
DomainName: subdomain.url, | ||
OriginPath: '', | ||
CustomHeaders: { | ||
Quantity: 0, | ||
Items: [] | ||
}, | ||
S3OriginConfig: { | ||
OriginAccessIdentity: '' | ||
} | ||
} | ||
] | ||
}, | ||
OriginGroups: { | ||
Quantity: 0, | ||
Items: [] | ||
}, | ||
DefaultCacheBehavior: { | ||
TargetOriginId: `app-sync-${subdomain.apiId}`, | ||
ForwardedValues: { | ||
QueryString: false, | ||
Cookies: { | ||
Forward: 'none' | ||
}, | ||
Headers: { | ||
Quantity: 0, | ||
Items: [] | ||
}, | ||
QueryStringCacheKeys: { | ||
Quantity: 0, | ||
Items: [] | ||
} | ||
}, | ||
TrustedSigners: { | ||
Enabled: false, | ||
Quantity: 0, | ||
Items: [] | ||
}, | ||
ViewerProtocolPolicy: 'redirect-to-https', | ||
MinTTL: 0, | ||
AllowedMethods: { | ||
Quantity: 2, | ||
Items: ['HEAD', 'GET'], | ||
CachedMethods: { | ||
Quantity: 2, | ||
Items: ['HEAD', 'GET'] | ||
} | ||
}, | ||
SmoothStreaming: false, | ||
DefaultTTL: 86400, | ||
MaxTTL: 31536000, | ||
Compress: false, | ||
LambdaFunctionAssociations: { | ||
Quantity: 0, | ||
Items: [] | ||
}, | ||
FieldLevelEncryptionId: '' | ||
}, | ||
CacheBehaviors: { | ||
Quantity: 0, | ||
Items: [] | ||
}, | ||
CustomErrorResponses: { | ||
Quantity: 2, | ||
Items: [ | ||
{ | ||
ErrorCode: 404, | ||
ErrorCachingMinTTL: 300, | ||
ResponseCode: '200', | ||
ResponsePagePath: '/index.html' | ||
}, | ||
{ | ||
ErrorCode: 403, | ||
ErrorCachingMinTTL: 300, | ||
ResponseCode: '200', | ||
ResponsePagePath: '/index.html' | ||
} | ||
] | ||
}, | ||
Comment: '', | ||
Logging: { | ||
Enabled: false, | ||
IncludeCookies: false, | ||
Bucket: '', | ||
Prefix: '' | ||
}, | ||
PriceClass: 'PriceClass_All', | ||
Enabled: true, | ||
ViewerCertificate: { | ||
ACMCertificateArn: certificateArn, | ||
SSLSupportMethod: 'sni-only', | ||
MinimumProtocolVersion: 'TLSv1.1_2016', | ||
Certificate: certificateArn, | ||
CertificateSource: 'acm' | ||
}, | ||
Restrictions: { | ||
GeoRestriction: { | ||
RestrictionType: 'none', | ||
Quantity: 0, | ||
Items: [] | ||
} | ||
}, | ||
WebACLId: '', | ||
HttpVersion: 'http2', | ||
IsIPV6Enabled: true | ||
} | ||
} | ||
const res = await cf.createDistribution(params).promise() | ||
return { | ||
id: res.Distribution.Id, | ||
arn: res.Distribution.ARN, | ||
url: res.Distribution.DomainName | ||
} | ||
} | ||
/** | ||
@@ -594,2 +733,64 @@ * Create Cloudfront Distribution | ||
const updateCloudfrontDistributionForAppSync = async (cf, subdomain, distributionId) => { | ||
// Update logic is a bit weird... | ||
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudFront.html#updateDistribution-property | ||
// 1. we gotta get the config first... | ||
const params = await cf.getDistributionConfig({ Id: distributionId }).promise() | ||
// 2. then add this property | ||
params.IfMatch = params.ETag | ||
// 3. then delete this property | ||
delete params.ETag | ||
// 4. then set this property | ||
params.Id = distributionId | ||
// 5. then make our changes | ||
// todo maybe we should add ALL error codes returned from CloudFront/S3?! | ||
params.DistributionConfig.CustomErrorResponses = { | ||
Quantity: 2, | ||
Items: [ | ||
{ | ||
ErrorCode: 404, | ||
ErrorCachingMinTTL: 300, | ||
ResponseCode: '200', | ||
ResponsePagePath: '/index.html' | ||
}, | ||
{ | ||
ErrorCode: 403, | ||
ErrorCachingMinTTL: 300, | ||
ResponseCode: '200', | ||
ResponsePagePath: '/index.html' | ||
} | ||
] | ||
} | ||
params.DistributionConfig.Origins.Items = [ | ||
{ | ||
Id: `app-sync-${subdomain.apiId}`, | ||
DomainName: subdomain.url, | ||
OriginPath: '', | ||
CustomHeaders: { | ||
Quantity: 0, | ||
Items: [] | ||
}, | ||
S3OriginConfig: { | ||
OriginAccessIdentity: '' | ||
} | ||
} | ||
] | ||
params.DistributionConfig.DefaultCacheBehavior.TargetOriginId = `app-sync-${subdomain.apiId}` | ||
// 6. then finally update! | ||
const res = await cf.updateDistribution(params).promise() | ||
return { | ||
id: res.Distribution.Id, | ||
arn: res.Distribution.ARN, | ||
url: res.Distribution.DomainName | ||
} | ||
} | ||
/* | ||
@@ -892,3 +1093,5 @@ * Updates a distribution's origins | ||
addDomainToCloudfrontDistribution, | ||
removeDomainFromCloudFrontDistribution | ||
removeDomainFromCloudFrontDistribution, | ||
createCloudfrontDistributionForAppSync, | ||
updateCloudfrontDistributionForAppSync | ||
} |
56954
1306