Reputation: 70
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.
Upvotes: 2
Views: 1077
Reputation: 36174
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}]
Upvotes: 1
Reputation: 23492
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.
Upvotes: 3