-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunch_rds_from_snapshot.rb
More file actions
106 lines (77 loc) · 2.34 KB
/
launch_rds_from_snapshot.rb
File metadata and controls
106 lines (77 loc) · 2.34 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
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
require File.expand_path(File.dirname(__FILE__) + '/config')
def wait_for_available_db(rdsClient, dbName)
dbAvailable = false
while !dbAvailable do
sleep(15)
puts "Check db status at #{Time.now}"
dbStatus = rdsClient.describe_db_instances({
:db_instance_identifier => dbName
})[:db_instances].first[:db_instance_status]
if(dbStatus == "available")
dbAvailable = true
end
end
end
(vpc_id, dbname, snapshot_id) = ARGV
unless vpc_id && dbname && snapshot_id
puts "Usage: launch_rds_from_snapshot <VPC_ID> <dbname> <snapshot_id>"
exit 1
end
ec2Client = Aws::EC2::Client.new
# Grab the private subnets. Note this assumes we know the subnets we
# are looking for via knowledge of how the VPC was created.
subnet_infos = ec2Client.describe_subnets({
:filters => [
{
:name => "vpc-id",
:values => [vpc_id]
},
{
:name => "cidr",
:values => ["10.0.1.0/24", "10.0.3.0/24"]
}
]
})[:subnets]
subnet_ids = subnet_infos.map do |subnet|
subnet[:subnet_id]
end
puts subnet_ids
# Need the security group name
sg_infos = ec2Client.describe_security_groups({
:filters => [
{
:name => "vpc-id",
:values => [vpc_id]
}
]
})[:security_groups]
private_launch_sg = (sg_infos.select {
|sg| sg[:group_name] == "sqlnet-sg"
}).first[:group_id]
puts private_launch_sg
# Create an RDS subnet group containing the two private subnets
rdsClient = Aws::RDS::Client::new
db_subnet_group_name = dbname + "-subnet-group"
db_subnet_group = rdsClient.create_db_subnet_group({
:db_subnet_group_name => db_subnet_group_name,
:db_subnet_group_description => "VPC DB Subnet Group",
:subnet_ids => subnet_ids
})
# Create an RDS instance in the VPC
rdsCreateDB = rdsClient.restore_db_instance_from_db_snapshot({
:db_snapshot_identifier => snapshot_id,
:db_instance_identifier => dbname,
:db_instance_class => "db.t1.micro",
:db_subnet_group_name => db_subnet_group_name,
:multi_az => false,
:publicly_accessible => false
})[:db_instance][:db_instance_identifier]
# Wait for the db state to be available
wait_for_available_db(rdsClient, dbname)
# Update the security group
puts "Update vpc security group"
rdsClient.modify_db_instance({
:db_instance_identifier => dbname,
:vpc_security_group_ids => [private_launch_sg]
})
puts "Done"