1
1

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-SDK Ruby で EC2 のインスタンス名からインスタンス id / ip address を取得する #aws #ec2 #ruby

Posted at

AWS-SDK Ruby で EC2 のインスタンス名からインスタンス id / ip address を取得する #aws #ec2 #ruby

概要

AWS-SDK Ruby で EC2 のインスタンス名からインスタンス id / ip address を取得します

目的

Vagrant AWS provider で EC2 のインスタンス名を tag name で指定して、 vagrant up で EC2 インスタンスを生成した後に、
後続の自動デプロイ処理に必要となる IPアドレス・インスタンス id を tag name を元に取得したい。

Vagrantfile 例

# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

$shell =<<-EOS
# some provisioning logic
EOS

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "dummy"

  config.vm.provider :aws do |aws, override|
    Dotenv.load
    aws.access_key_id             = ENV['ACCESS_KEY_ID']
    aws.secret_access_key         = ENV['SECRET_ACCESS_KEY']
    aws.keypair_name              = ENV['KEY_PAIR_NAME']

    aws.region                    = "ap-northeast-1"
    aws.ami                       = 'ami-858eb884'
    aws.instance_type             = 't1.micro'
    aws.security_groups           = 'sample'
    timestamp = Time.now.strftime('%Y%m%d_%H%M%S')
    aws.tags = {
      'Name' => "vagrant_aws_test_#{timestamp}",
    }

    override.ssh.username         = "core"
    override.ssh.private_key_path = "/path/to/your/key"

    config.vm.provision :shell, :inline => $shell
  end
end

手順

Gemfile

認証のための環境変数は dotenv GitHub を利用します

source "https://rubygems.org"

gem "dotenv"
gem "aws-sdk"

AWS API 用認証情報の取得

  • AWS Management Console にサインイン

  • IAM コンソールに移動
    aws1.png

  • 画面サブの [Navigation] ペインで [Users] をクリック
    aws2.png

  • 対象ユーザーを選択

  • 画面下部の Security Credentials の Manage Access Keys を選択

  • ダイアログの下部にある [Create Access Key] をクリック

  • ページの下部にある [Download Credentials] をクリック

  • アクセスキー ID とシークレットアクセスキーを CSV ファイルとしてコンピュータに保存

    • このファイルは安全な場所に保存
    • シークレットアクセスキーの再取得は不可能

.env

AWS Management Console から取得した情報を .env に設定する

ACCESS_KEY_ID=your_access_key
SECRET_ACCESS_KEY=your_secret_key
EC2_ENDPOINT=your_endpoint

read_ec2_instance_id_and_key.rb

require 'aws-sdk'
require 'dotenv'

Dotenv.load
ec2 = AWS::EC2.new(
  access_key_id:     ENV["ACCESS_KEY_ID"],
  secret_access_key: ENV["SECRET_ACCESS_KEY"],
  ec2_endpoint:      ENV["EC2_ENDPOINT"]
)

print ec2.instances.select { |e|e.tags['Name'] =~ /vagrant/ }
                   .map { |e| { id: e.id, ip_address: e.ip_address, name: e.tags['Name'] } }

実行

$ bundle exec ruby read_ec2_instance_id_and_key.rb
[{:id=>"i-some_id", :ip_address=>"your_ip_address", :name=>"vagrant_aws_test_20141216_224111"}]

参照

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?