5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS セキュリティグループを編集するスクリプト

Last updated at Posted at 2014-07-10

やりたいこと

AWSの複数リージョン構成でAutoScaleするEC2のZabbix自動登録をしたい。
自動登録時はZabbix-Agent→Zabbix-Serverに通信するので、Zabbix-Server側のセキュリティグループにZabbix-AgentのIPアドレスを許可する設定を記述する必要がある。

できていること/できていないこと

◆できていること
 ・IPアドレスの取得
 ・セキュリティグループへの定義追加

◆出来ていない事
 ・既に設定されている場合の除外
  →今後検討予定です。

スクリプト

例としてport 3306で実験しています。あとでzabbixのポートに修正予定です。

test.ruby
# !/bin/env ruby

## ライブラリのロード
require 'rubygems'
require 'aws-sdk'
require 'yaml'

config = YAML.load(File.read("test.yaml"))

AWS.config(config)

##################################################################
##       EC2                                                     #
##################################################################
## EC2をNEWする
ec2 = AWS::EC2.new

## SecurityGroupの取得
sg = ec2.security_groups.filter('group-name', 'ここにセキュリティグループ名を記述').first

# EC2のインスタンス毎に処理を実行
ec2.instances.each do |instance|
        #puts "#{instance.ip_address}\t#{instance.public_dns_name}"

        #セキュリティグループへの定義追加
        #起動していないインスタンスはIPアドレスがnilになるようなので判定
        if instance.ip_address != nil then
                #                         port ipaddress
                #                           ↓      ↓
                sg.authorize_ingress(:tcp,3306,"#{instance.ip_address}/32")
        end

end

test.yaml
access_key_id: アクセスKey
secret_access_key: シークレットアクセスKey
auto_scaling_endpoint: autoscaling.ap-northeast-1.amazonaws.com
cloud_front_endpoint: cloudfront.amazonaws.com
cloud_watch_endpoint: monitoring.ap-northeast-1.amazonaws.com
emr_endpoint: elasticmapreduce.ap-northeast-1.amazonaws.com
elasticache_endpoint: ec2.ap-northeast-1.amazonaws.com
elastic_beanstalk_endpoint: elasticbeanstalk.ap-northeast-1.amazonaws.com
elastic_load_balancing_endpoint: elasticloadbalancing.ap-northeast-1.amazonaws.com
elastic_transcoder_endpoint: elastictranscoder.ap-northeast-1.amazonaws.com
ec2_endpoint: ec2.ap-northeast-1.amazonaws.com
iam_endpoint: iam.amazonaws.com
s3_endpoint: s3.ap-northeast-1.amazonaws.com
rds_endpoint: rds.ap-northeast-1.amazonaws.com

他に良い方法をご存知の方がいらっしゃったら、お教えください・・・・

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?