ruby/example_code/ec2/ec2-ruby-example-create-instance.rb
# Prerequisites:
#
# - An EC2 key pair.
# - If you want to run any commands on the instance after it starts, a
#   file containing those commands.
#
# @param ec2_resource [Aws::EC2::Resource] An initialized EC2 resource object.
# @param image_id [String] The ID of the target Amazon Machine Image (AMI).
# @param key_pair_name [String] The name of the existing EC2 key pair.
# @param tag_key [String] The key portion of the tag for the instance.
# @param tag_value [String] The value portion of the tag for the instance.
# @param instance_type [String] The ID of the type of instance to create.
#   If not specified, the default value is 't2.micro'.
# @param user_data_file [String] The path to the file containing any commands
#   to run on the instance after it starts. If not specified, the default
#   value is an empty string.
# @return [Boolean] true if the instance was created and tagged;
#   otherwise, false.
# @example
#   exit 1 unless instance_created?(
#     Aws::EC2::Resource.new(region: 'us-west-2'),
#     'ami-0947d2ba12EXAMPLE',
#     'my-key-pair',
#     'my-key',
#     'my-value',
#     't2.micro',
#     'my-user-data.txt'
#   )

An EC2 key pair.
If you want to run any commands on the instance after it starts, a
file containing those commands.
An EC2 key pair . If you want to run any commands on the instance after it starts a file containing those commands .
def instance_created?(
  ec2_resource,
  image_id,
  key_pair_name,
  tag_key,
  tag_value,
  instance_type = 't2.micro',
  user_data_file = ''
)
  encoded_script = ''
  unless user_data_file == ''
    script = File.read(user_data_file)
    encoded_script = Base64.encode64(script)
  end
  instance = ec2_resource.create_instances(
    image_id: image_id,
    min_count: 1,
    max_count: 1,
    key_name: key_pair_name,
    instance_type: instance_type,
    user_data: encoded_script
  )
  puts 'Creating instance...'
  polls = 0
  loop do
    polls += 1
    response = ec2_resource.client.describe_instances(
      instance_ids: [
        instance.first.id
      ]
    )
    break if response.reservations[0].instances[0].state.name == 'running' || polls > 40
    sleep(15)
  end
  puts "Instance created with ID '#{instance.first.id}'."
  instance.batch_create_tags(
    tags: [
      {
        key: tag_key,
        value: tag_value
      }
    ]
  )
  puts 'Instance tagged.'
  return true
rescue StandardError => e
  puts "Error creating or tagging instance: #{e.message}"
  return false
end
def instance_created? ( ec2_resource , image_id , key_pair_name , tag_key , tag_value , instance_type = 't2.micro' , user_data_file = '' ) encoded_script = '' unless user_data_file == '' script = File . read ( user_data_file ) encoded_script = Base64 . encode64 ( script ) end instance = ec2_resource . create_instances ( image_id : image_id , min_count : 1 , max_count : 1 , key_name : key_pair_name , instance_type : instance_type , user_data : encoded_script ) puts 'Creating instance...' polls = 0 loop do polls += 1 response = ec2_resource . client . describe_instances ( instance_ids : [ instance . first . id ] ) break if response . reservations [ 0 ] . instances [ 0 ] . state . name == 'running' || polls > 40 sleep ( 15 ) end puts "Instance created with ID '#{instance.first.id}'." instance . batch_create_tags ( tags : [ { key : tag_key , value : tag_value } ] ) puts 'Instance tagged.' return true rescue StandardError => e puts "Error creating or tagging instance: #{e.message}" return false end
Prerequisites:
 An EC2 key pair.
Prerequisites : An EC2 key pair .
# Check whether the new instance is in the "running" state.
 # Stop polling after 10 minutes (40 polls * 15 seconds per poll) if not running.

true if the instance was created and tagged; otherwise, false.
true if the instance was created and tagged ; otherwise false .
Boolean
ec2_resource
An initialized EC2 resource object.
An initialized EC2 resource object .
image_id
The ID of the target Amazon Machine Image (AMI).
The ID of the target Amazon Machine Image ( AMI ) .
key_pair_name
The name of the existing EC2 key pair.
The name of the existing EC2 key pair .
tag_key
The key portion of the tag for the instance.
The key portion of the tag for the instance .
tag_value
The value portion of the tag for the instance.
The value portion of the tag for the instance .
instance_type
The ID of the type of instance to create. If not specified, the default value is 't2.micro'.
The ID of the type of instance to create . If not specified the default value is ' t2 . micro ' .
user_data_file
The path to the file containing any commands to run on the instance after it starts. If not specified, the default value is an empty string.
The path to the file containing any commands to run on the instance after it starts . If not specified the default value is an empty string .
example
exit 1 unless instance_created?( Aws::EC2::Resource.new(region: 'us-west-2'), 'ami-0947d2ba12EXAMPLE', 'my-key-pair', 'my-key', 'my-value', 't2.micro', 'my-user-data.txt' )
exit 1 unless instance_created? ( Aws :: EC2 :: Resource . new ( region : ' us - west - 2 ' ) ' ami - 0947d2ba12EXAMPLE ' ' my - key - pair ' ' my - key ' ' my - value ' ' t2 . micro ' ' my - user - data . txt ' )