Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: (aws-ec2): fix vpc endpoint incorrect issue in China region #16139

Merged
merged 15 commits into from Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 33 additions & 5 deletions packages/@aws-cdk/aws-ec2/lib/vpc-endpoint.ts
Expand Up @@ -326,14 +326,20 @@ export class InterfaceVpcEndpointAwsService implements IInterfaceVpcEndpointServ
const region = Lazy.uncachedString({
produce: (context) => Stack.of(context.scope).region,
});
const defaultEndpointPrefix =Lazy.uncachedString({
const defaultEndpointPrefix = Lazy.uncachedString({
produce: (context) => {
const regionName = Stack.of(context.scope).region;
return this.getDefaultEndpointPrefix(name, regionName);
},
});
const defaultEndpointSuffix = Lazy.uncachedString({
produce: (context) => {
const regionName = Stack.of(context.scope).region;
return this.getDefaultEndpointSuffix(name, regionName);
},
});

this.name = `${prefix || defaultEndpointPrefix}.${region}.${name}`;
this.name = `${prefix || defaultEndpointPrefix}.${region}.${name}${defaultEndpointSuffix}`;
this.port = port || 443;
}

Expand All @@ -351,21 +357,43 @@ export class InterfaceVpcEndpointAwsService implements IInterfaceVpcEndpointServ
'elasticbeanstalk', 'elasticfilesystem', 'elasticfilesystem-fips', 'execute-api', 'imagebuilder',
'iotsitewise.api', 'iotsitewise.data', 'kinesis-streams', 'lambda', 'license-manager', 'monitoring',
'rds', 'redshift', 'redshift-data', 's3', 'sagemaker.api', 'sagemaker.featurestore-runtime',
'sagemaker.runtime', 'servicecatalog', 'sms', 'sqs', 'states', 'sts', 'synthetics', 'transcribe.cn',
'sagemaker.runtime', 'servicecatalog', 'sms', 'sqs', 'states', 'sts', 'synthetics', 'transcribe',
'transcribestreaming', 'transfer', 'xray'],
'cn-northwest-1': ['application-autoscaling', 'athena', 'autoscaling', 'awsconnector', 'cassandra',
'cloudformation', 'codedeploy-commands-secure', 'databrew', 'dms', 'ebs', 'ec2', 'ecr.api', 'ecr.dkr',
'elasticbeanstalk', 'elasticfilesystem', 'elasticfilesystem-fips', 'execute-api', 'imagebuilder',
'kinesis-streams', 'lambda', 'license-manager', 'monitoring', 'rds', 'redshift', 'redshift-data', 's3',
'sagemaker.api', 'sagemaker.featurestore-runtime', 'sagemaker.runtime', 'servicecatalog', 'sms', 'sqs',
'states', 'sts', 'synthetics', 'transcribe.cn', 'transcribestreaming', 'transfer', 'workspaces', 'xray'],
'states', 'sts', 'synthetics', 'transcribe', 'transcribestreaming', 'transfer', 'workspaces', 'xray'],
};
if (VPC_ENDPOINT_SERVICE_EXCEPTIONS[region] && VPC_ENDPOINT_SERVICE_EXCEPTIONS[region].includes(name)) {
if (VPC_ENDPOINT_SERVICE_EXCEPTIONS[region]?.includes(name)) {
return 'cn.com.amazonaws';
} else {
return 'com.amazonaws';
}
}

/**
* Get the endpoint suffix for the service in the specified region.
* In cn-north-1 and cn-northwest-1, the vpc endpoint of transcribe is:
* cn.com.amazonaws.cn-north-1.transcribe.cn
* cn.com.amazonaws.cn-northwest-1.transcribe.cn
* so suffix '.cn' should be return in these scenarios.
*
* For future maintenance, the vpc endpoint services could be fetched using AWS CLI Commmand:
* aws ec2 describe-vpc-endpoint-services
*/
private getDefaultEndpointSuffix(name: string, region: string) {
const VPC_ENDPOINT_SERVICE_EXCEPTIONS: { [region: string]: string[] } = {
'cn-north-1': ['transcribe'],
'cn-northwest-1': ['transcribe'],
};
if (VPC_ENDPOINT_SERVICE_EXCEPTIONS[region]?.includes(name)) {
return '.cn';
} else {
return '';
}
njlynch marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
Expand Down
51 changes: 51 additions & 0 deletions packages/@aws-cdk/aws-ec2/test/vpc-endpoint.test.ts
Expand Up @@ -673,6 +673,57 @@ nodeunitShim({
ServiceName: 'com.amazonaws.cn-northwest-1.glue',
}));

test.done();
},
'test vpc interface endpoint for transcribe can be created correctly in none china regions'(test: Test) {
njlynch marked this conversation as resolved.
Show resolved Hide resolved
//GIVEN
const stack = new Stack(undefined, 'TestStack', { env: { account: '123456789012', region: 'us-east-1' } });
const vpc = new Vpc(stack, 'VPC');

//WHEN
vpc.addInterfaceEndpoint('Transcribe Endpoint', {
service: InterfaceVpcEndpointAwsService.TRANSCRIBE,
});

//THEN
expect(stack).to(haveResource('AWS::EC2::VPCEndpoint', {
ServiceName: 'com.amazonaws.us-east-1.transcribe',
}));

test.done();
},
'test vpc interface endpoint for transcribe can be created correctly in cn-north-1'(test: Test) {
//GIVEN
const stack = new Stack(undefined, 'TestStack', { env: { account: '123456789012', region: 'cn-north-1' } });
const vpc = new Vpc(stack, 'VPC');

//WHEN
vpc.addInterfaceEndpoint('Transcribe Endpoint', {
service: InterfaceVpcEndpointAwsService.TRANSCRIBE,
});

//THEN
expect(stack).to(haveResource('AWS::EC2::VPCEndpoint', {
ServiceName: 'cn.com.amazonaws.cn-north-1.transcribe.cn',
}));

test.done();
},
'test vpc interface endpoint for transcribe can be created correctly in cn-northwest-1'(test: Test) {
//GIVEN
const stack = new Stack(undefined, 'TestStack', { env: { account: '123456789012', region: 'cn-northwest-1' } });
const vpc = new Vpc(stack, 'VPC');

//WHEN
vpc.addInterfaceEndpoint('Transcribe Endpoint', {
service: InterfaceVpcEndpointAwsService.TRANSCRIBE,
});

//THEN
expect(stack).to(haveResource('AWS::EC2::VPCEndpoint', {
ServiceName: 'cn.com.amazonaws.cn-northwest-1.transcribe.cn',
}));

test.done();
},
},
Expand Down