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(ec2): add vpcArn to IVpc and Vpc #16666

Merged
merged 5 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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;
}
};
}
}