-
Notifications
You must be signed in to change notification settings - Fork 0
/
apigateway-vpc.js
108 lines (97 loc) · 2.79 KB
/
apigateway-vpc.js
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
const cdktf = require("cdktf");
const aws_security_group = require("@cdktf/provider-aws/lib/security-group");
const vpc_endpoint = require("@cdktf/provider-aws/lib/vpc-endpoint");
const vpc_endpoint_service = require("@cdktf/provider-aws/lib/data-aws-vpc-endpoint-service");
class TFLambdaWithVPCAspect {
constructor(vpcId, privateSubnetId) {
this.vpcId = vpcId;
this.privateSubnetId = privateSubnetId;
}
visit(node) {
if (node.constructor.name !== "Function") {
return;
}
const securityGroup = new aws_security_group.SecurityGroup(node, "SecurityGroup", {
vpcId: this.vpcId,
egress: [
{
cidrBlocks: ["0.0.0.0/0"],
fromPort: 0,
toPort: 0,
protocol: "-1"
}
]
});
node.addNetworkConfig({
securityGroupIds: [securityGroup.id],
subnetIds: [this.privateSubnetId]
});
}
}
class TFApiWithVPCAspect {
constructor(vpcId, privateSubnetId) {
this.vpcId = vpcId;
this.privateSubnetId = privateSubnetId;
}
visit(node) {
if (node.terraformResourceType !== "aws_api_gateway_rest_api") {
return
}
const scope = node.node.scope;
const serviceName = new vpc_endpoint_service.DataAwsVpcEndpointService(scope, "VpcEndpointService", {
service: "execute-api"
});
const securityGroup = new aws_security_group.SecurityGroup(scope, "SecurityGroup", {
vpcId: this.vpcId,
ingress: [
{
cidrBlocks: ["0.0.0.0/0"],
fromPort: 0,
toPort: 0,
protocol: "-1"
}
]
});
const vpcEndpoint = new vpc_endpoint.VpcEndpoint(scope, "VpcEndpoint", {
vpcId: this.vpcId,
serviceName: serviceName.serviceName,
vpcEndpointType: "Interface",
privateDnsEnabled: true,
subnetIds: [this.privateSubnetId],
securityGroupIds: [securityGroup.id]
});
// API Gateway
const api = node;
api.endpointConfiguration.types = ["PRIVATE"];
api.endpointConfiguration.vpcEndpointIds = [vpcEndpoint.id];
api.policy = JSON.stringify({
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Principal: "*",
Action: "execute-api:Invoke",
Resource: ["*"],
},
{
Effect: "Deny",
Principal: "*",
Action: "execute-api:Invoke",
Resource: ["*"],
Condition: {
StringNotEquals: {
"aws:SourceVpce": vpcEndpoint.id,
},
},
},
],
});
}
}
exports.Platform = class TFApiWithVPC {
target = "tf-aws";
preSynth(app) {
cdktf.Aspects.of(app).add(new TFApiWithVPCAspect(app.vpc.id, app.subnets.private.id));
cdktf.Aspects.of(app).add(new TFLambdaWithVPCAspect(app.vpc.id, app.subnets.private.id));
}
};