Created
September 3, 2014 22:20
-
-
Save garethr/2c4e5e406a6a7598c428 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'aws-sdk-core' | |
require 'colorize' | |
require 'retries' | |
ec2 = Aws::EC2::Client.new(region: 'us-west-2') | |
## Security Groups | |
puts '===> creating security groups'.green | |
['web-sg', 'db-sg', 'lb-sg'].each do |group| | |
begin | |
ec2.create_security_group( | |
group_name: group, | |
description: "#{group} security group", | |
) | |
rescue Aws::EC2::Errors::InvalidGroupDuplicate | |
puts "skipping #{group}".yellow | |
end | |
end | |
puts '===> configuring security groups'.green | |
begin | |
ec2.authorize_security_group_ingress( | |
group_name: 'lb-sg', | |
ip_permissions: [ | |
{ | |
ip_protocol: 'tcp', | |
to_port: 80, | |
from_port: 80, | |
ip_ranges: [ | |
{ | |
cidr_ip: '0.0.0.0/0', | |
}, | |
] | |
} | |
] | |
) | |
rescue Aws::EC2::Errors::InvalidPermissionDuplicate | |
puts "skipping".yellow | |
end | |
begin | |
ec2.authorize_security_group_ingress( | |
group_name: 'web-sg', | |
source_security_group_name: 'lb-sg', | |
) | |
rescue Aws::EC2::Errors::InvalidPermissionDuplicate | |
puts "skipping".yellow | |
end | |
begin | |
ec2.authorize_security_group_ingress( | |
group_name: 'db-sg', | |
source_security_group_name: 'web-sg', | |
) | |
rescue Aws::EC2::Errors::InvalidPermissionDuplicate | |
puts "skipping".yellow | |
end | |
## Instances | |
puts '===> creating instances'.green | |
ec2.run_instances( | |
image_id: 'ami-2d9add1d', | |
min_count: 2, | |
max_count: 2, | |
security_groups: ['web-sg'], | |
instance_type: 't1.micro', | |
) | |
ec2.run_instances( | |
image_id: 'ami-2d9add1d', | |
min_count: 1, | |
max_count: 1, | |
security_groups: ['db-sg'], | |
instance_type: 't1.micro', | |
) | |
## Load Balancer | |
puts '===> creating load balancer'.green | |
resp = ec2.describe_security_groups(group_names: ['lb-sg']) | |
security_group_id = resp.data.security_groups.first.group_id | |
elb = Aws::ElasticLoadBalancing::Client.new(region: 'us-west-2') | |
elb.create_load_balancer( | |
load_balancer_name: 'lb-1', | |
listeners: [ | |
{ | |
protocol: 'tcp', | |
load_balancer_port: 80, | |
instance_protocol: 'tcp', | |
instance_port: 80, | |
}, | |
], | |
availability_zones: ['us-west-2b'], | |
security_groups: [security_group_id], | |
) | |
puts '===> configuring load balancer'.green | |
resp = ec2.describe_security_groups(group_names: ['web-sg']) | |
security_group_id = resp.data.security_groups.first.group_id | |
resp = ec2.describe_instances( | |
filters: [ | |
{ | |
name: 'instance.group-id', | |
values: [security_group_id], | |
}, | |
], | |
) | |
web1 = resp.data.reservations.first.instances[0].instance_id | |
web2 = resp.data.reservations.first.instances[1].instance_id | |
elb.register_instances_with_load_balancer( | |
load_balancer_name: 'lb-1', | |
instances: [ | |
{ | |
instance_id: web1, | |
}, | |
{ | |
instance_id: web2, | |
}, | |
], | |
) | |
puts '===> and sleep' | |
sleep 10 | |
puts '===> deleting instances'.red | |
resp = ec2.describe_security_groups(group_names: ['db-sg']) | |
security_group_id = resp.data.security_groups.first.group_id | |
resp = ec2.describe_instances( | |
filters: [ | |
{ | |
name: 'instance.group-id', | |
values: [security_group_id], | |
}, | |
], | |
) | |
db1 = resp.data.reservations.first.instances[0].instance_id | |
ec2.terminate_instances( | |
instance_ids: [web1, web2, db1], | |
) | |
puts '===> deleting load balancer'.red | |
elb.delete_load_balancer( | |
load_balancer_name: 'lb-1', | |
) | |
puts '===> deleting security groups'.red | |
with_retries(:max_tries => 5, :base_sleep_seconds => 30.0, | |
:max_sleep_seconds => 120.0, :rescue => Aws::EC2::Errors::DependencyViolation) do |attempt_number| | |
puts "trying to delete (#{attempt_number}/5)".yellow | |
['db-sg', 'web-sg', 'lb-sg'].each do |group| | |
begin | |
ec2.delete_security_group( | |
group_name: group | |
) | |
rescue Aws::EC2::Errors::InvalidGroupNotFound | |
# already deleted | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hah, always love the sleeps... in the past I've polled the status api on a 1 second interval.