Amazon EC2 examples using SDK for Ruby - Amazon SDK for Ruby
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Amazon EC2 examples using SDK for Ruby

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for Ruby with Amazon EC2.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Get started

The following code examples show how to get started using Amazon EC2.

SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

require 'aws-sdk-ec2' require 'logger' # EC2Manager is a class responsible for managing EC2 operations # such as listing all EC2 instances in the current AWS account. class EC2Manager def initialize(client) @client = client @logger = Logger.new($stdout) end # Lists and prints all EC2 instances in the current AWS account. def list_instances @logger.info('Listing instances') instances = fetch_instances if instances.empty? @logger.info('You have no instances') else print_instances(instances) end end private # Fetches all EC2 instances using pagination. # # @return [Array<Aws::EC2::Types::Instance>] List of EC2 instances. def fetch_instances paginator = @client.describe_instances instances = [] paginator.each_page do |page| page.reservations.each do |reservation| reservation.instances.each do |instance| instances << instance end end end instances end # Prints details of the given EC2 instances. # # @param instances [Array<Aws::EC2::Types::Instance>] List of EC2 instances to print. def print_instances(instances) instances.each do |instance| @logger.info("Instance ID: #{instance.instance_id}") @logger.info("Instance Type: #{instance.instance_type}") @logger.info("Public IP: #{instance.public_ip_address}") @logger.info("Public DNS Name: #{instance.public_dns_name}") @logger.info("\n") end end end if $PROGRAM_NAME == __FILE__ ec2_client = Aws::EC2::Client.new(region: 'us-west-2') manager = EC2Manager.new(ec2_client) manager.list_instances end
Topics

Actions

The following code example shows how to use AllocateAddress.

SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

# Creates an Elastic IP address in Amazon Virtual Private Cloud (Amazon VPC). # # @param ec2_client [Aws::EC2::Client] An initialized EC2 client. # @return [String] The allocation ID corresponding to the Elastic IP address. # @example # puts allocate_elastic_ip_address(Aws::EC2::Client.new(region: 'us-west-2')) def allocate_elastic_ip_address(ec2_client) response = ec2_client.allocate_address(domain: "vpc") return response.allocation_id rescue StandardError => e puts "Error allocating Elastic IP address: #{e.message}" return "Error" end
  • For API details, see AllocateAddress in Amazon SDK for Ruby API Reference.

The following code example shows how to use AssociateAddress.

SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

# Associates an Elastic IP address with an Amazon Elastic Compute Cloud # (Amazon EC2) instance. # # Prerequisites: # # - The allocation ID corresponding to the Elastic IP address. # - The Amazon EC2 instance. # # @param ec2_client [Aws::EC2::Client] An initialized EC2 client. # @param allocation_id [String] The ID of the allocation corresponding to # the Elastic IP address. # @param instance_id [String] The ID of the instance. # @return [String] The assocation ID corresponding to the association of the # Elastic IP address to the instance. # @example # puts allocate_elastic_ip_address( # Aws::EC2::Client.new(region: 'us-west-2'), # 'eipalloc-04452e528a66279EX', # 'i-033c48ef067af3dEX') def associate_elastic_ip_address_with_instance( ec2_client, allocation_id, instance_id ) response = ec2_client.associate_address( allocation_id: allocation_id, instance_id: instance_id, ) return response.association_id rescue StandardError => e puts "Error associating Elastic IP address with instance: #{e.message}" return "Error" end

The following code example shows how to use CreateKeyPair.

SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

