-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmapbox-dns-record-alias.js
More file actions
64 lines (56 loc) · 2.1 KB
/
mapbox-dns-record-alias.js
File metadata and controls
64 lines (56 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict';
const Joi = require('joi');
const intrinsic = require('../intrinsic');
const cdkCommonDNS = require('@mapbox/mapbox-cdk-common/lib/constructs/mapbox-dns-record');
const cdkCommonSchemaDNS = require('@mapbox/mapbox-cdk-common/lib/utils/schemas/dns-schema');
/**
* Create an ALIAS DNS record in mapbox.com/tilestream.net/mbxinternal.com
*
* @param {Object} options - Options.
* @param {String} options.LogicalName - The logical name of the IAM role
* within the CloudFormation template.
*
* const cf = require('@mapbox/cloudfriend');
*
* const myTemplate = { ... };
*
* const record = new cf.shortcuts.MapboxDnsRecordAlias({
* Id: 'My-Record', // This becomes part of the logical id of the output
* Name: 'record-1', // Becomes 'record-1.mapbox.com'
* Environment: 'staging',
* HostedZone: 'mapbox.com', // Or 'tilestream.net' or 'mbxinternal.com',
* Routing: 'Simple', // Or 'Weighted', 'Latency', 'Geo'
* Target: LoadBalancerLogicalId,
* HealthChecks: [{ Path: '/lite' }],
* });
*
* module.exports = cf.merge(myTemplate, record);
*/
class MapboxDnsRecordAlias {
constructor(options) {
if (!options) throw new Error('Options required');
const {
Id,
HostedZone,
Target,
...rest
} = options;
const foundHostedZone = Object.values(cdkCommonSchemaDNS.MapboxHostedZones).find((z) => z.HostedZoneName === HostedZone);
if (!foundHostedZone) {
throw new Error(`Invalid HostedZone parameter. Must be one of: ${Object.values(cdkCommonSchemaDNS.MapboxHostedZones).map((v) => v.HostedZoneName).join(', ')}`);
}
const loadBalancerCanonicalHostedZoneId = intrinsic.getAtt(Target, 'CanonicalHostedZoneID');
const loadBalancerDnsName = intrinsic.getAtt(Target, 'DNSName');
const dnsRecordProps = {
...rest,
HostedZone: foundHostedZone,
Target: {
loadBalancerCanonicalHostedZoneId,
loadBalancerDnsName
}
};
this.Transform = 'AWS::LanguageExtensions';
this.Outputs = new cdkCommonDNS.MapboxDnsRecordAlias(dnsRecordProps).toCloudfriendOutputs(Id);
}
}
module.exports = MapboxDnsRecordAlias;