Skip to content

Commit

Permalink
fix(ec2): private DNS for custom endpoints has incorrect default
Browse files Browse the repository at this point in the history
Currently if I create an InterfaceVpcEndpoint to a non-AWS service, I must set the privateDnsEnabled field to be "false". This is because the default is "true", which works for AWS services but not for custom hosted services. This change exposes a privateDnsDefault for different IInterfaceVpcEndpointService implementations, and VpcEndpoint will use that as the default if privateDnsEnabled is not specified. This way, I could create a service without having to specify private DNS settings.
  • Loading branch information
flemjame-at-amazon committed Feb 4, 2020
1 parent da193ba commit d681d96
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 3 deletions.
111 changes: 111 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions packages/@aws-cdk/aws-ec2/lib/vpc-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ export interface IInterfaceVpcEndpointService {
* The port of the service.
*/
readonly port: number;

/**
* Whether Private DNS is supported by default.
*/
readonly privateDnsDefault?: boolean;
}

/**
Expand All @@ -218,6 +223,11 @@ export class InterfaceVpcEndpointService implements IInterfaceVpcEndpointService
*/
public readonly port: number;

/**
* Whether Private DNS is supported by default.
*/
public readonly privateDnsDefault?: boolean = false;

constructor(name: string, port?: number) {
this.name = name;
this.port = port || 443;
Expand Down Expand Up @@ -279,6 +289,11 @@ export class InterfaceVpcEndpointAwsService implements IInterfaceVpcEndpointServ
*/
public readonly port: number;

/**
* Whether Private DNS is supported by default.
*/
public readonly privateDnsDefault?: boolean = true;

constructor(name: string, prefix?: string, port?: number) {
this.name = `${prefix || 'com.amazonaws'}.${Aws.REGION}.${name}`;
this.port = port || 443;
Expand All @@ -298,7 +313,8 @@ export interface InterfaceVpcEndpointOptions {
* Whether to associate a private hosted zone with the specified VPC. This
* allows you to make requests to the service using its default DNS hostname.
*
* @default true
* @default set by the instance of IInterfaceVpcEndpointService, or true if
* not defined by the instance of IInterfaceVpcEndpointService
*/
readonly privateDnsEnabled?: boolean;

Expand Down Expand Up @@ -429,7 +445,7 @@ export class InterfaceVpcEndpoint extends VpcEndpoint implements IInterfaceVpcEn
const subnetIds = subnets.subnetIds;

const endpoint = new CfnVPCEndpoint(this, 'Resource', {
privateDnsEnabled: props.privateDnsEnabled !== undefined ? props.privateDnsEnabled : true,
privateDnsEnabled: props.privateDnsEnabled ?? props.service.privateDnsDefault ?? true,
policyDocument: Lazy.anyValue({ produce: () => this.policyDocument }),
securityGroupIds: securityGroups.map(s => s.securityGroupId),
serviceName: props.service.name,
Expand Down
23 changes: 22 additions & 1 deletion packages/@aws-cdk/aws-ec2/test/test.vpc-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,31 @@ export = {

// THEN
expect(stack).to(haveResource('AWS::EC2::VPCEndpoint', {
ServiceName: "com.amazonaws.vpce.us-east-1.vpce-svc-uuddlrlrbastrtsvc"
ServiceName: "com.amazonaws.vpce.us-east-1.vpce-svc-uuddlrlrbastrtsvc",
PrivateDnsEnabled: false
}));

test.done();
},
'marketplace partner service interface endpoint'(test: Test) {
// GIVEN
const stack = new Stack();
const vpc = new Vpc(stack, 'VpcNetwork');

// WHEN
vpc.addInterfaceEndpoint('YourService', {
service: {name: "com.amazonaws.vpce.us-east-1.vpce-svc-mktplacesvcwprdns",
port: 443,
privateDnsDefault: true}
});

// THEN
expect(stack).to(haveResource('AWS::EC2::VPCEndpoint', {
ServiceName: "com.amazonaws.vpce.us-east-1.vpce-svc-mktplacesvcwprdns",
PrivateDnsEnabled: true
}));

test.done();
}
}
};

0 comments on commit d681d96

Please sign in to comment.