arc-macro-custom-domain
Advanced tools
Comparing version
179
index.js
@@ -9,9 +9,16 @@ module.exports = function(arc, cloudformation, stage) { | ||
}); | ||
const domainName = process.env.ARC_DOMAIN || ( | ||
stage === 'staging' ? params.stagingDomain : params.productionDomain); | ||
const addDns = (stage === 'staging') ? (params.stagingDns !== false) : (params.productionDns === true); | ||
const customCloudFront = (stage === 'staging') ? params.stageCustomCloudFront : params.prodCustomCloudFront; | ||
// abort if no domainName was specified: | ||
if (!domainName) { | ||
console.log('WARNING: no domain name was specified for arc-macro-custom-domain!! (did you forget to set something?)'); | ||
if (!domainName && !customCloudFront) { | ||
console.log('WARNING: no domain name / cloudfront was specified for arc-macro-custom-domain!! (did you forget to set something?)'); | ||
return cloudformation; | ||
} | ||
const route53Host = params.zoneName; | ||
@@ -34,66 +41,126 @@ const certArn = stage === 'staging' ? params.stagingCertArn : params.productionCertArn; | ||
if (params.httpAPI) { | ||
cloudformation.Resources.ApiGatewayDomain = { | ||
Type: 'AWS::ApiGatewayV2::DomainName', | ||
Properties: { | ||
DomainName: domainName, | ||
DomainNameConfigurations: [{ | ||
CertificateArn: certArn, | ||
EndpointType: 'REGIONAL' | ||
}] | ||
if (!customCloudFront) { | ||
cloudformation.Resources.ApiGatewayDomain = { | ||
Type: 'AWS::ApiGatewayV2::DomainName', | ||
Properties: { | ||
DomainName: domainName, | ||
DomainNameConfigurations: [{ | ||
CertificateArn: certArn, | ||
EndpointType: 'REGIONAL' | ||
}] | ||
} | ||
}; | ||
cloudformation.Resources.ApiGatewayMapping = { | ||
Type: 'AWS::ApiGatewayV2::ApiMapping', | ||
Properties: { | ||
ApiId: { | ||
Ref: restId, | ||
}, | ||
DomainName: domainName, | ||
Stage: { | ||
Ref: `${restId}.Stage` | ||
} | ||
} | ||
}; | ||
} | ||
} else { | ||
if (!customCloudFront) { | ||
cloudformation.Resources.ApiGatewayDomain = { | ||
Type: 'AWS::ApiGateway::DomainName', | ||
Properties: { | ||
RegionalCertificateArn: certArn, | ||
DomainName: domainName, | ||
EndpointConfiguration: { | ||
Types: ['REGIONAL'] | ||
} | ||
} | ||
}; | ||
if (!customCloudFront) { | ||
cloudformation.Resources.ApiGatewayMapping = { | ||
Type: 'AWS::ApiGateway::BasePathMapping', | ||
Properties: { | ||
BasePath: '', | ||
DomainName: domainName, | ||
Stage: { | ||
Ref: `${restId}.Stage` | ||
}, | ||
RestApiId: { | ||
Ref: restId | ||
} | ||
} | ||
}; | ||
} | ||
}; | ||
cloudformation.Resources.ApiGatewayMapping = { | ||
Type: 'AWS::ApiGatewayV2::ApiMapping', | ||
} | ||
} | ||
if (customCloudFront) { | ||
originId = `Origin-${Math.floor(Math.random() * 1000)}`; | ||
cloudformation.Resources.CloudFrontDistro = { | ||
Type: 'AWS::CloudFront::Distribution', | ||
Properties: { | ||
ApiId: { | ||
Ref: restId, | ||
}, | ||
DomainName: domainName, | ||
Stage: { | ||
Ref: `${restId}.Stage` | ||
DistributionConfig: { | ||
Enabled: 'true', | ||
Aliases: [domainName], | ||
Origins: [ | ||
{ | ||
Id: originId, | ||
DomainName: { | ||
"Fn::Sub": [ | ||
'${ApiId}.execute-api.${AWS::Region}.${AWS::URLSuffix}', { | ||
ApiId: { | ||
Ref: restId | ||
} | ||
} | ||
] | ||
}, | ||
CustomOriginConfig: { | ||
HTTPPort: 80, | ||
HTTPSPort: 443, | ||
OriginKeepaliveTimeout: 5, | ||
OriginProtocolPolicy: "https-only", | ||
OriginSSLProtocols: ["TLSv1"] | ||
} | ||
} | ||
], | ||
DefaultCacheBehavior: { | ||
AllowedMethods: ['GET', 'HEAD', 'OPTIONS', 'PUT', 'POST', 'PATCH', 'DELETE'], | ||
ViewerProtocolPolicy: 'redirect-to-https', | ||
TargetOriginId: originId, | ||
ForwardedValues: { | ||
Cookies: { | ||
Forward: 'all' | ||
}, | ||
Headers: ['Referer'], | ||
QueryString: true | ||
} | ||
}, | ||
ViewerCertificate: { | ||
AcmCertificateArn: certArn, | ||
SslSupportMethod: 'sni-only', | ||
MinimumProtocolVersion: 'TLSv1.1_2016' | ||
} | ||
} | ||
} | ||
}; | ||
} else { | ||
cloudformation.Resources.ApiGatewayDomain = { | ||
Type: 'AWS::ApiGateway::DomainName', | ||
Properties: { | ||
RegionalCertificateArn: certArn, | ||
DomainName: domainName, | ||
EndpointConfiguration: { | ||
Types: ['REGIONAL'] | ||
} | ||
} | ||
} | ||
}; | ||
cloudformation.Resources.ApiGatewayMapping = { | ||
Type: 'AWS::ApiGateway::BasePathMapping', | ||
//route53 | ||
if (addDns) { | ||
cloudformation.Resources.DomainDns = { | ||
Type: 'AWS::Route53::RecordSetGroup', | ||
Properties: { | ||
BasePath: '', | ||
DomainName: domainName, | ||
Stage: { | ||
Ref: `${restId}.Stage` | ||
}, | ||
RestApiId: { | ||
Ref: restId | ||
} | ||
HostedZoneName: `${route53Host}.`, | ||
Comment: 'Route to api gateway stage', | ||
RecordSets: [{ | ||
Name: domainName, | ||
Type: 'A', | ||
AliasTarget: { | ||
HostedZoneId: { 'Fn::GetAtt': ['ApiGatewayDomain', 'RegionalHostedZoneId'] }, | ||
DNSName: { 'Fn::GetAtt': ['ApiGatewayDomain', 'RegionalDomainName'] } | ||
} | ||
}] | ||
} | ||
}; | ||
} | ||
//route53 | ||
cloudformation.Resources.DomainDns = { | ||
Type: 'AWS::Route53::RecordSetGroup', | ||
Properties: { | ||
HostedZoneName: `${route53Host}.`, | ||
Comment: 'Route to api gateway stage', | ||
RecordSets: [{ | ||
Name: domainName, | ||
Type: 'A', | ||
AliasTarget: { | ||
HostedZoneId: { 'Fn::GetAtt': ['ApiGatewayDomain', 'RegionalHostedZoneId'] }, | ||
DNSName: { 'Fn::GetAtt': ['ApiGatewayDomain', 'RegionalDomainName'] } | ||
} | ||
}] | ||
} | ||
}; | ||
cloudformation.Outputs.Domain = { | ||
@@ -100,0 +167,0 @@ Description: 'Domain', |
{ | ||
"name": "arc-macro-custom-domain", | ||
"version": "2.2.1", | ||
"version": "2.2.2-alpha", | ||
"description": "- only works on staging (for now) - can override domain with `ARC_DOMAIN`", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
6296
48.98%161
59.41%2
100%