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

feat(vpcv2): vpc peering connection construct #31645

Open
wants to merge 36 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
af43218
initial skeleton code
Oct 2, 2024
16c8c3e
improve readability and update missing params
Oct 3, 2024
7ebbfe5
add test cases
Oct 3, 2024
9f60103
add integration test
Oct 3, 2024
7222bf4
additional test coverage
Oct 3, 2024
cc7c813
Merge branch 'main' into vpc_peering_connection_construct
1kaileychen Oct 3, 2024
d920b57
add vpc peering connection into readme
Oct 3, 2024
49780bf
Merge branch 'vpc_peering_connection_construct' of https://github.com…
Oct 3, 2024
65a0846
Merge branch 'main' into vpc_peering_connection_construct
1kaileychen Oct 3, 2024
b21736e
fix rosetta readme errors
Oct 4, 2024
41f0fb7
Merge branch 'vpc_peering_connection_construct' of https://github.com…
Oct 4, 2024
3baf408
Merge branch 'main' into vpc_peering_connection_construct
paulhcsun Oct 4, 2024
39f7072
move validateVpcCidrOverlap to utils file
Oct 7, 2024
2b4c5c6
initial design change implementation
Oct 10, 2024
0c3bc8b
add region & ownerAccountId to VpcV2Props
Oct 10, 2024
ac63088
remove old tests
Oct 10, 2024
9f96a92
error msg modifications & use ownerAccountId
Oct 10, 2024
57df8ed
update policies and warning
Oct 11, 2024
5299751
add tests and integration tests
Oct 11, 2024
0143dbc
add readme
Oct 11, 2024
1c0f389
Merge remote-tracking branch 'origin/main' into vpc_peering_connectio…
Oct 11, 2024
d31b6ca
fix lint fail due to ordering in readme
Oct 11, 2024
751530c
update readme errors
Oct 11, 2024
98d39d2
update readme peering connection class to function
Oct 11, 2024
49e2e4f
add createPeeringConnection test
Oct 15, 2024
b8fc4a0
change warning to error when peerRoleArn is defined for same account
Oct 16, 2024
e810bd7
update description for peerRoleArn
Oct 16, 2024
b926d86
add clarity in readme different types of peering
Oct 16, 2024
9ff9da6
return integ.route-v2 to original test case
Oct 16, 2024
b13d363
integration test for peering connection
Oct 16, 2024
6607192
Apply suggestions from code review
shikha372 Oct 16, 2024
2f78a5a
Update wording suggestion
1kaileychen Oct 17, 2024
5773a08
remove region and ownerAccountId from VpcV2Props
Oct 17, 2024
8e3ff5f
update test cases and readme
Oct 17, 2024
9022843
Merge branch 'vpc_peering_connection_construct' of https://github.com…
Oct 17, 2024
951abf0
update account ids that are allowed in readme and tests
Oct 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 170 additions & 4 deletions packages/@aws-cdk/aws-ec2-alpha/lib/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { CfnEIP, CfnEgressOnlyInternetGateway, CfnInternetGateway, CfnNatGateway, CfnRoute, CfnRouteTable, CfnVPCGatewayAttachment, CfnVPNGateway, CfnVPNGatewayRoutePropagation, GatewayVpcEndpoint, IRouteTable, IVpcEndpoint, RouterType } from 'aws-cdk-lib/aws-ec2';
import { CfnEIP, CfnEgressOnlyInternetGateway, CfnInternetGateway, CfnNatGateway, CfnVPCPeeringConnection, CfnRoute, CfnRouteTable, CfnVPCGatewayAttachment, CfnVPNGateway, CfnVPNGatewayRoutePropagation, GatewayVpcEndpoint, IRouteTable, IVpcEndpoint, RouterType } from 'aws-cdk-lib/aws-ec2';
import { Construct, IDependable } from 'constructs';
import { Annotations, Duration, IResource, Resource } from 'aws-cdk-lib/core';
import { Aws, Annotations, Duration, IResource, Resource, Stack } from 'aws-cdk-lib/core';
import { IVpcV2, VPNGatewayV2Options } from './vpc-v2-base';
import { NetworkUtils, allRouteTableIds } from './util';
import { CidrBlock, NetworkUtils, allRouteTableIds } from './util';
import { ISubnetV2 } from './subnet-v2';
import { AccountPrincipal, Effect, PolicyStatement, Role } from 'aws-cdk-lib/aws-iam';

