-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat(ec2-alpha): improve VPC peering API with type-safe role handling and fromAttributes method #35606
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
base: main
Are you sure you want to change the base?
feat(ec2-alpha): improve VPC peering API with type-safe role handling and fromAttributes method #35606
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,7 +71,7 @@ export interface EgressOnlyInternetGatewayOptions { | |
/** | ||
* Options to define InternetGateway for VPC | ||
*/ | ||
export interface InternetGatewayOptions{ | ||
export interface InternetGatewayOptions { | ||
|
||
/** | ||
* Destination Ipv6 address for EGW route | ||
|
@@ -400,9 +400,9 @@ export abstract class VpcV2Base extends Resource implements IVpcV2 { | |
routeTableIds, | ||
vpnGatewayId: this._vpnGatewayId, | ||
}); | ||
// The AWS::EC2::VPNGatewayRoutePropagation resource cannot use the VPN gateway | ||
// until it has successfully attached to the VPC. | ||
// See https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-vpn-gatewayrouteprop.html | ||
// The AWS::EC2::VPNGatewayRoutePropagation resource cannot use the VPN gateway | ||
// until it has successfully attached to the VPC. | ||
// See https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-vpn-gatewayrouteprop.html | ||
routePropagation.node.addDependency(attachment); | ||
} | ||
|
||
|
@@ -481,7 +481,7 @@ export abstract class VpcV2Base extends Resource implements IVpcV2 { | |
let useIpv6; | ||
if (this.secondaryCidrBlock) { | ||
useIpv6 = (this.secondaryCidrBlock.some((secondaryAddress) => secondaryAddress.amazonProvidedIpv6CidrBlock === true || | ||
secondaryAddress.ipv6IpamPoolId !== undefined || secondaryAddress.ipv6CidrBlock !== undefined)); | ||
secondaryAddress.ipv6IpamPoolId !== undefined || secondaryAddress.ipv6CidrBlock !== undefined)); | ||
} | ||
|
||
if (!useIpv6) { | ||
|
@@ -618,12 +618,16 @@ export abstract class VpcV2Base extends Resource implements IVpcV2 { | |
} | ||
|
||
/** | ||
* Creates peering connection role for acceptor VPC | ||
* Creates peering connection role for acceptor VPC. | ||
* | ||
* The role name will be auto-generated by CDK to ensure uniqueness. | ||
* | ||
* @param requestorAccountId The AWS account ID that will assume this role | ||
* @returns The created IAM role that can be used for cross-account VPC peering | ||
*/ | ||
public createAcceptorVpcRole(requestorAccountId: string): Role { | ||
const peeringRole = new Role(this, 'VpcPeeringRole', { | ||
assumedBy: new AccountPrincipal(requestorAccountId), | ||
roleName: 'VpcPeeringRole', | ||
description: 'Restrictive role for VPC peering', | ||
}); | ||
|
||
|
@@ -647,6 +651,50 @@ export abstract class VpcV2Base extends Resource implements IVpcV2 { | |
return peeringRole; | ||
} | ||
|
||
/** | ||
* Creates peering connection role for requestor VPC. | ||
* | ||
* This role allows the acceptor account to create a VPC peering connection | ||
* with this VPC. The role should be assumed by the acceptor account when | ||
* initiating the peering connection. | ||
* | ||
* The role name will be auto-generated by CDK to ensure uniqueness. | ||
* | ||
* @param acceptorAccountId The AWS account ID that will assume this role | ||
* @returns The created IAM role that can be used for cross-account VPC peering | ||
* | ||
* @example | ||
* ```typescript | ||
* const requestorRole = vpc.createRequestorPeerRole('123456789012'); | ||
* // Share this role ARN with the acceptor account | ||
* ``` | ||
*/ | ||
public createRequestorPeerRole(acceptorAccountId: string): Role { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think requestor needs a role? My prior suggestion was to create this kind of method to create a role from attributes, since well known role name was hard coded. But since you've removed the hard coded name, I don't think this is relevant. |
||
const peeringRole = new Role(this, 'RequestorVpcPeeringRole', { | ||
assumedBy: new AccountPrincipal(acceptorAccountId), | ||
description: 'Restrictive role for VPC peering from requestor side', | ||
}); | ||
|
||
peeringRole.addToPolicy(new PolicyStatement({ | ||
effect: Effect.ALLOW, | ||
actions: ['ec2:CreateVpcPeeringConnection'], | ||
resources: [`arn:${Aws.PARTITION}:ec2:${this.region}:${this.ownerAccountId}:vpc/${this.vpcId}`], | ||
})); | ||
|
||
peeringRole.addToPolicy(new PolicyStatement({ | ||
actions: ['ec2:CreateVpcPeeringConnection'], | ||
effect: Effect.ALLOW, | ||
resources: [`arn:${Aws.PARTITION}:ec2:${this.region}:${this.ownerAccountId}:vpc-peering-connection/*`], | ||
conditions: { | ||
StringEquals: { | ||
'ec2:RequesterVpc': `arn:${Aws.PARTITION}:ec2:${this.region}:${this.ownerAccountId}:vpc/${this.vpcId}`, | ||
}, | ||
}, | ||
})); | ||
|
||
return peeringRole; | ||
} | ||
|
||
/** | ||
* Creates a peering connection | ||
*/ | ||
|
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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.