-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
vpc-link.ts
134 lines (114 loc) · 3.37 KB
/
vpc-link.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { Construct } from 'constructs';
import { CfnVpcLink } from '.././index';
import * as ec2 from '../../../aws-ec2';
import { IResource, Lazy, Names, Resource } from '../../../core';
/**
* Represents an API Gateway VpcLink
*/
export interface IVpcLink extends IResource {
/**
* Physical ID of the VpcLink resource
* @attribute
*/
readonly vpcLinkId: string;
/**
* The VPC to which this VPC Link is associated with.
*/
readonly vpc: ec2.IVpc;
}
/**
* Properties for a VpcLink
*/
export interface VpcLinkProps {
/**
* The VPC in which the private resources reside.
*/
readonly vpc: ec2.IVpc;
/**
* The name used to label and identify the VPC link.
* @default - automatically generated name
*/
readonly vpcLinkName?: string;
/**
* A list of subnets for the VPC link.
*
* @default - private subnets of the provided VPC. Use `addSubnets` to add more subnets
*/
readonly subnets?: ec2.SubnetSelection;
/**
* A list of security groups for the VPC link.
*
* @default - no security groups. Use `addSecurityGroups` to add security groups
*/
readonly securityGroups?: ec2.ISecurityGroup[];
}
/**
* Attributes when importing a new VpcLink
*/
export interface VpcLinkAttributes {
/**
* The VPC Link id
*/
readonly vpcLinkId: string;
/**
* The VPC to which this VPC link is associated with.
*/
readonly vpc: ec2.IVpc;
}
/**
* Define a new VPC Link
* Specifies an API Gateway VPC link for a HTTP API to access resources in an Amazon Virtual Private Cloud (VPC).
*/
export class VpcLink extends Resource implements IVpcLink {
/**
* Import a VPC Link by specifying its attributes.
*/
public static fromVpcLinkAttributes(scope: Construct, id: string, attrs: VpcLinkAttributes): IVpcLink {
class Import extends Resource implements IVpcLink {
public vpcLinkId = attrs.vpcLinkId;
public vpc = attrs.vpc;
}
return new Import(scope, id);
}
public readonly vpcLinkId: string;
public readonly vpc: ec2.IVpc;
private readonly subnets = new Array<ec2.ISubnet>();
private readonly securityGroups = new Array<ec2.ISecurityGroup>();
constructor(scope: Construct, id: string, props: VpcLinkProps) {
super(scope, id);
this.vpc = props.vpc;
const cfnResource = new CfnVpcLink(this, 'Resource', {
name: props.vpcLinkName || Lazy.string({ produce: () => Names.uniqueId(this) }),
subnetIds: Lazy.list({ produce: () => this.renderSubnets() }),
securityGroupIds: Lazy.list({ produce: () => this.renderSecurityGroups() }),
});
this.vpcLinkId = cfnResource.ref;
const { subnets } = props.vpc.selectSubnets(props.subnets ?? { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS });
this.addSubnets(...subnets);
if (props.securityGroups) {
this.addSecurityGroups(...props.securityGroups);
}
}
/**
* Adds the provided subnets to the vpc link
*
* @param subnets
*/
public addSubnets(...subnets: ec2.ISubnet[]) {
this.subnets.push(...subnets);
}
/**
* Adds the provided security groups to the vpc link
*
* @param groups
*/
public addSecurityGroups(...groups: ec2.ISecurityGroup[]) {
this.securityGroups.push(...groups);
}
private renderSubnets() {
return this.subnets.map(subnet => subnet.subnetId);
}
private renderSecurityGroups() {
return this.securityGroups.map(sg => sg.securityGroupId);
}
}