Skip to content

Commit

Permalink
feat(aws-route53-targets): Support for Elastic Beanstalk environment …
Browse files Browse the repository at this point in the history
…URLs (#16305)

## Summary

This PR adds a new Route53 target `ElasticBeanstalkEnvironmentTarget` for creating RecordSets that target Elastic Beanstalk environment URLs.

E.g.

```ts
const ebsEnvironmentUrl = 'mysampleenvironment.xyz.us-east-1.elasticbeanstalk.com';

new route53.ARecord(this, 'AliasRecord', {
	zone,
    target: route53.RecordTarget.fromAlias(new alias.ElasticBeanstalkEnvironmentTarget(ebsEnvironmentUrl)),
});
```

[How to find your Elastic Beanstalk environment URL](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/routing-to-beanstalk-environment.html#routing-to-beanstalk-environment-get-domain-name)

Fixes: #3206


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
ryparker authored Oct 28, 2021
1 parent c4ba858 commit bc07cb0
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-route53-targets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,15 @@ See [the Developer Guide](https://docs.aws.amazon.com/Route53/latest/DeveloperGu
});
```

* Elastic Beanstalk environment:

**Important:** Only supports Elastic Beanstalk environments created after 2016 that have a regional endpoint.

```ts
new route53.ARecord(this, 'AliasRecord', {
zone,
target: route53.RecordTarget.fromAlias(new alias.ElasticBeanstalkEnvironmentEndpointTarget(ebsEnvironmentUrl)),
});
```

See the documentation of `@aws-cdk/aws-route53` for more information.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as route53 from '@aws-cdk/aws-route53';
import * as cdk from '@aws-cdk/core';
import { RegionInfo } from '@aws-cdk/region-info';

/**
* Use an Elastic Beanstalk environment URL as an alias record target.
* E.g. mysampleenvironment.xyz.us-east-1.elasticbeanstalk.com
*
* Only supports Elastic Beanstalk environments created after 2016 that have a regional endpoint.
*/
export class ElasticBeanstalkEnvironmentEndpointTarget implements route53.IAliasRecordTarget {
constructor(private readonly environmentEndpoint: string) {
}

public bind(_record: route53.IRecordSet, _zone?: route53.IHostedZone): route53.AliasRecordTargetConfig {
if (cdk.Token.isUnresolved(this.environmentEndpoint)) {
throw new Error('Cannot use an EBS alias as `environmentEndpoint`. You must find your EBS environment endpoint via the AWS console. See the Elastic Beanstalk developer guide: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customdomains.html');
}

const dnsName = this.environmentEndpoint;
const region = cdk.Fn.select(2, cdk.Fn.split('.', dnsName));
const { ebsEnvEndpointHostedZoneId: hostedZoneId } = RegionInfo.get(region);

if (!hostedZoneId || !dnsName) {
throw new Error(`Elastic Beanstalk environment target is not supported for the "${region}" region.`);
}

return {
hostedZoneId,
dnsName,
};
}
}
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-route53-targets/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './api-gateway-domain-name';
export * from './api-gatewayv2-domain-name';
export * from './bucket-website-target';
export * from './elastic-beanstalk-environment-target';
export * from './classic-load-balancer-target';
export * from './cloudfront-target';
export * from './load-balancer-target';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import '@aws-cdk/assert-internal/jest';
import * as route53 from '@aws-cdk/aws-route53';
import { Stack } from '@aws-cdk/core';
import * as targets from '../lib';

test('use EBS environment as record target', () => {
// GIVEN
const stack = new Stack();
const zone = new route53.PublicHostedZone(stack, 'HostedZone', { zoneName: 'test.public' });

// WHEN
new route53.ARecord(stack, 'Alias', {
zone,
recordName: '_foo',
target: route53.RecordTarget.fromAlias(new targets.ElasticBeanstalkEnvironmentEndpointTarget('mysampleenvironment.xyz.us-east-1.elasticbeanstalk.com')),
});

// THEN
expect(stack).toHaveResource('AWS::Route53::RecordSet', {
AliasTarget: {
DNSName: 'mysampleenvironment.xyz.us-east-1.elasticbeanstalk.com',
HostedZoneId: 'Z117KPS5GTRQ2G',
},
});
});
31 changes: 31 additions & 0 deletions packages/@aws-cdk/region-info/build-tools/fact-tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,37 @@ export const ROUTE_53_BUCKET_WEBSITE_ZONE_IDS: { [region: string]: string } = {
'us-west-2': 'Z3BJ6K6RIION7M',
};

/**
* The hosted zone Id of the Elastic Beanstalk environment.
*
* @see https://docs.aws.amazon.com/general/latest/gr/elasticbeanstalk.html
*/
export const EBS_ENV_ENDPOINT_HOSTED_ZONE_IDS: { [region: string]: string } = {
'af-south-1': 'Z1EI3BVKMKK4AM',
'ap-east-1': 'ZPWYUBWRU171A',
'ap-northeast-1': 'Z1R25G3KIG2GBW',
'ap-northeast-2': 'Z3JE5OI70TWKCP',
'ap-northeast-3': 'ZNE5GEY1TIAGY',
'ap-south-1': 'Z18NTBI3Y7N9TZ',
'ap-southeast-1': 'Z16FZ9L249IFLT',
'ap-southeast-2': 'Z2PCDNR3VC2G1N',
'ca-central-1': 'ZJFCZL7SSZB5I',
'eu-central-1': 'Z1FRNW7UH4DEZJ',
'eu-north-1': 'Z23GO28BZ5AETM',
'eu-south-1': 'Z10VDYYOA2JFKM',
'eu-west-1': 'Z2NYPWQ7DFZAZH',
'eu-west-2': 'Z1GKAAAUGATPF1',
'eu-west-3': 'Z5WN6GAYWG5OB',
'me-south-1': 'Z2BBTEKR2I36N2',
'sa-east-1': 'Z10X7K2B4QSOFV',
'us-east-1': 'Z117KPS5GTRQ2G',
'us-east-2': 'Z14LCN19Q5QHIC',
'us-gov-east-1': 'Z35TSARG0EJ4VU',
'us-gov-west-1': 'Z4KAURWC4UUUG',
'us-west-1': 'Z1LQECGX5PH1X',
'us-west-2': 'Z38NKT9BP95V3O',
};

interface Region { partition: string, domainSuffix: string }

export const PARTITION_MAP: { [region: string]: Region } = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Default } from '../lib/default';
import { AWS_REGIONS, AWS_SERVICES } from './aws-entities';
import {
APPMESH_ECR_ACCOUNTS, AWS_CDK_METADATA, AWS_OLDER_REGIONS, CLOUDWATCH_LAMBDA_INSIGHTS_ARNS, DLC_REPOSITORY_ACCOUNTS,
ELBV2_ACCOUNTS, FIREHOSE_CIDR_BLOCKS, PARTITION_MAP, ROUTE_53_BUCKET_WEBSITE_ZONE_IDS,
ELBV2_ACCOUNTS, FIREHOSE_CIDR_BLOCKS, PARTITION_MAP, ROUTE_53_BUCKET_WEBSITE_ZONE_IDS, EBS_ENV_ENDPOINT_HOSTED_ZONE_IDS,
} from './fact-tables';

async function main(): Promise<void> {
Expand Down Expand Up @@ -57,6 +57,9 @@ async function main(): Promise<void> {

registerFact(region, 'S3_STATIC_WEBSITE_ZONE_53_HOSTED_ZONE_ID', ROUTE_53_BUCKET_WEBSITE_ZONE_IDS[region] || '');

registerFact(region, 'EBS_ENV_ENDPOINT_HOSTED_ZONE_ID', EBS_ENV_ENDPOINT_HOSTED_ZONE_IDS[region] || '');


registerFact(region, 'ELBV2_ACCOUNT', ELBV2_ACCOUNTS[region]);

registerFact(region, 'DLC_REPOSITORY_ACCOUNT', DLC_REPOSITORY_ACCOUNTS[region]);
Expand Down
5 changes: 5 additions & 0 deletions packages/@aws-cdk/region-info/lib/fact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ export class FactName {
*/
public static readonly S3_STATIC_WEBSITE_ZONE_53_HOSTED_ZONE_ID = 's3-static-website:route-53-hosted-zone-id';

/**
* The hosted zone ID used by Route 53 to alias a EBS environment endpoint in this region (e.g: Z2O1EMRO9K5GLX)
*/
public static readonly EBS_ENV_ENDPOINT_HOSTED_ZONE_ID = 'ebs-environment:route-53-hosted-zone-id';

/**
* The prefix for VPC Endpoint Service names,
* cn.com.amazonaws.vpce for China regions,
Expand Down
7 changes: 7 additions & 0 deletions packages/@aws-cdk/region-info/lib/region-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ export class RegionInfo {
return Fact.find(this.name, FactName.S3_STATIC_WEBSITE_ZONE_53_HOSTED_ZONE_ID);
}

/**
* The hosted zone ID used by Route 53 to alias a EBS environment endpoint in this region (e.g: Z2O1EMRO9K5GLX)
*/
public get ebsEnvEndpointHostedZoneId(): string | undefined {
return Fact.find(this.name, FactName.EBS_ENV_ENDPOINT_HOSTED_ZONE_ID);
}

/**
* The prefix for VPC Endpoint Service names,
* cn.com.amazonaws.vpce for China regions,
Expand Down

0 comments on commit bc07cb0

Please sign in to comment.