EC2Exemples d'utilisation d'Amazon SDK pour Ruby - AWS SDKpour Ruby

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

EC2Exemples d'utilisation d'Amazon SDK pour Ruby

Les exemples de code suivants vous montrent comment effectuer des actions et implémenter des scénarios courants à l' AWS SDK for Ruby aide d'AmazonEC2.

Les actions sont des extraits de code de programmes plus larges et doivent être exécutées dans leur contexte. Alors que les actions vous montrent comment appeler des fonctions de service individuelles, vous pouvez les visualiser dans leur contexte dans leurs scénarios associés.

Chaque exemple inclut un lien vers le code source complet, où vous trouverez des instructions sur la façon de configurer et d'exécuter le code en contexte.

Mise en route

Les exemples de code suivants montrent comment commencer à utiliser AmazonEC2.

SDKpour Ruby
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

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
Rubriques

Actions

L'exemple de code suivant montre comment utiliserAllocateAddress.

SDKpour Ruby
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

# 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') response.allocation_id rescue StandardError => e puts "Error allocating Elastic IP address: #{e.message}" 'Error' end
  • Pour API plus de détails, voir AllocateAddressla section AWS SDK for Ruby APIRéférence.

L'exemple de code suivant montre comment utiliserAssociateAddress.

SDKpour Ruby
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

# 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 ) response.association_id rescue StandardError => e puts "Error associating Elastic IP address with instance: #{e.message}" 'Error' end
  • Pour API plus de détails, voir AssociateAddressla section AWS SDK for Ruby APIRéférence.

L'exemple de code suivant montre comment utiliserCreateKeyPair.

SDKpour Ruby
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

# 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}'." true rescue Aws::EC2::Errors::InvalidKeyPairDuplicate puts "Error creating key pair: a key pair named '#{key_pair_name}' " \ 'already exists.' false rescue StandardError => e puts "Error creating key pair or saving private key file: #{e.message}" 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) true rescue StandardError => e puts "Error deleting key pair: #{e.message}" 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__
  • Pour API plus de détails, voir CreateKeyPairla section AWS SDK for Ruby APIRéférence.

L'exemple de code suivant montre comment utiliserCreateRouteTable.

SDKpour Ruby
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

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}'." 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.' 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__
  • Pour API plus de détails, voir CreateRouteTablela section AWS SDK for Ruby APIRéférence.

L'exemple de code suivant montre comment utiliserCreateSecurityGroup.

SDKpour Ruby
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

# 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}'." security_group.group_id rescue StandardError => e puts "Error creating security group: #{e.message}" '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}'." true rescue StandardError => e puts "Error adding inbound rule to security group: #{e.message}" false end # Refactored method to simplify complexity for describing security group permissions def format_port_information(perm) from_port_str = perm.from_port == '-1' || perm.from_port == -1 ? 'All' : perm.from_port.to_s to_port_str = perm.to_port == '-1' || perm.to_port == -1 ? 'All' : perm.to_port.to_s { from_port: from_port_str, to_port: to_port_str } end # Displays information about a security group's IP permissions set in # Amazon Elastic Compute Cloud (Amazon EC2). def describe_security_group_permissions(perm) ports = format_port_information(perm) print " Protocol: #{perm.ip_protocol == '-1' ? 'All' : perm.ip_protocol}" print ", From: #{ports[:from_port]}, To: #{ports[:to_port]}" print ", CIDR IPv6: #{perm.ipv_6_ranges[0].cidr_ipv_6}" if perm.key?(:ipv_6_ranges) && perm.ipv_6_ranges.count.positive? print ", CIDR IPv4: #{perm.ip_ranges[0].cidr_ip}" if perm.key?(:ip_ranges) && perm.ip_ranges.count.positive? print "\n" end # Displays information about available security groups in # Amazon Elastic Compute Cloud (Amazon EC2). def describe_security_groups(ec2_client) response = ec2_client.describe_security_groups if response.security_groups.count.positive? response.security_groups.each do |sg| display_group_details(sg) end else puts 'No security groups found.' end rescue StandardError => e puts "Error getting information about security groups: #{e.message}" end # Helper method to display the details of security groups def display_group_details(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}" display_group_tags(sg.tags) if sg.tags.count.positive? display_group_permissions(sg) end def display_group_tags(tags) puts 'Tags:' tags.each do |tag| puts " Key: #{tag.key}, Value: #{tag.value}" end end def display_group_permissions(sg) if sg.ip_permissions.count.positive? puts 'Inbound rules:' sg.ip_permissions.each do |p| describe_security_group_permissions(p) end end return if sg.ip_permissions_egress.empty? puts 'Outbound rules:' sg.ip_permissions_egress.each do |p| describe_security_group_permissions(p) end end # Deletes an Amazon Elastic Compute Cloud (Amazon EC2) # security group. 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}'." true rescue StandardError => e puts "Error deleting security group: #{e.message}" false end # Example usage with refactored run_me to reduce complexity 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 = process_arguments ec2_client = Aws::EC2::Client.new(region: region) security_group_id = attempt_create_security_group(ec2_client, group_name, description, vpc_id) security_group_exists = security_group_id != 'Error' if security_group_exists add_inbound_rules(ec2_client, security_group_id, ip_protocol_http, from_port_http, to_port_http, cidr_ip_range_http) add_inbound_rules(ec2_client, security_group_id, ip_protocol_ssh, from_port_ssh, to_port_ssh, cidr_ip_range_ssh) end describe_security_groups(ec2_client) attempt_delete_security_group(ec2_client, security_group_id) if security_group_exists end def process_arguments if ARGV[0] == '--help' || ARGV[0] == '-h' display_help exit 1 elsif ARGV.count.zero? default_values else ARGV end end def attempt_create_security_group(ec2_client, group_name, description, vpc_id) puts 'Attempting to create security group...' security_group_id = create_security_group(ec2_client, group_name, description, vpc_id) puts 'Could not create security group. Skipping this step.' if security_group_id == 'Error' security_group_id end def add_inbound_rules(ec2_client, security_group_id, ip_protocol, from_port, to_port, cidr_ip_range) puts 'Attempting to add inbound rules to security group...' return if security_group_ingress_authorized?(ec2_client, security_group_id, ip_protocol, from_port, to_port, cidr_ip_range) puts 'Could not add inbound rule to security group. Skipping this step.' end def attempt_delete_security_group(ec2_client, security_group_id) puts "\nAttempting to delete security group..." return if security_group_deleted?(ec2_client, security_group_id) puts 'Could not delete security group. You must delete it yourself.' end def display_help 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" end def default_values [ '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' ] end run_me if $PROGRAM_NAME == __FILE__
  • Pour API plus de détails, voir CreateSecurityGroupla section AWS SDK for Ruby APIRéférence.