# This code example does the following: # 1. Creates a key pair in Amazon Elastic Compute Cloud (Amazon EC2). # 2. Displays information about available key pairs. # 3. Deletes the key pair. require "aws-sdk-ec2" # @param ec2_client [Aws::EC2::Client] An initialized EC2 client. # @param key_pair_name [String] The name for the key pair and private # key file. # @return [Boolean] true if the key pair and private key file were # created; otherwise, false. # @example # exit 1 unless key_pair_created?( # Aws::EC2::Client.new(region: 'us-west-2'), # 'my-key-pair' # ) def key_pair_created?(ec2_client, key_pair_name) key_pair = ec2_client.create_key_pair(key_name: key_pair_name) puts "Created key pair '#{key_pair.key_name}' with fingerprint " \ "'#{key_pair.key_fingerprint}' and ID '#{key_pair.key_pair_id}'." filename = File.join(Dir.home, key_pair_name + ".pem") File.open(filename, "w") { |file| file.write(key_pair.key_material) } puts "Private key file saved locally as '#{filename}'." return true rescue Aws::EC2::Errors::InvalidKeyPairDuplicate puts "Error creating key pair: a key pair named '#{key_pair_name}' " \ "already exists." return false rescue StandardError => e puts "Error creating key pair or saving private key file: #{e.message}" return false end # Displays information about available key pairs in # Amazon Elastic Compute Cloud (Amazon EC2). # # @param ec2_client [Aws::EC2::Client] An initialized EC2 client. # @example # describe_key_pairs(Aws::EC2::Client.new(region: 'us-west-2')) def describe_key_pairs(ec2_client) result = ec2_client.describe_key_pairs if result.key_pairs.count.zero? puts "No key pairs found." else puts "Key pair names:" result.key_pairs.each do |key_pair| puts key_pair.key_name end end rescue StandardError => e puts "Error getting information about key pairs: #{e.message}" end # Deletes a key pair in Amazon Elastic Compute Cloud (Amazon EC2). # # Prerequisites: # # - The key pair to delete. # # @param ec2_client [Aws::EC2::Client] An initialized EC2 client. # @param key_pair_name [String] The name of the key pair to delete. # @return [Boolean] true if the key pair was deleted; otherwise, false. # @example # exit 1 unless key_pair_deleted?( # Aws::EC2::Client.new(region: 'us-west-2'), # 'my-key-pair' # ) def key_pair_deleted?(ec2_client, key_pair_name) ec2_client.delete_key_pair(key_name: key_pair_name) return true rescue StandardError => e puts "Error deleting key pair: #{e.message}" return false end # Example usage: def run_me key_pair_name = "" region = "" # Print usage information and then stop. if ARGV[0] == "--help" || ARGV[0] == "-h" puts "Usage: ruby ec2-ruby-example-key-pairs.rb KEY_PAIR_NAME REGION" puts "Example: ruby ec2-ruby-example-key-pairs.rb my-key-pair us-west-2" exit 1 # If no values are specified at the command prompt, use these default values. # Replace us-west-2 with the AWS Region you're using for Amazon EC2. elsif ARGV.count.zero? key_pair_name = "my-key-pair" region = "us-west-2" # Otherwise, use the values as specified at the command prompt. else key_pair_name = ARGV[0] region = ARGV[1] end ec2_client = Aws::EC2::Client.new(region: region) puts "Displaying existing key pair names before creating this key pair..." describe_key_pairs(ec2_client) puts "-" * 10 puts "Creating key pair..." unless key_pair_created?(ec2_client, key_pair_name) puts "Stopping program." exit 1 end puts "-" * 10 puts "Displaying existing key pair names after creating this key pair..." describe_key_pairs(ec2_client) puts "-" * 10 puts "Deleting key pair..." unless key_pair_deleted?(ec2_client, key_pair_name) puts "Stopping program. You must delete the key pair yourself." exit 1 end puts "Key pair deleted." puts "-" * 10 puts "Now that the key pair is deleted, " \ "also deleting the related private key pair file..." filename = File.join(Dir.home, key_pair_name + ".pem") File.delete(filename) if File.exist?(filename) puts "Could not delete file at '#{filename}'. You must delete it yourself." else puts "File deleted." end puts "-" * 10 puts "Displaying existing key pair names after deleting this key pair..." describe_key_pairs(ec2_client) end run_me if $PROGRAM_NAME == __FILE__
  • For API details, see CreateKeyPair in Amazon SDK for Ruby API Reference.

The following code example shows how to use CreateRouteTable.

SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

require "aws-sdk-ec2" # Prerequisites: # # - A VPC in Amazon VPC. # - A subnet in that VPC. # - A gateway attached to that subnet. # # @param ec2_resource [Aws::EC2::Resource] An initialized # Amazon Elastic Compute Cloud (Amazon EC2) resource object. # @param vpc_id [String] The ID of the VPC for the route table. # @param subnet_id [String] The ID of the subnet for the route table. # @param gateway_id [String] The ID of the gateway for the route. # @param destination_cidr_block [String] The destination CIDR block # for the route. # @param tag_key [String] The key portion of the tag for the route table. # @param tag_value [String] The value portion of the tag for the route table. # @return [Boolean] true if the route table was created and associated; # otherwise, false. # @example # exit 1 unless route_table_created_and_associated?( # Aws::EC2::Resource.new(region: 'us-west-2'), # 'vpc-0b6f769731EXAMPLE', # 'subnet-03d9303b57EXAMPLE', # 'igw-06ca90c011EXAMPLE', # '0.0.0.0/0', # 'my-key', # 'my-value' # ) def route_table_created_and_associated?( ec2_resource, vpc_id, subnet_id, gateway_id, destination_cidr_block, tag_key, tag_value ) route_table = ec2_resource.create_route_table(vpc_id: vpc_id) puts "Created route table with ID '#{route_table.id}'." route_table.create_tags( tags: [ { key: tag_key, value: tag_value } ] ) puts "Added tags to route table." route_table.create_route( destination_cidr_block: destination_cidr_block, gateway_id: gateway_id ) puts "Created route with destination CIDR block " \ "'#{destination_cidr_block}' and associated with gateway " \ "with ID '#{gateway_id}'." route_table.associate_with_subnet(subnet_id: subnet_id) puts "Associated route table with subnet with ID '#{subnet_id}'." return true rescue StandardError => e puts "Error creating or associating route table: #{e.message}" puts "If the route table was created but not associated, you should " \ "clean up by deleting the route table." return false end # Example usage: def run_me vpc_id = "" subnet_id = "" gateway_id = "" destination_cidr_block = "" tag_key = "" tag_value = "" region = "" # Print usage information and then stop. if ARGV[0] == "--help" || ARGV[0] == "-h" puts "Usage: ruby ec2-ruby-example-create-route-table.rb " \ "VPC_ID SUBNET_ID GATEWAY_ID DESTINATION_CIDR_BLOCK " \ "TAG_KEY TAG_VALUE REGION" # Replace us-west-2 with the AWS Region you're using for Amazon EC2. puts "Example: ruby ec2-ruby-example-create-route-table.rb " \ "vpc-0b6f769731EXAMPLE subnet-03d9303b57EXAMPLE igw-06ca90c011EXAMPLE " \ "'0.0.0.0/0' my-key my-value us-west-2" exit 1 # If no values are specified at the command prompt, use these default values. elsif ARGV.count.zero? vpc_id = "vpc-0b6f769731EXAMPLE" subnet_id = "subnet-03d9303b57EXAMPLE" gateway_id = "igw-06ca90c011EXAMPLE" destination_cidr_block = "0.0.0.0/0" tag_key = "my-key" tag_value = "my-value" # Replace us-west-2 with the AWS Region you're using for Amazon EC2. region = "us-west-2" # Otherwise, use the values as specified at the command prompt. else vpc_id = ARGV[0] subnet_id = ARGV[1] gateway_id = ARGV[2] destination_cidr_block = ARGV[3] tag_key = ARGV[4] tag_value = ARGV[5] region = ARGV[6] end ec2_resource = Aws::EC2::Resource.new(region: region) if route_table_created_and_associated?( ec2_resource, vpc_id, subnet_id, gateway_id, destination_cidr_block, tag_key, tag_value ) puts "Route table created and associated." else puts "Route table not created or not associated." end end run_me if $PROGRAM_NAME == __FILE__

The following code example shows how to use CreateSecurityGroup.

SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

