2022-10-11 14:21:14 -06:00
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
# Purpose:
# ec2-ruby-example-create-instance.rb demonstrates how to create and tag an
# Amazon Elastic Compute Cloud (Amazon EC2) instance using AWS SDK for Ruby.
# snippet-start:[ec2.Ruby.createInstances]
2024-09-25 15:25:54 -04:00
require 'aws-sdk-ec2'
require 'base64'
2022-10-11 14:21:14 -06:00
# 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'
# )
def instance_created? (
ec2_resource ,
image_id ,
key_pair_name ,
tag_key ,
tag_value ,
2024-09-25 15:25:54 -04:00
instance_type = 't2.micro' ,
user_data_file = ''
2022-10-11 14:21:14 -06:00
)
2024-09-25 15:25:54 -04:00
encoded_script = ''
2022-10-11 14:21:14 -06:00
2024-09-25 15:25:54 -04:00
unless user_data_file == ''
2022-10-11 14:21:14 -06:00
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
)
2024-09-25 15:25:54 -04:00
puts 'Creating instance...'
2022-10-11 14:21:14 -06:00
# Check whether the new instance is in the "running" state.
polls = 0
loop do
polls += 1
response = ec2_resource . client . describe_instances (
instance_ids : [
instance . first . id
]
)
# Stop polling after 10 minutes (40 polls * 15 seconds per poll) if not running.
2024-09-25 15:25:54 -04:00
break if response . reservations [ 0 ] . instances [ 0 ] . state . name == 'running' || polls > 40
2022-10-11 14:21:14 -06:00
sleep ( 15 )
end
puts " Instance created with ID ' #{ instance . first . id } '. "
instance . batch_create_tags (
tags : [
{
key : tag_key ,
value : tag_value
}
]
)
2024-09-25 15:25:54 -04:00
puts 'Instance tagged.'
2022-10-11 14:21:14 -06:00
2024-09-25 15:25:54 -04:00
true
2022-10-11 14:21:14 -06:00
rescue StandardError = > e
puts " Error creating or tagging instance: #{ e . message } "
2024-09-25 15:25:54 -04:00
false
2022-10-11 14:21:14 -06:00
end
2024-02-16 11:17:29 -05:00
# Example usage:
2022-10-11 14:21:14 -06:00
def run_me
2024-09-25 15:25:54 -04:00
image_id = ''
key_pair_name = ''
tag_key = ''
tag_value = ''
instance_type = ''
region = ''
user_data_file = ''
2022-10-11 14:21:14 -06:00
# Print usage information and then stop.
2024-09-25 15:25:54 -04:00
if ARGV [ 0 ] == '--help' || ARGV [ 0 ] == '-h'
puts 'Usage: ruby ec2-ruby-example-create-instance.rb ' \
'IMAGE_ID KEY_PAIR_NAME TAG_KEY TAG_VALUE INSTANCE_TYPE ' \
'REGION [USER_DATA_FILE]'
# Replace us-west-2 with the AWS Region you're using for Amazon EC2.
puts 'Example: ruby ec2-ruby-example-create-instance.rb ' \
'ami-0947d2ba12EXAMPLE my-key-pair my-key my-value t2.micro ' \
'us-west-2 my-user-data.txt'
2022-10-11 14:21:14 -06:00
exit 1
# If no values are specified at the command prompt, use these default values.
elsif ARGV . count . zero?
2024-09-25 15:25:54 -04:00
image_id = 'ami-0947d2ba12EXAMPLE'
key_pair_name = 'my-key-pair'
tag_key = 'my-key'
tag_value = 'my-value'
instance_type = 't2.micro'
2022-10-11 14:21:14 -06:00
# Replace us-west-2 with the AWS Region you're using for Amazon EC2.
2024-09-25 15:25:54 -04:00
region = 'us-west-2'
user_data_file = 'my-user-data.txt'
2022-10-11 14:21:14 -06:00
# Otherwise, use the values as specified at the command prompt.
else
image_id = ARGV [ 0 ]
key_pair_name = ARGV [ 1 ]
tag_key = ARGV [ 2 ]
tag_value = ARGV [ 3 ]
instance_type = ARGV [ 4 ]
region = ARGV [ 5 ]
user_data_file = ARGV [ 6 ] if ARGV . count == 7 # If user data file specified.
end
ec2_resource = Aws :: EC2 :: Resource . new ( region : region )
if instance_created? (
ec2_resource ,
image_id ,
key_pair_name ,
tag_key ,
tag_value ,
instance_type ,
user_data_file
)
2024-09-25 15:25:54 -04:00
puts 'Created and tagged instance.'
2022-10-11 14:21:14 -06:00
else
2024-09-25 15:25:54 -04:00
puts 'Could not create or tag instance.'
2022-10-11 14:21:14 -06:00
end
end
run_me if $PROGRAM_NAME == __FILE__
# snippet-end:[ec2.Ruby.createInstances]