Skip to content

Commit

Permalink
feat(ec2): add vpcArn to IVpc and Vpc (#16666)
Browse files Browse the repository at this point in the history
fixes #16493

introduces context aware ARN for VPC, dervied from the current stack.
This allows easier referencing compared to constructing the ARN from the vpc id.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
LukvonStrom authored Oct 25, 2021
1 parent 8d0c555 commit 7b31376
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ec2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ By default, a new security group is created and logging is enabled. Moreover, a
authorize all users to the VPC CIDR is created.

To customize authorization rules, set the `authorizeAllUsersToVpcCidr` prop to `false`
and use `addaddAuthorizationRule()`:
and use `addAuthorizationRule()`:

```ts fixture=client-vpn
const endpoint = vpc.addClientVpnEndpoint('Endpoint', {
Expand Down
35 changes: 34 additions & 1 deletion packages/@aws-cdk/aws-ec2/lib/vpc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import {
Annotations, ConcreteDependable, ContextProvider, DependableTrait, IConstruct,
IDependable, IResource, Lazy, Resource, Stack, Token, Tags, Names,
IDependable, IResource, Lazy, Resource, Stack, Token, Tags, Names, Arn,
} from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
import { Construct, Node } from 'constructs';
Expand Down Expand Up @@ -78,6 +78,12 @@ export interface IVpc extends IResource {
*/
readonly vpcId: string;

/**
* ARN for this VPC
* @attribute
*/
readonly vpcArn: string;

/**
* CIDR range for this VPC
*
Expand Down Expand Up @@ -357,6 +363,11 @@ abstract class VpcBase extends Resource implements IVpc {
*/
public abstract readonly vpcId: string;

/**
* Arn of this VPC
*/
public abstract readonly vpcArn: string;

/**
* CIDR range for this VPC
*/
Expand Down Expand Up @@ -1153,6 +1164,11 @@ export class Vpc extends VpcBase {
*/
public readonly vpcId: string;

/**
* @attribute
*/
public readonly vpcArn: string;

/**
* @attribute
*/
Expand Down Expand Up @@ -1283,6 +1299,11 @@ export class Vpc extends VpcBase {
this.availabilityZones = this.availabilityZones.slice(0, maxAZs);

this.vpcId = this.resource.ref;
this.vpcArn = Arn.format({
service: 'ec2',
resource: 'vpc',
resourceName: this.vpcId,
}, stack);

const defaultSubnet = props.natGateways === 0 ? Vpc.DEFAULT_SUBNETS_NO_NAT : Vpc.DEFAULT_SUBNETS;
this.subnetConfiguration = ifUndefined(props.subnetConfiguration, defaultSubnet);
Expand Down Expand Up @@ -1859,6 +1880,7 @@ function ifUndefined<T>(value: T | undefined, defaultValue: T): T {

class ImportedVpc extends VpcBase {
public readonly vpcId: string;
public readonly vpcArn: string;
public readonly publicSubnets: ISubnet[];
public readonly privateSubnets: ISubnet[];
public readonly isolatedSubnets: ISubnet[];
Expand All @@ -1870,6 +1892,11 @@ class ImportedVpc extends VpcBase {
super(scope, id);

this.vpcId = props.vpcId;
this.vpcArn = Arn.format({
service: 'ec2',
resource: 'vpc',
resourceName: this.vpcId,
}, Stack.of(this));
this.cidr = props.vpcCidrBlock;
this.availabilityZones = props.availabilityZones;
this._vpnGatewayId = props.vpnGatewayId;
Expand Down Expand Up @@ -1903,6 +1930,7 @@ class ImportedVpc extends VpcBase {

class LookedUpVpc extends VpcBase {
public readonly vpcId: string;
public readonly vpcArn: string;
public readonly internetConnectivityEstablished: IDependable = new ConcreteDependable();
public readonly availabilityZones: string[];
public readonly publicSubnets: ISubnet[];
Expand All @@ -1914,6 +1942,11 @@ class LookedUpVpc extends VpcBase {
super(scope, id);

this.vpcId = props.vpcId;
this.vpcArn = Arn.format({
service: 'ec2',
resource: 'vpc',
resourceName: this.vpcId,
}, Stack.of(this));
this.cidr = props.vpcCidrBlock;
this._vpnGatewayId = props.vpnGatewayId;
this.incompleteSubnetDefinition = isIncomplete;
Expand Down
7 changes: 6 additions & 1 deletion packages/@aws-cdk/aws-ec2/test/vpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ describe('vpc', () => {
const stack = getTestStack();
const vpc = new Vpc(stack, 'TheVPC');
expect(stack.resolve(vpc.vpcId)).toEqual({ Ref: 'TheVPC92636AB0' });
});

test('vpc.vpcArn returns a token to the VPC ID', () => {
const stack = getTestStack();
const vpc = new Vpc(stack, 'TheVPC');
expect(stack.resolve(vpc.vpcArn)).toEqual({ 'Fn::Join': ['', ['arn:', { Ref: 'AWS::Partition' }, ':ec2:us-east-1:123456789012:vpc/', { Ref: 'TheVPC92636AB0' }]] });
});

test('it uses the correct network range', () => {
Expand Down Expand Up @@ -1786,4 +1791,4 @@ function hasTags(expectedTags: Array<{Key: string, Value: string}>): (props: any
throw e;
}
};
}
}

0 comments on commit 7b31376

Please sign in to comment.