简体   繁体   中英

aws-sdk for ruby. How to assign a public ip address with instance?

After thoroughly reading the ruby aws-sdk docs , I dont see a way to associate a public ip address with an instance when creating inside a VPC. I dont want to manage elastic IP addresses, just have the good old random public ip.

instance = ec2.instances.create(
  count: 1,
  image_id: IMAGE_ID,
  instance_type: INSTANCE_TYPE,
  key_name: KEYNAME,
  subnet_id: SUBNET_ID,
  security_groups: security_group,
  user_data: USER_DATA
)

Anyone know the simplest way to associate a public address on ec2 instance creation?

thanks in advance.

From http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/EC2/Client/V20131015.html#run_instances-instance_method , Under run_instances method, Under :network_interfaces option, you can use :associate_public_ip_address input parameter to specify assignment of the Public IP address.

FYI, You are using older version of Ruby SDK. Use AWS SDK CORE as AWS is focusing more on Core SDK now a days. Above option for assigning Public ip address in AWS SDK CORE can be found here .

Here is some sample code to launch an instance in a VPC with a public IP address, using version 2.2 of the Ruby AWS SDK. As far as I can tell, this is the minimum amount of code needed in order to launch an instance that actually has a public IP address. All the variables named my-something would need to be set first, of course.

require 'aws-sdk'
@ec2 = Aws::EC2::Resource.new
@ec2.create_instances(dry_run: false,
                      min_count: 1,
                      max_count: 1,
                      image_id: my-image-id,
                      key_name: my-key-name,
                      network_interfaces: [{device_index: 0,
                                            subnet_id: my-subnet-id,
                                            groups: [my-security-group-id],
                                            delete_on_termination: true,
                                            associate_public_ip_address: true}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM