Created
February 24, 2015 16:03
-
-
Save james-prickett/1b37ab98afc564eec39e to your computer and use it in GitHub Desktop.
Ruby scripts to create a load balanced cluster on AWS.
This file contains hidden or 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
#!/usr/bin/env ruby | |
require 'aws-sdk' | |
@elb = Aws::ElasticLoadBalancing::Client.new(region: 'us-east-1') | |
@ec2 = Aws::EC2::Client.new(region: 'us-east-1') | |
def create_load_balancer | |
response = @elb.create_load_balancer( | |
load_balancer_name: "jboss-cluster-lb", | |
listeners: [ | |
{ | |
protocol: "HTTP", | |
load_balancer_port: "80", | |
instance_protocol: "HTTP", | |
instance_port: "8080" | |
} | |
], | |
subnets: ["subnet-df8225a8"], | |
security_groups: ["sg-4463b820"], | |
) | |
end | |
def create_ec2_instances(number_of_instances) | |
response = @ec2.run_instances( | |
image_id: 'ami-0a96d662', | |
min_count: 1, | |
max_count: number_of_instances, | |
key_name: 'jprickett', | |
security_group_ids: ['sg-4463b820', 'sg-724d9716'], | |
instance_type: 'c4.large', | |
placement: { | |
availability_zone: 'us-east-1b', | |
group_name: "Wildfly Servers" | |
}, | |
subnet_id: 'subnet-df8225a8', | |
monitoring: { | |
enabled: true, | |
} | |
) | |
response.instances | |
end | |
create_load_balancer() | |
instances = create_ec2_instances(3) | |
instance_ids = instances.map { |instance| { instance_id: instance.instance_id } } | |
@elb.register_instances_with_load_balancer( | |
load_balancer_name: "jboss-cluster-lb", | |
instances: instance_ids | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment