Skip to content

Commit 289a794

Browse files
authored
fix(aws-route53-targets): add support for custom cname_prefix urls in elastic beanstalk environment endpoint target (#18804)
This PR fixes the extraction of the region name from a Elastic Beanstalk Environment URL generated with a custom CNAME_PREFIX. The code is backward compatible with the regular URL # Motivation ElasticBeanstalkEnvironmentEndpointTarget is used to create an alias target for ElasticBeanstalk Environments that are already created and published for a certain region. When creating an ElasticBeanstalk Environment is possible to configure a `cname_prefix` in order to have a "deterministic" url for the generated environment. Normally the generated url looks like `mybeanstalkenvironment.xyz.eu-west-1.elasticbeanstalk.com`, however when the `cname_prefix` is specified the url loses the randomly generated hash and looks like `mycnameprefix.eu-west-1.elasticbeanstalk.com`. In the custom `cname_prefix` scenario the `ElasticBeanstalkEnvironmentEndpointTarget` class fails with the following error: ``` jsii.errors.JSIIError: Elastic Beanstalk environment target is not supported for the "elasticbeanstalk" region. ``` I mentioned this problem also [here](#17992 (comment)), sorry for the double comment. This PR does not fix the original problem of that issue thread. # How to reproduce Consider this scenario: ``` # app1.py # EB Enviroment enviroment = elasticbeanstalk.CfnEnvironment(self, 'Enviroment', environment_name='MySampleEnviroment', application_name=application.application_name or application_name, solution_stack_name=SOLUTION_STACK_NAME, option_settings=self.get_option_setting_properties(), version_label=app_version_props.ref, cname_prefix='myapp' ) ``` This generates an elastic beanstalk environment with url `myapp.eu-west-1.elasticbeanstalk.com` (supposing one it's deploying in eu-west-1). Then a following cdk app tries to create a DNS record for it ``` # app2.py route53.ARecord(self, f'{construct_label}AliasRecord', target=route53.RecordTarget.from_alias( targets.ElasticBeanstalkEnvironmentEndpointTarget( 'myapp.eu-west-1.elasticbeanstalk.com', ), ), zone=domain_configuration.hosted_zone, ) ``` At this point the command cdk diff returns the following error: ``` jsii.errors.JSIIError: Elastic Beanstalk environment target is not supported for the "elasticbeanstalk" region. ``` ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 1c8bd65 commit 289a794

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

packages/@aws-cdk/aws-route53-targets/lib/elastic-beanstalk-environment-target.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { RegionInfo } from '@aws-cdk/region-info';
55
/**
66
* Use an Elastic Beanstalk environment URL as an alias record target.
77
* E.g. mysampleenvironment.xyz.us-east-1.elasticbeanstalk.com
8+
* or mycustomcnameprefix.us-east-1.elasticbeanstalk.com
89
*
910
* Only supports Elastic Beanstalk environments created after 2016 that have a regional endpoint.
1011
*/
@@ -18,7 +19,9 @@ export class ElasticBeanstalkEnvironmentEndpointTarget implements route53.IAlias
1819
}
1920

2021
const dnsName = this.environmentEndpoint;
21-
const region = cdk.Fn.select(2, cdk.Fn.split('.', dnsName));
22+
const subDomains = cdk.Fn.split('.', dnsName);
23+
const regionSubdomainIndex = subDomains.length - 3;
24+
const region = cdk.Fn.select(regionSubdomainIndex, subDomains);
2225
const { ebsEnvEndpointHostedZoneId: hostedZoneId } = RegionInfo.get(region);
2326

2427
if (!hostedZoneId || !dnsName) {

packages/@aws-cdk/aws-route53-targets/test/elastic-beanstalk-environment-target.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,25 @@ test('use EBS environment as record target', () => {
2323
},
2424
});
2525
});
26+
27+
28+
test('support 4-levels subdomain URLs for EBS environments', () => {
29+
// GIVEN
30+
const stack = new Stack();
31+
const zone = new route53.PublicHostedZone(stack, 'HostedZone', { zoneName: 'test.public' });
32+
33+
// WHEN
34+
new route53.ARecord(stack, 'Alias', {
35+
zone,
36+
recordName: '_foo',
37+
target: route53.RecordTarget.fromAlias(new targets.ElasticBeanstalkEnvironmentEndpointTarget('mycustomcnameprefix.us-east-1.elasticbeanstalk.com')),
38+
});
39+
40+
// THEN
41+
Template.fromStack(stack).hasResourceProperties('AWS::Route53::RecordSet', {
42+
AliasTarget: {
43+
DNSName: 'mycustomcnameprefix.us-east-1.elasticbeanstalk.com',
44+
HostedZoneId: 'Z117KPS5GTRQ2G',
45+
},
46+
});
47+
});

0 commit comments

Comments
 (0)