# This code example does the following: # 1. Creates an Amazon Elastic Compute Cloud (Amazon EC2) security group. # 2. Adds inbound rules to the security group. # 3. Displays information about available security groups. # 4. Deletes the security group. require "aws-sdk-ec2" # Creates an Amazon Elastic Compute Cloud (Amazon EC2) security group. # # Prerequisites: # # - A VPC in Amazon Virtual Private Cloud (Amazon VPC). # # @param ec2_client [Aws::EC2::Client] An initialized # Amazon EC2 client. # @param group_name [String] A name for the security group. # @param description [String] A description for the security group. # @param vpc_id [String] The ID of the VPC for the security group. # @return [String] The ID of security group that was created. # @example # puts create_security_group( # Aws::EC2::Client.new(region: 'us-west-2'), # 'my-security-group', # 'This is my security group.', # 'vpc-6713dfEX' # ) def create_security_group( ec2_client, group_name, description, vpc_id ) security_group = ec2_client.create_security_group( group_name: group_name, description: description, vpc_id: vpc_id ) puts "Created security group '#{group_name}' with ID " \ "'#{security_group.group_id}' in VPC with ID '#{vpc_id}'." return security_group.group_id rescue StandardError => e puts "Error creating security group: #{e.message}" return "Error" end # Adds an inbound rule to an Amazon Elastic Compute Cloud (Amazon EC2) # security group. # # Prerequisites: # # - The security group. # # @param ec2_client [Aws::EC2::Client] An initialized Amazon EC2 client. # @param security_group_id [String] The ID of the security group. # @param ip_protocol [String] The network protocol for the inbound rule. # @param from_port [String] The originating port for the inbound rule. # @param to_port [String] The destination port for the inbound rule. # @param cidr_ip_range [String] The CIDR IP range for the inbound rule. # @return # @example # exit 1 unless security_group_ingress_authorized?( # Aws::EC2::Client.new(region: 'us-west-2'), # 'sg-030a858e078f1b9EX', # 'tcp', # '80', # '80', # '0.0.0.0/0' # ) def security_group_ingress_authorized?( ec2_client, security_group_id, ip_protocol, from_port, to_port, cidr_ip_range ) ec2_client.authorize_security_group_ingress( group_id: security_group_id, ip_permissions: [ { ip_protocol: ip_protocol, from_port: from_port, to_port: to_port, ip_ranges: [ { cidr_ip: cidr_ip_range } ] } ] ) puts "Added inbound rule to security group '#{security_group_id}' for protocol " \ "'#{ip_protocol}' from port '#{from_port}' to port '#{to_port}' " \ "with CIDR IP range '#{cidr_ip_range}'." return true rescue StandardError => e puts "Error adding inbound rule to security group: #{e.message}" return false end # Displays information about a security group's IP permissions set in # Amazon Elastic Compute Cloud (Amazon EC2). # # Prerequisites: # # - A security group with inbound rules, outbound rules, or both. # # @param p [Aws::EC2::Types::IpPermission] The IP permissions set. # @example # ec2_client = Aws::EC2::Client.new(region: 'us-west-2') # response = ec2_client.describe_security_groups # unless sg.ip_permissions.empty? # describe_security_group_permissions( # response.security_groups[0].ip_permissions[0] # ) # end def describe_security_group_permissions(perm) print " Protocol: #{perm.ip_protocol == '-1' ? 'All' : perm.ip_protocol}" unless perm.from_port.nil? if perm.from_port == "-1" || perm.from_port == -1 print ", From: All" else print ", From: #{perm.from_port}" end end unless perm.to_port.nil? if perm.to_port == "-1" || perm.to_port == -1 print ", To: All" else print ", To: #{perm.to_port}" end end if perm.key?(:ipv_6_ranges) && perm.ipv_6_ranges.count.positive? print ", CIDR IPv6: #{perm.ipv_6_ranges[0].cidr_ipv_6}" end if perm.key?(:ip_ranges) && perm.ip_ranges.count.positive? print ", CIDR IPv4: #{perm.ip_ranges[0].cidr_ip}" end print "\n" end # Displays information about available security groups in # Amazon Elastic Compute Cloud (Amazon EC2). # # @param ec2_client [Aws::EC2::Client] An initialized Amazon EC2 client. # @example # describe_security_groups(Aws::EC2::Client.new(region: 'us-west-2')) def describe_security_groups(ec2_client) response = ec2_client.describe_security_groups if response.security_groups.count.positive? response.security_groups.each do |sg| puts "-" * (sg.group_name.length + 13) puts "Name: #{sg.group_name}" puts "Description: #{sg.description}" puts "Group ID: #{sg.group_id}" puts "Owner ID: #{sg.owner_id}" puts "VPC ID: #{sg.vpc_id}" if sg.tags.count.positive? puts "Tags:" sg.tags.each do |tag| puts " Key: #{tag.key}, Value: #{tag.value}" end end unless sg.ip_permissions.empty? puts "Inbound rules:" if sg.ip_permissions.count.positive? sg.ip_permissions.each do |p| describe_security_group_permissions(p) end end unless sg.ip_permissions_egress.empty? puts "Outbound rules:" if sg.ip_permissions.count.positive? sg.ip_permissions_egress.each do |p| describe_security_group_permissions(p) end end end else puts "No security groups found." end rescue StandardError => e puts "Error getting information about security groups: #{e.message}" end # Deletes an Amazon Elastic Compute Cloud (Amazon EC2) # security group. # # Prerequisites: # # - The security group. # # @param ec2_client [Aws::EC2::Client] An initialized # Amazon EC2 client. # @param security_group_id [String] The ID of the security group to delete. # @return [Boolean] true if the security group was deleted; otherwise, false. # @example # exit 1 unless security_group_deleted?( # Aws::EC2::Client.new(region: 'us-west-2'), # 'sg-030a858e078f1b9EX' # ) def security_group_deleted?(ec2_client, security_group_id) ec2_client.delete_security_group(group_id: security_group_id) puts "Deleted security group '#{security_group_id}'." return true rescue StandardError => e puts "Error deleting security group: #{e.message}" return false end # Example usage: def run_me group_name = "" description = "" vpc_id = "" ip_protocol_http = "" from_port_http = "" to_port_http = "" cidr_ip_range_http = "" ip_protocol_ssh = "" from_port_ssh = "" to_port_ssh = "" cidr_ip_range_ssh = "" region = "" # Print usage information and then stop. if ARGV[0] == "--help" || ARGV[0] == "-h" puts "Usage: ruby ec2-ruby-example-security-group.rb " \ "GROUP_NAME DESCRIPTION VPC_ID IP_PROTOCOL_1 FROM_PORT_1 TO_PORT_1 " \ "CIDR_IP_RANGE_1 IP_PROTOCOL_2 FROM_PORT_2 TO_PORT_2 " \ "CIDR_IP_RANGE_2 REGION" puts "Example: ruby ec2-ruby-example-security-group.rb " \ "my-security-group 'This is my security group.' vpc-6713dfEX " \ "tcp 80 80 '0.0.0.0/0' tcp 22 22 '0.0.0.0/0' us-west-2" exit 1 # If no values are specified at the command prompt, use these default values. elsif ARGV.count.zero? group_name = "my-security-group" description = "This is my security group." vpc_id = "vpc-6713dfEX" ip_protocol_http = "tcp" from_port_http = "80" to_port_http = "80" cidr_ip_range_http = "0.0.0.0/0" ip_protocol_ssh = "tcp" from_port_ssh = "22" to_port_ssh = "22" cidr_ip_range_ssh = "0.0.0.0/0" # Replace us-west-2 with the AWS Region you're using for Amazon EC2. region = "us-west-2" # Otherwise, use the values as specified at the command prompt. else group_name = ARGV[0] description = ARGV[1] vpc_id = ARGV[2] ip_protocol_http = ARGV[3] from_port_http = ARGV[4] to_port_http = ARGV[5] cidr_ip_range_http = ARGV[6] ip_protocol_ssh = ARGV[7] from_port_ssh = ARGV[8] to_port_ssh = ARGV[9] cidr_ip_range_ssh = ARGV[10] region = ARGV[11] end security_group_id = "" security_group_exists = false ec2_client = Aws::EC2::Client.new(region: region) puts "Attempting to create security group..." security_group_id = create_security_group( ec2_client, group_name, description, vpc_id ) if security_group_id == "Error" puts "Could not create security group. Skipping this step." else security_group_exists = true end if security_group_exists puts "Attempting to add inbound rules to security group..." unless security_group_ingress_authorized?( ec2_client, security_group_id, ip_protocol_http, from_port_http, to_port_http, cidr_ip_range_http ) puts "Could not add inbound HTTP rule to security group. " \ "Skipping this step." end unless security_group_ingress_authorized?( ec2_client, security_group_id, ip_protocol_ssh, from_port_ssh, to_port_ssh, cidr_ip_range_ssh ) puts "Could not add inbound SSH rule to security group. " \ "Skipping this step." end end puts "\nInformation about available security groups:" describe_security_groups(ec2_client) if security_group_exists puts "\nAttempting to delete security group..." unless security_group_deleted?(ec2_client, security_group_id) puts "Could not delete security group. You must delete it yourself." end end end run_me if $PROGRAM_NAME == __FILE__

The following code example shows how to use CreateSubnet.

SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

require "aws-sdk-ec2" # Creates a subnet within a virtual private cloud (VPC) in # Amazon Virtual Private Cloud (Amazon VPC) and then tags # the subnet. # # Prerequisites: # # - A VPC in Amazon VPC. # # @param ec2_resource [Aws::EC2::Resource] An initialized # Amazon Elastic Compute Cloud (Amazon EC2) resource object. # @param vpc_id [String] The ID of the VPC for the subnet. # @param cidr_block [String] The IPv4 CIDR block for the subnet. # @param availability_zone [String] The ID of the Availability Zone # for the subnet. # @param tag_key [String] The key portion of the tag for the subnet. # @param tag_vlue [String] The value portion of the tag for the subnet. # @return [Boolean] true if the subnet was created and tagged; # otherwise, false. # @example # exit 1 unless subnet_created_and_tagged?( # Aws::EC2::Resource.new(region: 'us-west-2'), # 'vpc-6713dfEX', # '10.0.0.0/24', # 'us-west-2a', # 'my-key', # 'my-value' # ) def subnet_created_and_tagged?( ec2_resource, vpc_id, cidr_block, availability_zone, tag_key, tag_value ) subnet = ec2_resource.create_subnet( vpc_id: vpc_id, cidr_block: cidr_block, availability_zone: availability_zone ) subnet.create_tags( tags: [ { key: tag_key, value: tag_value } ] ) puts "Subnet created with ID '#{subnet.id}' in VPC with ID '#{vpc_id}' " \ "and CIDR block '#{cidr_block}' in availability zone " \ "'#{availability_zone}' and tagged with key '#{tag_key}' and " \ "value '#{tag_value}'." return true rescue StandardError => e puts "Error creating or tagging subnet: #{e.message}" return false end # Example usage: def run_me vpc_id = "" cidr_block = "" availability_zone = "" tag_key = "" tag_value = "" region = "" # Print usage information and then stop. if ARGV[0] == "--help" || ARGV[0] == "-h" puts "Usage: ruby ec2-ruby-example-create-subnet.rb " \ "VPC_ID CIDR_BLOCK AVAILABILITY_ZONE TAG_KEY TAG_VALUE REGION" # Replace us-west-2 with the AWS Region you're using for Amazon EC2. puts "Example: ruby ec2-ruby-example-create-subnet.rb " \ "vpc-6713dfEX 10.0.0.0/24 us-west-2a my-key my-value us-west-2" exit 1 # If no values are specified at the command prompt, use these default values. elsif ARGV.count.zero? vpc_id = "vpc-6713dfEX" cidr_block = "10.0.0.0/24" availability_zone = "us-west-2a" tag_key = "my-key" tag_value = "my-value" # Replace us-west-2 with the AWS Region you're using for Amazon EC2. region = "us-west-2" # Otherwise, use the values as specified at the command prompt. else vpc_id = ARGV[0] cidr_block = ARGV[1] availability_zone = ARGV[2] tag_key = ARGV[3] tag_value = ARGV[4] region = ARGV[5] end ec2_resource = Aws::EC2::Resource.new(region: region) if subnet_created_and_tagged?( ec2_resource, vpc_id, cidr_block, availability_zone, tag_key, tag_value ) puts "Subnet created and tagged." else puts "Subnet not created or not tagged." end end run_me if $PROGRAM_NAME == __FILE__
  • For API details, see CreateSubnet in Amazon SDK for Ruby API Reference.

The following code example shows how to use CreateVpc.

SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

require "aws-sdk-ec2" # Creates a virtual private cloud (VPC) in # Amazon Virtual Private Cloud (Amazon VPC) and then tags # the VPC. # # @param ec2_resource [Aws::EC2::Resource] An initialized # Amazon Elastic Compute Cloud (Amazon EC2) resource object. # @param cidr_block [String] The IPv4 CIDR block for the subnet. # @param tag_key [String] The key portion of the tag for the VPC. # @param tag_value [String] The value portion of the tag for the VPC. # @return [Boolean] true if the VPC was created and tagged; # otherwise, false. # @example # exit 1 unless vpc_created_and_tagged?( # Aws::EC2::Resource.new(region: 'us-west-2'), # '10.0.0.0/24', # 'my-key', # 'my-value' # ) def vpc_created_and_tagged?( ec2_resource, cidr_block, tag_key, tag_value ) vpc = ec2_resource.create_vpc(cidr_block: cidr_block) # Create a public DNS by enabling DNS support and DNS hostnames. vpc.modify_attribute(enable_dns_support: { value: true }) vpc.modify_attribute(enable_dns_hostnames: { value: true }) vpc.create_tags(tags: [{ key: tag_key, value: tag_value }]) puts "Created VPC with ID '#{vpc.id}' and tagged with key " \ "'#{tag_key}' and value '#{tag_value}'." return true rescue StandardError => e puts "#{e.message}" return false end # Example usage: def run_me cidr_block = "" tag_key = "" tag_value = "" region = "" # Print usage information and then stop. if ARGV[0] == "--help" || ARGV[0] == "-h" puts "Usage: ruby ec2-ruby-example-create-vpc.rb " \ "CIDR_BLOCK TAG_KEY TAG_VALUE REGION" # Replace us-west-2 with the AWS Region you're using for Amazon EC2. puts "Example: ruby ec2-ruby-example-create-vpc.rb " \ "10.0.0.0/24 my-key my-value us-west-2" exit 1 # If no values are specified at the command prompt, use these default values. elsif ARGV.count.zero? cidr_block = "10.0.0.0/24" tag_key = "my-key" tag_value = "my-value" # Replace us-west-2 with the AWS Region you're using for Amazon EC2. region = "us-west-2" # Otherwise, use the values as specified at the command prompt. else cidr_block = ARGV[0] tag_key = ARGV[1] tag_value = ARGV[2] region = ARGV[3] end ec2_resource = Aws::EC2::Resource.new(region: region) if vpc_created_and_tagged?( ec2_resource, cidr_block, tag_key, tag_value ) puts "VPC created and tagged." else puts "VPC not created or not tagged." end end run_me if $PROGRAM_NAME == __FILE__
  • For API details, see CreateVpc in Amazon SDK for Ruby API Reference.

The following code example shows how to use DescribeInstances.

SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

require "aws-sdk-ec2" # @param ec2_resource [Aws::EC2::Resource] An initialized EC2 resource object. # @example # list_instance_ids_states(Aws::EC2::Resource.new(region: 'us-west-2')) def list_instance_ids_states(ec2_resource) response = ec2_resource.instances if response.count.zero? puts "No instances found." else puts "Instances -- ID, state:" response.each do |instance| puts "#{instance.id}, #{instance.state.name}" end end rescue StandardError => e puts "Error getting information about instances: #{e.message}" end # Example usage: def run_me region = "" # Print usage information and then stop. if ARGV[0] == "--help" || ARGV[0] == "-h" puts "Usage: ruby ec2-ruby-example-get-all-instance-info.rb REGION" # Replace us-west-2 with the AWS Region you're using for Amazon EC2. puts "Example: ruby ec2-ruby-example-get-all-instance-info.rb us-west-2" exit 1 # If no values are specified at the command prompt, use these default values. # Replace us-west-2 with the AWS Region you're using for Amazon EC2. elsif ARGV.count.zero? region = "us-west-2" # Otherwise, use the values as specified at the command prompt. else region = ARGV[0] end ec2_resource = Aws::EC2::Resource.new(region: region) list_instance_ids_states(ec2_resource) end run_me if $PROGRAM_NAME == __FILE__

The following code example shows how to use DescribeRegions.

SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

require "aws-sdk-ec2" # @param ec2_client [Aws::EC2::Client] An initialized EC2 client. # @example # list_regions_endpoints(Aws::EC2::Client.new(region: 'us-west-2')) def list_regions_endpoints(ec2_client) result = ec2_client.describe_regions # Enable pretty printing. max_region_string_length = 16 max_endpoint_string_length = 33 # Print header. print "Region" print " " * (max_region_string_length - "Region".length) print " Endpoint\n" print "-" * max_region_string_length print " " print "-" * max_endpoint_string_length print "\n" # Print Regions and their endpoints. result.regions.each do |region| print region.region_name print " " * (max_region_string_length - region.region_name.length) print " " print region.endpoint print "\n" end end # Displays a list of Amazon Elastic Compute Cloud (Amazon EC2) # Availability Zones available to you depending on the AWS Region # of the Amazon EC2 client. # # @param ec2_client [Aws::EC2::Client] An initialized EC2 client. # @example # list_availability_zones(Aws::EC2::Client.new(region: 'us-west-2')) def list_availability_zones(ec2_client) result = ec2_client.describe_availability_zones # Enable pretty printing. max_region_string_length = 16 max_zone_string_length = 18 max_state_string_length = 9 # Print header. print "Region" print " " * (max_region_string_length - "Region".length) print " Zone" print " " * (max_zone_string_length - "Zone".length) print " State\n" print "-" * max_region_string_length print " " print "-" * max_zone_string_length print " " print "-" * max_state_string_length print "\n" # Print Regions, Availability Zones, and their states. result.availability_zones.each do |zone| print zone.region_name print " " * (max_region_string_length - zone.region_name.length) print " " print zone.zone_name print " " * (max_zone_string_length - zone.zone_name.length) print " " print zone.state # Print any messages for this Availability Zone. if zone.messages.count.positive? print "\n" puts " Messages for this zone:" zone.messages.each do |message| print " #{message.message}\n" end end print "\n" end end # Example usage: def run_me region = "" # Print usage information and then stop. if ARGV[0] == "--help" || ARGV[0] == "-h" puts "Usage: ruby ec2-ruby-example-regions-availability-zones.rb REGION" # Replace us-west-2 with the AWS Region you're using for Amazon EC2. puts "Example: ruby ec2-ruby-example-regions-availability-zones.rb us-west-2" exit 1 # If no values are specified at the command prompt, use these default values. # Replace us-west-2 with the AWS Region you're using for Amazon EC2. elsif ARGV.count.zero? region = "us-west-2" # Otherwise, use the values as specified at the command prompt. else region = ARGV[0] end ec2_client = Aws::EC2::Client.new(region: region) puts "AWS Regions for Amazon EC2 that are available to you:" list_regions_endpoints(ec2_client) puts "\n\nAmazon EC2 Availability Zones that are available to you for AWS Region '#{region}':" list_availability_zones(ec2_client) end run_me if $PROGRAM_NAME == __FILE__
  • For API details, see DescribeRegions in Amazon SDK for Ruby API Reference.

The following code example shows how to use ReleaseAddress.

SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

# Releases an Elastic IP address from an # Amazon Elastic Compute Cloud (Amazon EC2) instance. # # Prerequisites: # # - An Amazon EC2 instance with an associated Elastic IP address. # # @param ec2_client [Aws::EC2::Client] An initialized EC2 client. # @param allocation_id [String] The ID of the allocation corresponding to # the Elastic IP address. # @return [Boolean] true if the Elastic IP address was released; # otherwise, false. # @example # exit 1 unless elastic_ip_address_released?( # Aws::EC2::Client.new(region: 'us-west-2'), # 'eipalloc-04452e528a66279EX' # ) def elastic_ip_address_released?(ec2_client, allocation_id) ec2_client.release_address(allocation_id: allocation_id) return true rescue StandardError => e puts("Error releasing Elastic IP address: #{e.message}") return false end
  • For API details, see ReleaseAddress in Amazon SDK for Ruby API Reference.

The following code example shows how to use StartInstances.

SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

require "aws-sdk-ec2" # Attempts to start an Amazon Elastic Compute Cloud (Amazon EC2) instance. # # Prerequisites: # # - The Amazon EC2 instance. # # @param ec2_client [Aws::EC2::Client] An initialized EC2 client. # @param instance_id [String] The ID of the instance. # @return [Boolean] true if the instance was started; otherwise, false. # @example # exit 1 unless instance_started?( # Aws::EC2::Client.new(region: 'us-west-2'), # 'i-123abc' # ) def instance_started?(ec2_client, instance_id) response = ec2_client.describe_instance_status(instance_ids: [instance_id]) if response.instance_statuses.count.positive? state = response.instance_statuses[0].instance_state.name case state when "pending" puts "Error starting instance: the instance is pending. Try again later." return false when "running" puts "The instance is already running." return true when "terminated" puts "Error starting instance: " \ "the instance is terminated, so you cannot start it." return false end end ec2_client.start_instances(instance_ids: [instance_id]) ec2_client.wait_until(:instance_running, instance_ids: [instance_id]) puts "Instance started." return true rescue StandardError => e puts "Error starting instance: #{e.message}" return false end # Example usage: def run_me instance_id = "" region = "" # Print usage information and then stop. if ARGV[0] == "--help" || ARGV[0] == "-h" puts "Usage: ruby ec2-ruby-example-start-instance-i-123abc.rb " \ "INSTANCE_ID REGION " # Replace us-west-2 with the AWS Region you're using for Amazon EC2. puts "Example: ruby ec2-ruby-example-start-instance-i-123abc.rb " \ "i-123abc us-west-2" exit 1 # If no values are specified at the command prompt, use these default values. # Replace us-west-2 with the AWS Region you're using for Amazon EC2. elsif ARGV.count.zero? instance_id = "i-123abc" region = "us-west-2" # Otherwise, use the values as specified at the command prompt. else instance_id = ARGV[0] region = ARGV[1] end ec2_client = Aws::EC2::Client.new(region: region) puts "Attempting to start instance '#{instance_id}' " \ "(this might take a few minutes)..." unless instance_started?(ec2_client, instance_id) puts "Could not start instance." end end run_me if $PROGRAM_NAME == __FILE__
  • For API details, see StartInstances in Amazon SDK for Ruby API Reference.

The following code example shows how to use StopInstances.

SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

require "aws-sdk-ec2" # Prerequisites: # # - The Amazon EC2 instance. # # @param ec2_client [Aws::EC2::Client] An initialized EC2 client. # @param instance_id [String] The ID of the instance. # @return [Boolean] true if the instance was stopped; otherwise, false. # @example # exit 1 unless instance_stopped?( # Aws::EC2::Client.new(region: 'us-west-2'), # 'i-123abc' # ) def instance_stopped?(ec2_client, instance_id) response = ec2_client.describe_instance_status(instance_ids: [instance_id]) if response.instance_statuses.count.positive? state = response.instance_statuses[0].instance_state.name case state when "stopping" puts "The instance is already stopping." return true when "stopped" puts "The instance is already stopped." return true when "terminated" puts "Error stopping instance: " \ "the instance is terminated, so you cannot stop it." return false end end ec2_client.stop_instances(instance_ids: [instance_id]) ec2_client.wait_until(:instance_stopped, instance_ids: [instance_id]) puts "Instance stopped." return true rescue StandardError => e puts "Error stopping instance: #{e.message}" return false end # Example usage: def run_me instance_id = "" region = "" # Print usage information and then stop. if ARGV[0] == "--help" || ARGV[0] == "-h" puts "Usage: ruby ec2-ruby-example-stop-instance-i-123abc.rb " \ "INSTANCE_ID REGION " # Replace us-west-2 with the AWS Region you're using for Amazon EC2. puts "Example: ruby ec2-ruby-example-start-instance-i-123abc.rb " \ "i-123abc us-west-2" exit 1 # If no values are specified at the command prompt, use these default values. # Replace us-west-2 with the AWS Region you're using for Amazon EC2. elsif ARGV.count.zero? instance_id = "i-123abc" region = "us-west-2" # Otherwise, use the values as specified at the command prompt. else instance_id = ARGV[0] region = ARGV[1] end ec2_client = Aws::EC2::Client.new(region: region) puts "Attempting to stop instance '#{instance_id}' " \ "(this might take a few minutes)..." unless instance_stopped?(ec2_client, instance_id) puts "Could not stop instance." end end run_me if $PROGRAM_NAME == __FILE__
  • For API details, see StopInstances in Amazon SDK for Ruby API Reference.

The following code example shows how to use TerminateInstances.

SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

require "aws-sdk-ec2" # Prerequisites: # # - The Amazon EC2 instance. # # @param ec2_client [Aws::EC2::Client] An initialized EC2 client. # @param instance_id [String] The ID of the instance. # @return [Boolean] true if the instance was terminated; otherwise, false. # @example # exit 1 unless instance_terminated?( # Aws::EC2::Client.new(region: 'us-west-2'), # 'i-123abc' # ) def instance_terminated?(ec2_client, instance_id) response = ec2_client.describe_instance_status(instance_ids: [instance_id]) if response.instance_statuses.count.positive? && response.instance_statuses[0].instance_state.name == "terminated" puts "The instance is already terminated." return true end ec2_client.terminate_instances(instance_ids: [instance_id]) ec2_client.wait_until(:instance_terminated, instance_ids: [instance_id]) puts "Instance terminated." return true rescue StandardError => e puts "Error terminating instance: #{e.message}" return false end # Example usage: def run_me instance_id = "" region = "" # Print usage information and then stop. if ARGV[0] == "--help" || ARGV[0] == "-h" puts "Usage: ruby ec2-ruby-example-terminate-instance-i-123abc.rb " \ "INSTANCE_ID REGION " # Replace us-west-2 with the AWS Region you're using for Amazon EC2. puts "Example: ruby ec2-ruby-example-terminate-instance-i-123abc.rb " \ "i-123abc us-west-2" exit 1 # If no values are specified at the command prompt, use these default values. # Replace us-west-2 with the AWS Region you're using for Amazon EC2. elsif ARGV.count.zero? instance_id = "i-123abc" region = "us-west-2" # Otherwise, use the values as specified at the command prompt. else instance_id = ARGV[0] region = ARGV[1] end ec2_client = Aws::EC2::Client.new(region: region) puts "Attempting to terminate instance '#{instance_id}' " \ "(this might take a few minutes)..." unless instance_terminated?(ec2_client, instance_id) puts "Could not terminate instance." end end run_me if $PROGRAM_NAME == __FILE__