/**
* Indicates whether the NAT gateway supports public or private connectivity.
Expand Down Expand Up @@ -175,6 +176,47 @@ export interface NatGatewayProps extends NatGatewayOptions {
readonly vpc?: IVpcV2;
}

/**
* Properties to define a VPC peering connection.
*/
export interface VPCPeeringConnectionProps {
/**
paulhcsun marked this conversation as resolved.
Show resolved Hide resolved
* Indicates whether this is a cross-account VPC peering connection.
*/
readonly isCrossAccount: boolean;

/**
* The VPC that is requesting the peering connection.
*/
readonly requestorVpc: IVpcV2;

/**
* The VPC that is accepting the peering connection.
*/
readonly acceptorVpc: IVpcV2;

/**
* The AWS account ID of the acceptor VPC owner.
*
1kaileychen marked this conversation as resolved.
Show resolved Hide resolved
* @default - no acceptor account ID needed if not cross account connection
*/
readonly acceptorAccountId?: string;

/**
* The region of the acceptor VPC.
*
* @default - same region as the requestor VPC
*/
readonly acceptorRegion?: string;

/**
* The resource name of the peering connection.
*
* @default - peering connection provisioned without any name
*/
readonly vpcPeeringConnectionName?: string;
}

/**
* Creates an egress-only internet gateway
* @resource AWS::EC2::EgressOnlyInternetGateway
Expand Down Expand Up @@ -402,6 +444,88 @@ export class NatGateway extends Resource implements IRouteTarget {
}
}

/**
* Creates a peering connection between two VPCs
* @resource AWS::EC2::VPCPeeringConnection
*/
export class VPCPeeringConnection extends Resource implements IRouteTarget {

/**
* The type of router used in the route.
*/
readonly routerType: RouterType;

1kaileychen marked this conversation as resolved.
Show resolved Hide resolved
/**
* The ID of the route target.
*/
readonly routerTargetId: string;

1kaileychen marked this conversation as resolved.
Show resolved Hide resolved
/**
* The VPC peering connection CFN resource.
*/
public readonly resource: CfnVPCPeeringConnection;

constructor(scope: Construct, id: string, props: VPCPeeringConnectionProps) {
super(scope, id);

const region = props.acceptorRegion ? props.acceptorRegion : Stack.of(this).region;
let peerRole: Role | undefined;
1kaileychen marked this conversation as resolved.
Show resolved Hide resolved
this.routerType = RouterType.VPC_PEERING_CONNECTION;

if (props.isCrossAccount && props.acceptorAccountId === undefined) {
throw new Error('AcceptorAccountId is required for cross-account peering connections');
}

1kaileychen marked this conversation as resolved.
Show resolved Hide resolved
const overlap = validateVpcCidrOverlap(props.requestorVpc, props.acceptorVpc);
if (overlap) {
1kaileychen marked this conversation as resolved.
Show resolved Hide resolved
throw new Error('CIDR block should not overlap with existing subnet blocks');
}
shikha372 marked this conversation as resolved.
Show resolved Hide resolved

if (props.isCrossAccount) {
peerRole = new Role(this, 'PeerRole', {
assumedBy: new AccountPrincipal(Stack.of(this).account),
roleName: `VPCPeeringRole-${id}`,
});

peerRole.assumeRolePolicy?.addStatements(
new PolicyStatement({
effect: Effect.ALLOW,
actions: ['sts:AssumeRole'],
principals: [new AccountPrincipal(Stack.of(this).account)],
}),
);

peerRole.addToPolicy(new PolicyStatement({
effect: Effect.ALLOW,
actions: ['ec2:acceptVpcPeeringConnection'],
resources: [`arn:${Aws.PARTITION}:ec2:${region}:${props.acceptorAccountId}:vpc/${props.acceptorVpc.vpcId}`],
}));

peerRole.addToPolicy(new PolicyStatement({
actions: ['ec2:acceptVpcPeeringConnection'],
effect: Effect.ALLOW,
resources: [`arn:${Aws.PARTITION}:ec2:${region}:${props.acceptorAccountId}:vpc-peering-connection/*`],
conditions: {
StringEquals: {
'ec2:AccepterVpc': `arn:${Aws.PARTITION}:ec2:${region}:${props.acceptorAccountId}:vpc/${props.acceptorVpc.vpcId}`,
},
},
}));
}

paulhcsun marked this conversation as resolved.
Show resolved Hide resolved
1kaileychen marked this conversation as resolved.
Show resolved Hide resolved
this.resource = new CfnVPCPeeringConnection(this, 'PeeringConnection', {
vpcId: props.requestorVpc.vpcId,
peerVpcId: props.acceptorVpc.vpcId,
peerOwnerId: props.acceptorAccountId,
peerRegion: region,
peerRoleArn: peerRole?.roleArn,
});

this.routerTargetId = this.resource.attrId;
this.node.defaultChild = this.resource;
}
}

