in packages/aws-cdk-lib/aws-servicediscovery/lib/service.ts [219:323]
constructor(scope: Construct, id: string, props: ServiceProps) {
super(scope, id);
// Enhanced CDK Analytics Telemetry
addConstructMetadata(this, props);
const namespaceType = props.namespace.type;
const discoveryType = props.discoveryType || defaultDiscoveryType(props.namespace);
if (namespaceType == NamespaceType.HTTP && discoveryType == DiscoveryType.DNS_AND_API) {
throw new Error(
'Cannot specify `discoveryType` of DNS_AND_API when using an HTTP namespace.',
);
}
// Validations
if (
discoveryType === DiscoveryType.API &&
(props.routingPolicy || props.dnsRecordType)
) {
throw new Error(
'Cannot specify `routingPolicy` or `dnsRecord` when using an HTTP namespace.',
);
}
if (props.healthCheck && props.customHealthCheck) {
throw new Error('Cannot specify both `healthCheckConfig` and `healthCheckCustomConfig`.');
}
if (namespaceType === NamespaceType.DNS_PRIVATE && props.healthCheck) {
throw new Error('Cannot specify `healthCheckConfig` for a Private DNS namespace.');
}
if (props.routingPolicy === RoutingPolicy.MULTIVALUE
&& props.dnsRecordType === DnsRecordType.CNAME) {
throw new Error('Cannot use `CNAME` record when routing policy is `Multivalue`.');
}
// Additional validation for eventual attachment of LBs
// The same validation happens later on during the actual attachment
// of LBs, but we need the property for the correct configuration of
// routingPolicy anyway, so might as well do the validation as well.
if (props.routingPolicy === RoutingPolicy.MULTIVALUE
&& props.loadBalancer) {
throw new Error('Cannot register loadbalancers when routing policy is `Multivalue`.');
}
if (props.healthCheck
&& props.healthCheck.type === HealthCheckType.TCP
&& props.healthCheck.resourcePath) {
throw new Error('Cannot specify `resourcePath` when using a `TCP` health check.');
}
// Set defaults where necessary
const routingPolicy = (props.dnsRecordType === DnsRecordType.CNAME) || props.loadBalancer
? RoutingPolicy.WEIGHTED
: RoutingPolicy.MULTIVALUE;
const dnsRecordType = props.dnsRecordType || DnsRecordType.A;
if (props.loadBalancer
&& (!(dnsRecordType === DnsRecordType.A
|| dnsRecordType === DnsRecordType.AAAA
|| dnsRecordType === DnsRecordType.A_AAAA))) {
throw new Error('Must support `A` or `AAAA` records to register loadbalancers.');
}
const dnsConfig: CfnService.DnsConfigProperty | undefined =
discoveryType === DiscoveryType.API
? undefined
: {
dnsRecords: renderDnsRecords(dnsRecordType, props.dnsTtl),
namespaceId: props.namespace.namespaceId,
routingPolicy,
};
const healthCheckConfigDefaults = {
type: HealthCheckType.HTTP,
failureThreshold: 1,
resourcePath: props.healthCheck && props.healthCheck.type !== HealthCheckType.TCP
? '/'
: undefined,
};
const healthCheckConfig = props.healthCheck && { ...healthCheckConfigDefaults, ...props.healthCheck };
const healthCheckCustomConfig = props.customHealthCheck;
// Create service
const service = new CfnService(this, 'Resource', {
name: props.name,
description: props.description,
dnsConfig,
healthCheckConfig,
healthCheckCustomConfig,
namespaceId: props.namespace.namespaceId,
type: props.discoveryType == DiscoveryType.API ? 'HTTP' : undefined,
});
this.serviceName = service.attrName;
this.serviceArn = service.attrArn;
this.serviceId = service.attrId;
this.namespace = props.namespace;
this.dnsRecordType = dnsRecordType;
this.routingPolicy = routingPolicy;
this.discoveryType = discoveryType;
}