L'exemple de code suivant montre comment utiliserCreateSubnet.

SDKpour Ruby
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

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}'." true rescue StandardError => e puts "Error creating or tagging subnet: #{e.message}" 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__
  • Pour API plus de détails, voir CreateSubnetla section AWS SDK for Ruby APIRéférence.

L'exemple de code suivant montre comment utiliserCreateVpc.

SDKpour Ruby
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

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}'." true rescue StandardError => e puts e.message 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__
  • Pour API plus de détails, voir CreateVpcla section AWS SDK for Ruby APIRéférence.

L'exemple de code suivant montre comment utiliserDescribeInstances.

SDKpour Ruby
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

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__
  • Pour API plus de détails, voir DescribeInstancesla section AWS SDK for Ruby APIRéférence.

L'exemple de code suivant montre comment utiliserDescribeRegions.

SDKpour Ruby
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

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__
  • Pour API plus de détails, voir DescribeRegionsla section AWS SDK for Ruby APIRéférence.

L'exemple de code suivant montre comment utiliserReleaseAddress.

SDKpour Ruby
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

# 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) true rescue StandardError => e puts("Error releasing Elastic IP address: #{e.message}") false end
  • Pour API plus de détails, voir ReleaseAddressla section AWS SDK for Ruby APIRéférence.

L'exemple de code suivant montre comment utiliserStartInstances.

SDKpour Ruby
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

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.' true rescue StandardError => e puts "Error starting instance: #{e.message}" 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)...' return if instance_started?(ec2_client, instance_id) puts 'Could not start instance.' end run_me if $PROGRAM_NAME == __FILE__
  • Pour API plus de détails, voir StartInstancesla section AWS SDK for Ruby APIRéférence.

L'exemple de code suivant montre comment utiliserStopInstances.

SDKpour Ruby
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

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.' true rescue StandardError => e puts "Error stopping instance: #{e.message}" 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)...' return if instance_stopped?(ec2_client, instance_id) puts 'Could not stop instance.' end run_me if $PROGRAM_NAME == __FILE__
  • Pour API plus de détails, voir StopInstancesla section AWS SDK for Ruby APIRéférence.

L'exemple de code suivant montre comment utiliserTerminateInstances.

SDKpour Ruby
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

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.' true rescue StandardError => e puts "Error terminating instance: #{e.message}" 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)...' return if instance_terminated?(ec2_client, instance_id) puts 'Could not terminate instance.' end run_me if $PROGRAM_NAME == __FILE__
  • Pour API plus de détails, voir TerminateInstancesla section AWS SDK for Ruby APIRéférence.