TransWikia.com

How to get private IP address of EC2 after spinning the AWS from local machine/ at Jenkins outside AWS

Unix & Linux Asked by user176867 on December 27, 2021

Assume we already spin the AWS using cloudformation plugin from Jenkins outside AWS and now, how to get private IP address after spinning the AWS at my local machine/jenkins using any API methods? I tried ruby aws-sdk, REST API methods to get the private ip outside AWS (at my local) and I’m getting timed out for the connection. Here are some examples that did not yield the ip address/EC2 objects and getting timeout –

using ruby aws-sdk

require 'rubygems'
require 'aws-sdk-v1'
require 'aws-sdk'
AWS.config(:region => "xxxxx",
           :access_key_id => "xxxxx",
           :secret_access_key => "xxxxx")
ec2 =  AWS::EC2.new(
    :region => "xxxxxx",
    :access_key_id => "xxxx",
    :secret_access_key => "xxxxxxx")
ec2.instances.each do |test|
  puts test.id
end

using REST API client –

require 'rubygems'
require 'rest-client'
url = "https://ec2.amazonaws.com/?Action=DescribeNatGateways&ImageId=xxxxxxx&access_key_id=xxxxxx&secret_access_key=xxxxxxxx"
response=RestClient::Request.execute(:url =>url, :method => :get, :verify_ssl => false)
puts response

Tried uploading the IP address contained text file to S3 and then reading it back –

  --------------cloudformation Json contains the following ---------
"wget -qO- http://169.254.169.254/latest/meta-data/local-pv4>>dockeriseleniumgrid_ip_addressn", "aws s3 cp dockeriseleniumgrid_ip_address s3://xxxxx/dockeriseleniumgrid_ip_addressn"

----------tried reading it from s3 and writing to local machine --------

    require 'aws/s3'
    S3ID = "xxxxx"
    S3KEY = "xxxx"
    # include AWS::S3
    AWS::S3::Base.establish_connection!(
        :access_key_id     => S3ID,
        :secret_access_key => S3KEY
    )
    bucket = AWS::S3::Bucket.find("dockeriseleniumgrid_ip_address")

    File.open("ip_address.txt", "w") do |f|
      f.write(bucket.objects[1].read)
    end

I’m new to AWS and appreciate if anyone can help

One Answer

here is a working code which might help others in future - Use the below output in the json file as part of jenkins cloudformation

"Outputs": {
    "GridtoGetIP": {
      "Description": "IP Address",
      "Value": {
        "Fn::GetAtt": ["GridtoGetIP", "PrivateIp"]
      }
    }
  }

In the Jenkins you will have to provide stack name, for example - name it "GetPrivateIP" then you retrieve the IP using ruby as

ENV['GetPrivateIP_GridtoGetIP']

Similarly you can read this environment variable in other programming languages!!!

Answered by user176867 on December 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP