Last active
February 13, 2016 08:53
-
-
Save akira345/35bdfa1d63130eae7a8b to your computer and use it in GitHub Desktop.
ELBを作成するRubyスクリプトです。
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
# -*- coding: utf-8 -*- | |
# | |
# ELBを作成するスクリプトです。ついでにSSL証明書もセットしますが、EC2をぶら下げるまではしません。 | |
# 要 AWS SDK for Ruby V2 | |
require 'aws-sdk-core' | |
require 'yaml' | |
require 'pp' | |
config = YAML.load(File.read("config.yml")) | |
Aws.config[:credentials] = Aws::Credentials.new(config['access_key_id'],config['secret_access_key']) | |
iam = Aws::IAM::Client.new(region:config['region']) | |
ec2 = Aws::EC2::Client.new(region:config['region']) | |
elb = Aws::ElasticLoadBalancing::Client.new(region:config['region']) | |
###### 設定 ########################################### | |
elb_name = "example-elb" # 作成するELB名 | |
elb_comment = "サンプルELB" # 作成するELBのコメント | |
# 証明書周り | |
certificate_name = "" # AWSにアップロード済み証明書名。無指定もしくは存在しない場合はセットした証明書をアップロードして適用する。 | |
# 証明書ファイル。certificate_nameが指定されている場合は無視される | |
ca_file = "www.example.com.ca" # 中間証明書ファイル | |
crt_file = "www.example.com.crt" # 証明書ファイル | |
key_file = "www.example.com.key" # 秘密鍵ファイル | |
# セキュリティグループやサブネット周り | |
elb_security_group_names = ["default-vpc"] # ELBにセットするセキュリティグループ名 | |
vpc_name = "aws-vpc" # VPC名 | |
subnet_names = ["Public subnet-a","Public subnet-c"] # ELBにぶら下げるサブネット | |
####################################################### | |
def get_subbet_ids(ec2,vpc_id,subnet_names) | |
#サブネットIDを取得 | |
begin | |
tmp = [] | |
ec2.describe_subnets({ | |
filters: [ | |
{ name: "vpc-id",values: [vpc_id] }, | |
{ name: "tag:Name", values: subnet_names } | |
] | |
}).subnets.each do |s| | |
tmp.push(s.subnet_id) | |
end | |
return tmp.to_a | |
rescue => e | |
pp "エラー発生!" | |
pp e.message | |
exit 1 | |
end | |
end | |
def get_vpc_id(ec2,vpc_name) | |
# VPC IDを取得 | |
begin | |
ec2.describe_vpcs({ | |
filters: [ | |
{ name: "tag:Name", values: [vpc_name] } | |
] | |
}).vpcs.each do |v| | |
return v.vpc_id | |
end | |
rescue => e | |
pp "エラー発生!" | |
pp e.message | |
exit 1 | |
end | |
end | |
def get_security_group_ids(ec2,vpc_id, elb_security_group_names) | |
#セキュリティグループIDを取得 | |
begin | |
tmp = [] | |
ec2.describe_security_groups({ | |
filters: [ | |
{ name: "vpc-id",values: [vpc_id] }, | |
{ name: "group-name", values: elb_security_group_names } | |
] | |
}).security_groups.each do |s| | |
tmp.push(s.group_id) | |
end | |
return tmp.to_a | |
rescue => e | |
pp "エラー発生!" | |
pp e.message | |
exit 1 | |
end | |
end | |
def get_certificate_arn(iam,certificate_name) | |
#証明書のARN取得 | |
begin | |
resp = iam.get_server_certificate({ | |
server_certificate_name: certificate_name | |
}) | |
return resp.server_certificate.server_certificate_metadata.arn | |
rescue Aws::IAM::Errors::NoSuchEntity => e | |
pp "証明書が存在しない" | |
return false | |
rescue => e | |
pp "エラー発生!" | |
pp e.message | |
return false | |
end | |
end | |
def elb_exists?(elb,elb_name) | |
# ELB存在チェック | |
begin | |
resp = elb.describe_load_balancers({ | |
load_balancer_names: [elb_name], | |
}) | |
pp "ELBが存在する" | |
return true | |
rescue Aws::ElasticLoadBalancing::Errors::LoadBalancerNotFound => e | |
pp "ELBが存在しない" | |
return false | |
rescue Aws::ElasticLoadBalancing::Errors::ValidationError => e | |
pp "ロードバランサーの名前に使用できるのは、英数字とダッシュのみです。" | |
pp e.message | |
return false | |
rescue => e | |
pp "エラー発生!" | |
pp e | |
exit 1 | |
end | |
end | |
d = Date.today | |
if certificate_name == "" || certificate_name.nil? | |
certificate_name = "#{elb_name}_" + d.strftime("%Y%m%d") | |
end | |
if get_certificate_arn(iam,certificate_name) | |
certificate_arn = get_certificate_arn(iam,certificate_name) | |
else | |
#証明書がアップロードされていない。 | |
# 証明書ファイルを読み込む。中身の妥当性まではチェックしない。 | |
ca = File.read(ca_file) | |
crt = File.read(crt_file) | |
key = File.read(key_file) | |
begin | |
resp = iam.upload_server_certificate({ | |
server_certificate_name: certificate_name, | |
certificate_body: crt, | |
private_key: key, | |
certificate_chain: ca, | |
}) | |
certificate_arn = resp.server_certificate_metadata.arn | |
pp "証明書アップロード完了。" | |
rescue => e | |
pp "エラー発生!" | |
pp e.message | |
exit 1 | |
end | |
end if | |
vpc_id = get_vpc_id(ec2,vpc_name) | |
elb_secutiry_groups = get_security_group_ids(ec2,vpc_id,elb_security_group_names) | |
subnet_ids = get_subbet_ids(ec2,vpc_id,subnet_names) | |
# ELB作成 | |
if !elb_exists?(elb,elb_name) | |
begin | |
resp = elb.create_load_balancer({ | |
load_balancer_name: elb_name, # required | |
listeners: [ # required | |
{ | |
protocol: "HTTP", # required | |
load_balancer_port: 80, # required | |
instance_protocol: "HTTP", | |
instance_port: 80, # required | |
}, | |
{ | |
protocol: "HTTPS", # required | |
load_balancer_port: 443, # required | |
instance_protocol: "HTTP", | |
instance_port: 80, # required | |
ssl_certificate_id: certificate_arn, | |
}, | |
], | |
subnets: subnet_ids, | |
security_groups: elb_secutiry_groups, | |
tags: [ | |
{ | |
key: "Name", # required | |
value: elb_comment, | |
}, | |
], | |
}) | |
pp "ELB作成完了" | |
pp resp.dns_name | |
rescue => e | |
pp "エラー発生!" | |
pp e.message | |
exit 1 | |
end | |
end | |
## 参考:インスタンスをセットする。 | |
instances = ["i-zzzzzzzz","i-xxxxxxxx"] | |
begin | |
tmp = [] | |
instances.each do |i| | |
tmp.push({instance_id: i}) | |
end | |
resp = elb.register_instances_with_load_balancer({ | |
load_balancer_name: elb_name, | |
instances: tmp | |
}) | |
pp "インスタンスセット" | |
rescue => e | |
pp "エラー発生!" | |
pp e | |
exit 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment