1 | var AWS = require('aws-sdk');
|
2 | var utils = require('../lib/utils');
|
3 | var Response = require('../lib/response');
|
4 |
|
5 | module.exports = function(event, context) {
|
6 | if (!utils.validCloudFormationEvent(event))
|
7 | return context.done(null, 'ERROR: Invalid CloudFormation event');
|
8 | var response = new Response(event, context);
|
9 |
|
10 | var requestType = event.RequestType.toLowerCase();
|
11 | if (requestType === 'delete') return response.send();
|
12 |
|
13 | var ec2 = new AWS.EC2();
|
14 | var info = {};
|
15 |
|
16 | ec2.describeVpcs({
|
17 | Filters: [{ Name: 'isDefault', Values: ['true'] }]
|
18 | }).promise().then((data) => {
|
19 | info.VpcId = data.Vpcs[0].VpcId;
|
20 | return Promise.all([
|
21 | ec2.describeSubnets({ Filters: [{ Name: 'vpc-id', Values: [info.VpcId] }] }).promise(),
|
22 | ec2.describeRouteTables({ Filters: [{ Name: 'vpc-id', Values: [info.VpcId] }] }).promise()
|
23 | ]);
|
24 | }).then((results) => {
|
25 | var publicSubnets = results[0].Subnets.filter((subnet) => subnet.MapPublicIpOnLaunch);
|
26 | var privateSubnets = results[0].Subnets.filter((subnet) => !subnet.MapPublicIpOnLaunch);
|
27 |
|
28 | info.AvailabilityZones = publicSubnets.map((subnet) => subnet.AvailabilityZone);
|
29 | info.AvailabilityZoneCount = publicSubnets.length;
|
30 | info.PrivateSubnetAvailabilityZones = privateSubnets.map((subnet) => subnet.AvailabilityZone);
|
31 | info.PrivateSubnetAvailabilityZoneCount = privateSubnets.length;
|
32 |
|
33 | info.AzIndexedPrivateSubnets = ['a', 'b', 'c', 'd', 'e', 'f'].map((letter) => {
|
34 | const subnet = privateSubnets.find((s) => s.AvailabilityZone.slice(-1) === letter);
|
35 | if (subnet) return subnet.SubnetId;
|
36 | else return { Ref: 'AWS::NoValue' }
|
37 | });
|
38 |
|
39 | info.AzIndexedPublicSubnets = ['a', 'b', 'c', 'd', 'e', 'f'].map((letter) => {
|
40 | const subnet = publicSubnets.find((s) => s.AvailabilityZone.slice(-1) === letter);
|
41 | if (subnet) return subnet.SubnetId;
|
42 | else return { Ref: 'AWS::NoValue' }
|
43 | });
|
44 |
|
45 | info.PublicSubnets = publicSubnets.map((subnet) => subnet.SubnetId);
|
46 | info.PrivateSubnets = privateSubnets.map((subnet) => subnet.SubnetId);
|
47 | info.RouteTable = results[1].RouteTables[0].RouteTableId;
|
48 | info.RouteTables = results[1].RouteTables.map((rt) => rt.RouteTableId);
|
49 | response.setId(info.VpcId);
|
50 | response.send(null, info);
|
51 | }).catch((err) => response.send(err));
|
52 | };
|