@ttoss/cloud-vpc 0.1.28 → 0.1.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,232 @@
1
+ /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
+ var __defProp = Object.defineProperty;
3
+ var __name = (target, value) => __defProp(target, "name", {
4
+ value,
5
+ configurable: true
6
+ });
7
+
8
+ // src/index.ts
9
+ var NUMBER_OF_AVAILABILITY_ZONES = 3;
10
+ var PUBLIC_ROUTER_TABLE_LOGICAL_ID = "PublicRouteTable";
11
+ var PRIVATE_ROUTER_TABLE_LOGICAL_ID = "PrivateRouteTable";
12
+ var createVpcTemplate = /* @__PURE__ */__name(({
13
+ cidrBlock,
14
+ createPublicSubnets = true
15
+ }) => {
16
+ const totalOfSubnetsOnEachType = 2;
17
+ const getSubnetResource = /* @__PURE__ */__name(({
18
+ number,
19
+ isPublic
20
+ }) => {
21
+ const subnetType = isPublic ? "Public" : "Private";
22
+ const key = `${subnetType}Subnet${number}`;
23
+ const routerTableAssociationKey = `${subnetType}Subnet${number}RouteTableAssociation`;
24
+ const index = (isPublic ? 0 : 1) * totalOfSubnetsOnEachType + number - 1;
25
+ const azIndex = index % NUMBER_OF_AVAILABILITY_ZONES;
26
+ const cidrSubBlockCount = 2 * totalOfSubnetsOnEachType;
27
+ const cidrPrefixLength = parseInt(cidrBlock.split("/")[1], 10);
28
+ const cidrSubBlockBits = 32 - cidrPrefixLength - Math.ceil(Math.log2(cidrSubBlockCount));
29
+ let resources = {
30
+ [key]: {
31
+ Type: "AWS::EC2::Subnet",
32
+ Properties: {
33
+ AvailabilityZone: {
34
+ "Fn::Select": [azIndex, {
35
+ "Fn::GetAZs": ""
36
+ }]
37
+ },
38
+ CidrBlock: {
39
+ "Fn::Select": [index, {
40
+ "Fn::Cidr": [cidrBlock, cidrSubBlockCount, cidrSubBlockBits]
41
+ }]
42
+ },
43
+ MapPublicIpOnLaunch: isPublic,
44
+ VpcId: {
45
+ Ref: "Vpc"
46
+ },
47
+ Tags: [{
48
+ Key: "Name",
49
+ Value: {
50
+ "Fn::Sub": `\${Project}-${subnetType.toLowerCase()}-subnet-${number}-\${AWS::Region}`
51
+ }
52
+ }]
53
+ }
54
+ },
55
+ [routerTableAssociationKey]: {
56
+ Type: "AWS::EC2::SubnetRouteTableAssociation",
57
+ Properties: {
58
+ RouteTableId: {
59
+ Ref: isPublic ? PUBLIC_ROUTER_TABLE_LOGICAL_ID : PRIVATE_ROUTER_TABLE_LOGICAL_ID
60
+ },
61
+ SubnetId: {
62
+ Ref: key
63
+ }
64
+ }
65
+ }
66
+ };
67
+ if (isPublic) {
68
+ resources = {
69
+ ...resources
70
+ };
71
+ }
72
+ if (!isPublic) {
73
+ resources = {
74
+ ...resources
75
+ };
76
+ }
77
+ return resources;
78
+ }, "getSubnetResource");
79
+ const template = {
80
+ AWSTemplateFormatVersion: "2010-09-09",
81
+ Description: "VPC, Subnets, and Route Tables for the project.",
82
+ Parameters: {
83
+ Project: {
84
+ Type: "String",
85
+ Description: "The name of the project"
86
+ }
87
+ },
88
+ Resources: {
89
+ Vpc: {
90
+ Type: "AWS::EC2::VPC",
91
+ Properties: {
92
+ CidrBlock: cidrBlock,
93
+ EnableDnsHostnames: true,
94
+ EnableDnsSupport: true,
95
+ Tags: [{
96
+ Key: "Name",
97
+ Value: {
98
+ "Fn::Sub": "${Project}-vpc"
99
+ }
100
+ }]
101
+ }
102
+ },
103
+ ...getSubnetResource({
104
+ number: 1,
105
+ isPublic: false
106
+ }),
107
+ ...getSubnetResource({
108
+ number: 2,
109
+ isPublic: false
110
+ }),
111
+ [PRIVATE_ROUTER_TABLE_LOGICAL_ID]: {
112
+ Type: "AWS::EC2::RouteTable",
113
+ Properties: {
114
+ VpcId: {
115
+ Ref: "Vpc"
116
+ },
117
+ Tags: [{
118
+ Key: "Name",
119
+ Value: {
120
+ "Fn::Sub": "${Project}-private-rtb"
121
+ }
122
+ }]
123
+ }
124
+ }
125
+ },
126
+ Outputs: {
127
+ DefaultSecurityGroup: {
128
+ Description: "The default security group ID",
129
+ Value: {
130
+ "Fn::GetAtt": ["Vpc", "DefaultSecurityGroup"]
131
+ }
132
+ },
133
+ VpcId: {
134
+ Description: "The VPC ID",
135
+ Value: {
136
+ Ref: "Vpc"
137
+ }
138
+ },
139
+ PrivateSubnet1: {
140
+ Description: "The private subnet 1 ID",
141
+ Value: {
142
+ Ref: "PrivateSubnet1"
143
+ }
144
+ },
145
+ PrivateSubnet2: {
146
+ Description: "The private subnet 2 ID",
147
+ Value: {
148
+ Ref: "PrivateSubnet2"
149
+ }
150
+ }
151
+ }
152
+ };
153
+ if (createPublicSubnets) {
154
+ template.Resources = {
155
+ ...template.Resources,
156
+ ...getSubnetResource({
157
+ number: 1,
158
+ isPublic: true
159
+ }),
160
+ ...getSubnetResource({
161
+ number: 2,
162
+ isPublic: true
163
+ }),
164
+ InternetGateway: {
165
+ Type: "AWS::EC2::InternetGateway",
166
+ Properties: {
167
+ Tags: [{
168
+ Key: "Name",
169
+ Value: {
170
+ "Fn::Sub": "${Project}-igw"
171
+ }
172
+ }]
173
+ }
174
+ },
175
+ [PUBLIC_ROUTER_TABLE_LOGICAL_ID]: {
176
+ Type: "AWS::EC2::RouteTable",
177
+ Properties: {
178
+ VpcId: {
179
+ Ref: "Vpc"
180
+ },
181
+ Tags: [{
182
+ Key: "Name",
183
+ Value: {
184
+ "Fn::Sub": "${Project}-public-rtb"
185
+ }
186
+ }]
187
+ }
188
+ },
189
+ InternetGatewayAttachment: {
190
+ Type: "AWS::EC2::VPCGatewayAttachment",
191
+ Properties: {
192
+ InternetGatewayId: {
193
+ Ref: "InternetGateway"
194
+ },
195
+ VpcId: {
196
+ Ref: "Vpc"
197
+ }
198
+ }
199
+ },
200
+ PublicRoute: {
201
+ Type: "AWS::EC2::Route",
202
+ DependsOn: "InternetGatewayAttachment",
203
+ Properties: {
204
+ DestinationCidrBlock: "0.0.0.0/0",
205
+ GatewayId: {
206
+ Ref: "InternetGateway"
207
+ },
208
+ RouteTableId: {
209
+ Ref: PUBLIC_ROUTER_TABLE_LOGICAL_ID
210
+ }
211
+ }
212
+ }
213
+ };
214
+ template.Outputs = {
215
+ ...template.Outputs,
216
+ PublicSubnet1: {
217
+ Description: "The public subnet 1 ID",
218
+ Value: {
219
+ Ref: "PublicSubnet1"
220
+ }
221
+ },
222
+ PublicSubnet2: {
223
+ Description: "The public subnet 2 ID",
224
+ Value: {
225
+ Ref: "PublicSubnet2"
226
+ }
227
+ }
228
+ };
229
+ }
230
+ return template;
231
+ }, "createVpcTemplate");
232
+ export { createVpcTemplate };
@@ -0,0 +1,9 @@
1
+ import { CloudFormationTemplate } from '@ttoss/cloudformation';
2
+ export { CloudFormationTemplate } from '@ttoss/cloudformation';
3
+
4
+ declare const createVpcTemplate: ({ cidrBlock, createPublicSubnets, }: {
5
+ cidrBlock: string;
6
+ createPublicSubnets?: boolean;
7
+ }) => CloudFormationTemplate;
8
+
9
+ export { createVpcTemplate };
@@ -0,0 +1,9 @@
1
+ import { CloudFormationTemplate } from '@ttoss/cloudformation';
2
+ export { CloudFormationTemplate } from '@ttoss/cloudformation';
3
+
4
+ declare const createVpcTemplate: ({ cidrBlock, createPublicSubnets, }: {
5
+ cidrBlock: string;
6
+ createPublicSubnets?: boolean;
7
+ }) => CloudFormationTemplate;
8
+
9
+ export { createVpcTemplate };
package/dist/index.js ADDED
@@ -0,0 +1,263 @@
1
+ /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
+ "use strict";
3
+
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __name = (target, value) => __defProp(target, "name", {
9
+ value,
10
+ configurable: true
11
+ });
12
+ var __export = (target, all) => {
13
+ for (var name in all) __defProp(target, name, {
14
+ get: all[name],
15
+ enumerable: true
16
+ });
17
+ };
18
+ var __copyProps = (to, from, except, desc) => {
19
+ if (from && typeof from === "object" || typeof from === "function") {
20
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
21
+ get: () => from[key],
22
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
23
+ });
24
+ }
25
+ return to;
26
+ };
27
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
28
+ value: true
29
+ }), mod);
30
+
31
+ // src/index.ts
32
+ var index_exports = {};
33
+ __export(index_exports, {
34
+ createVpcTemplate: () => createVpcTemplate
35
+ });
36
+ module.exports = __toCommonJS(index_exports);
37
+ var NUMBER_OF_AVAILABILITY_ZONES = 3;
38
+ var PUBLIC_ROUTER_TABLE_LOGICAL_ID = "PublicRouteTable";
39
+ var PRIVATE_ROUTER_TABLE_LOGICAL_ID = "PrivateRouteTable";
40
+ var createVpcTemplate = /* @__PURE__ */__name(({
41
+ cidrBlock,
42
+ createPublicSubnets = true
43
+ }) => {
44
+ const totalOfSubnetsOnEachType = 2;
45
+ const getSubnetResource = /* @__PURE__ */__name(({
46
+ number,
47
+ isPublic
48
+ }) => {
49
+ const subnetType = isPublic ? "Public" : "Private";
50
+ const key = `${subnetType}Subnet${number}`;
51
+ const routerTableAssociationKey = `${subnetType}Subnet${number}RouteTableAssociation`;
52
+ const index = (isPublic ? 0 : 1) * totalOfSubnetsOnEachType + number - 1;
53
+ const azIndex = index % NUMBER_OF_AVAILABILITY_ZONES;
54
+ const cidrSubBlockCount = 2 * totalOfSubnetsOnEachType;
55
+ const cidrPrefixLength = parseInt(cidrBlock.split("/")[1], 10);
56
+ const cidrSubBlockBits = 32 - cidrPrefixLength - Math.ceil(Math.log2(cidrSubBlockCount));
57
+ let resources = {
58
+ [key]: {
59
+ Type: "AWS::EC2::Subnet",
60
+ Properties: {
61
+ AvailabilityZone: {
62
+ "Fn::Select": [azIndex, {
63
+ "Fn::GetAZs": ""
64
+ }]
65
+ },
66
+ CidrBlock: {
67
+ "Fn::Select": [index, {
68
+ "Fn::Cidr": [cidrBlock, cidrSubBlockCount, cidrSubBlockBits]
69
+ }]
70
+ },
71
+ MapPublicIpOnLaunch: isPublic,
72
+ VpcId: {
73
+ Ref: "Vpc"
74
+ },
75
+ Tags: [{
76
+ Key: "Name",
77
+ Value: {
78
+ "Fn::Sub": `\${Project}-${subnetType.toLowerCase()}-subnet-${number}-\${AWS::Region}`
79
+ }
80
+ }]
81
+ }
82
+ },
83
+ [routerTableAssociationKey]: {
84
+ Type: "AWS::EC2::SubnetRouteTableAssociation",
85
+ Properties: {
86
+ RouteTableId: {
87
+ Ref: isPublic ? PUBLIC_ROUTER_TABLE_LOGICAL_ID : PRIVATE_ROUTER_TABLE_LOGICAL_ID
88
+ },
89
+ SubnetId: {
90
+ Ref: key
91
+ }
92
+ }
93
+ }
94
+ };
95
+ if (isPublic) {
96
+ resources = {
97
+ ...resources
98
+ };
99
+ }
100
+ if (!isPublic) {
101
+ resources = {
102
+ ...resources
103
+ };
104
+ }
105
+ return resources;
106
+ }, "getSubnetResource");
107
+ const template = {
108
+ AWSTemplateFormatVersion: "2010-09-09",
109
+ Description: "VPC, Subnets, and Route Tables for the project.",
110
+ Parameters: {
111
+ Project: {
112
+ Type: "String",
113
+ Description: "The name of the project"
114
+ }
115
+ },
116
+ Resources: {
117
+ Vpc: {
118
+ Type: "AWS::EC2::VPC",
119
+ Properties: {
120
+ CidrBlock: cidrBlock,
121
+ EnableDnsHostnames: true,
122
+ EnableDnsSupport: true,
123
+ Tags: [{
124
+ Key: "Name",
125
+ Value: {
126
+ "Fn::Sub": "${Project}-vpc"
127
+ }
128
+ }]
129
+ }
130
+ },
131
+ ...getSubnetResource({
132
+ number: 1,
133
+ isPublic: false
134
+ }),
135
+ ...getSubnetResource({
136
+ number: 2,
137
+ isPublic: false
138
+ }),
139
+ [PRIVATE_ROUTER_TABLE_LOGICAL_ID]: {
140
+ Type: "AWS::EC2::RouteTable",
141
+ Properties: {
142
+ VpcId: {
143
+ Ref: "Vpc"
144
+ },
145
+ Tags: [{
146
+ Key: "Name",
147
+ Value: {
148
+ "Fn::Sub": "${Project}-private-rtb"
149
+ }
150
+ }]
151
+ }
152
+ }
153
+ },
154
+ Outputs: {
155
+ DefaultSecurityGroup: {
156
+ Description: "The default security group ID",
157
+ Value: {
158
+ "Fn::GetAtt": ["Vpc", "DefaultSecurityGroup"]
159
+ }
160
+ },
161
+ VpcId: {
162
+ Description: "The VPC ID",
163
+ Value: {
164
+ Ref: "Vpc"
165
+ }
166
+ },
167
+ PrivateSubnet1: {
168
+ Description: "The private subnet 1 ID",
169
+ Value: {
170
+ Ref: "PrivateSubnet1"
171
+ }
172
+ },
173
+ PrivateSubnet2: {
174
+ Description: "The private subnet 2 ID",
175
+ Value: {
176
+ Ref: "PrivateSubnet2"
177
+ }
178
+ }
179
+ }
180
+ };
181
+ if (createPublicSubnets) {
182
+ template.Resources = {
183
+ ...template.Resources,
184
+ ...getSubnetResource({
185
+ number: 1,
186
+ isPublic: true
187
+ }),
188
+ ...getSubnetResource({
189
+ number: 2,
190
+ isPublic: true
191
+ }),
192
+ InternetGateway: {
193
+ Type: "AWS::EC2::InternetGateway",
194
+ Properties: {
195
+ Tags: [{
196
+ Key: "Name",
197
+ Value: {
198
+ "Fn::Sub": "${Project}-igw"
199
+ }
200
+ }]
201
+ }
202
+ },
203
+ [PUBLIC_ROUTER_TABLE_LOGICAL_ID]: {
204
+ Type: "AWS::EC2::RouteTable",
205
+ Properties: {
206
+ VpcId: {
207
+ Ref: "Vpc"
208
+ },
209
+ Tags: [{
210
+ Key: "Name",
211
+ Value: {
212
+ "Fn::Sub": "${Project}-public-rtb"
213
+ }
214
+ }]
215
+ }
216
+ },
217
+ InternetGatewayAttachment: {
218
+ Type: "AWS::EC2::VPCGatewayAttachment",
219
+ Properties: {
220
+ InternetGatewayId: {
221
+ Ref: "InternetGateway"
222
+ },
223
+ VpcId: {
224
+ Ref: "Vpc"
225
+ }
226
+ }
227
+ },
228
+ PublicRoute: {
229
+ Type: "AWS::EC2::Route",
230
+ DependsOn: "InternetGatewayAttachment",
231
+ Properties: {
232
+ DestinationCidrBlock: "0.0.0.0/0",
233
+ GatewayId: {
234
+ Ref: "InternetGateway"
235
+ },
236
+ RouteTableId: {
237
+ Ref: PUBLIC_ROUTER_TABLE_LOGICAL_ID
238
+ }
239
+ }
240
+ }
241
+ };
242
+ template.Outputs = {
243
+ ...template.Outputs,
244
+ PublicSubnet1: {
245
+ Description: "The public subnet 1 ID",
246
+ Value: {
247
+ Ref: "PublicSubnet1"
248
+ }
249
+ },
250
+ PublicSubnet2: {
251
+ Description: "The public subnet 2 ID",
252
+ Value: {
253
+ Ref: "PublicSubnet2"
254
+ }
255
+ }
256
+ };
257
+ }
258
+ return template;
259
+ }, "createVpcTemplate");
260
+ // Annotate the CommonJS export names for ESM import in node:
261
+ 0 && (module.exports = {
262
+ createVpcTemplate
263
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/cloud-vpc",
3
- "version": "0.1.28",
3
+ "version": "0.1.29",
4
4
  "description": "Deploy a VPC with public and private subnets",
5
5
  "license": "MIT",
6
6
  "author": "ttoss",
@@ -23,14 +23,14 @@
23
23
  "dist"
24
24
  ],
25
25
  "dependencies": {
26
- "@ttoss/cloudformation": "^0.11.9"
26
+ "@ttoss/cloudformation": "^0.11.10"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@types/jest": "^30.0.0",
30
30
  "jest": "^30.2.0",
31
31
  "tsup": "^8.5.1",
32
32
  "@ttoss/config": "^1.35.12",
33
- "@ttoss/test-utils": "^4.0.2"
33
+ "@ttoss/test-utils": "^4.0.3"
34
34
  },
35
35
  "keywords": [
36
36
  "aws",