/**
* The type of endpoint or gateway being targeted by the route.
*/
Expand Down Expand Up @@ -534,7 +658,7 @@ export class Route extends Resource implements IRouteV2 {
/**
* The type of router the route is targetting
*/
public readonly targetRouterType: RouterType
public readonly targetRouterType: RouterType;

/**
* The route CFN resource.
Expand Down Expand Up @@ -663,4 +787,46 @@ function routerTypeToPropName(routerType: RouterType) {
[RouterType.VPC_PEERING_CONNECTION]: 'vpcPeeringConnectionId',
[RouterType.VPC_ENDPOINT]: 'vpcEndpointId',
})[routerType];
}

/**
* Validates if the provided IPv4 CIDR block overlaps with existing subnet CIDR blocks within the given VPC.
*
* @param requestorVpc The VPC of the requestor.
* @param acceptorVpc The VPC of the acceptor.
* @returns True if the IPv4 CIDR block overlaps with existing subnet CIDR blocks, false otherwise.
* @internal
*/
function validateVpcCidrOverlap(requestorVpc: IVpcV2, acceptorVpc: IVpcV2): boolean {

1kaileychen marked this conversation as resolved.
Show resolved Hide resolved
const requestorCidrs = [requestorVpc.ipv4CidrBlock];
const acceptorCidrs = [acceptorVpc.ipv4CidrBlock];

if (requestorVpc.secondaryCidrBlock) {
requestorCidrs.push(...requestorVpc.secondaryCidrBlock
.map(block => block.cidrBlock)
.filter((cidr): cidr is string => cidr !== undefined));
}

if (acceptorVpc.secondaryCidrBlock) {
acceptorCidrs.push(...acceptorVpc.secondaryCidrBlock
.map(block => block.cidrBlock)
.filter((cidr): cidr is string => cidr !== undefined));
}

for (const requestorCidr of requestorCidrs) {
const requestorRange = new CidrBlock(requestorCidr);
const requestorIpRange: [string, string] = [requestorRange.minIp(), requestorRange.maxIp()];

for (const acceptorCidr of acceptorCidrs) {
const acceptorRange = new CidrBlock(acceptorCidr);
const acceptorIpRange: [string, string] = [acceptorRange.minIp(), acceptorRange.maxIp()];

if (requestorRange.rangesOverlap(acceptorIpRange, requestorIpRange)) {
return true;
}
}
}

return false;
}

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

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"TestRoottableeigwRouteF867084E": {
"Type": "AWS::EC2::Route",
"Properties": {
"DestinationCidrBlock": "::/0",
"DestinationIpv6CidrBlock": "::/0",
"EgressOnlyInternetGatewayId": {
"Fn::GetAtt": [
Expand All @@ -49,7 +48,10 @@
"RouteTableId"
]
}
}
},
"DependsOn": [
"testEOIGWEIGW54CCAD37"
]
},
"eigwSubnetCC28B9F9": {
"Type": "AWS::EC2::Subnet",
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,11 @@
"RouteTableId"
]
}
}
},
"TestRoottableigwRouteGWAttachment4B3E8FD9": {
"Type": "AWS::EC2::VPCGatewayAttachment",
"Properties": {
"InternetGatewayId": {
"Fn::GetAtt": [
"testIGW8D947AF2",
"InternetGatewayId"
]
},
"VpcId": {
"Fn::GetAtt": [
"igw127F1970",
"VpcId"
]
}
}
},
"DependsOn": [
"testIGWGWAttachment682A6782",
"testIGW8D947AF2"
]
},
"igwSubnetF238E402": {
"Type": "AWS::EC2::Subnet",
Expand Down Expand Up @@ -114,6 +101,23 @@
},
"testIGW8D947AF2": {
"Type": "AWS::EC2::InternetGateway"
},
"testIGWGWAttachment682A6782": {
"Type": "AWS::EC2::VPCGatewayAttachment",
"Properties": {
"InternetGatewayId": {
"Fn::GetAtt": [
"testIGW8D947AF2",
"InternetGatewayId"
]
},
"VpcId": {
"Fn::GetAtt": [
"igw127F1970",
"VpcId"
]
}
}
}
},
"Parameters": {
Expand Down

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@
"RouteTableId"
]
}
}
},
"DependsOn": [
"testNATgwNATGateway1533420D"
]
},
"natgwprivSubnetE547C5A0": {
"Type": "AWS::EC2::Subnet",
Expand Down

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

Loading
Loading