-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_vpc.rb
More file actions
80 lines (60 loc) · 2.3 KB
/
create_vpc.rb
File metadata and controls
80 lines (60 loc) · 2.3 KB
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
require File.expand_path(File.dirname(__FILE__) + '/config')
require File.expand_path(File.dirname(__FILE__) + '/get_vpc_status')
def create_public_facing_subnets(ec2Client, cidr, az, vpc, routeTable)
# Create the subnet
subnet = ec2Client.create_subnet({
:vpc_id => vpc[:vpc_id],
:cidr_block => cidr,
:availability_zone => az
})[:subnet]
puts "created subnet #{subnet}"
rtAssociation = ec2Client.associate_route_table({
:subnet_id => subnet[:subnet_id],
:route_table_id => routeTable[:route_table_id]
})[:association_id]
puts "Associated route to subnet - #{rtAssociation}"
end
def create_private_subnets(ec2Client, cidr, az, vpc)
privateSubnet = ec2Client.create_subnet({
:vpc_id => vpc[:vpc_id],
:cidr_block => cidr,
:availability_zone => az
})[:subnet]
ec2Client.create_tags({
:resources => [
privateSubnet[:subnet_id]
],
:tags => [{:key => "access", :value => "private"}]
})
end
ec2Client = Aws::EC2::Client.new
#Create a VPC
puts "create VPC..."
vpc = ec2Client.create_vpc({ :cidr_block => "10.0.0.0/16"})[:vpc]
puts "created vpc #{vpc[:vpc_id]}"
wait_until_vpc_available(ec2Client, vpc[:vpc_id])
puts "vpc is available"
# Create and attach an internet gateway
igw = ec2Client.create_internet_gateway()[:internet_gateway]
puts "created internet gateway #{igw}"
ec2Client.attach_internet_gateway({
:internet_gateway_id => igw[:internet_gateway_id],
:vpc_id => vpc[:vpc_id]
})
#Create a route table
routeTable = ec2Client.create_route_table({:vpc_id => vpc[:vpc_id]})[:route_table]
puts "create route table #{routeTable}"
# Create a route to the gateway, and associate it with the subnet
ec2Client.create_route({
:route_table_id => routeTable[:route_table_id],
:destination_cidr_block => "0.0.0.0/0",
:gateway_id => igw[:internet_gateway_id]
})
# Create two public subnets
create_public_facing_subnets(ec2Client, "10.0.0.0/24", "us-east-1a", vpc, routeTable)
create_public_facing_subnets(ec2Client, "10.0.2.0/24", "us-east-1c", vpc, routeTable)
# Create two private subnets (RDS needs two availability zones), and tag them
# as private
create_private_subnets(ec2Client, "10.0.1.0/24", "us-east-1a", vpc)
create_private_subnets(ec2Client, "10.0.3.0/24", "us-east-1c", vpc)
puts "finished configuration of #{vpc[:vpc_id]}"