使用 AWS SDK 描述帳戶的區域
下列程式碼範例示範如何描述 Amazon EC2 區域。
- C++
-
- 適用於 C++ 的開發套件
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::DescribeRegionsRequest request; auto outcome = ec2Client.DescribeRegions(request); bool result = true; if (outcome.IsSuccess()) { std::cout << std::left << std::setw(32) << "RegionName" << std::setw(64) << "Endpoint" << std::endl; const auto ®ions = outcome.GetResult().GetRegions(); for (const auto ®ion: regions) { std::cout << std::left << std::setw(32) << region.GetRegionName() << std::setw(64) << region.GetEndpoint() << std::endl; } } else { std::cerr << "Failed to describe regions:" << outcome.GetError().GetMessage() << std::endl; result = false; }
-
如需 API 詳細資訊,請參閱《AWS SDK for C++ API 參考》中的 DescribeRegions。
-
- JavaScript
-
- SDK for JavaScript (v3)
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 import { DescribeRegionsCommand } from "@aws-sdk/client-ec2"; import { client } from "../libs/client.js"; export const main = async () => { const command = new DescribeRegionsCommand({ // By default this command will not show regions that require you to opt-in. // When AllRegions true even the regions that require opt-in will be returned. AllRegions: true, // You can omit the Filters property if you want to get all regions. Filters: [ { Name: "region-name", // You can specify multiple values for a filter. // You can also use '*' as a wildcard. This will return all // of the regions that start with `us-east-`. Values: ["ap-southeast-4"], }, ], }); try { const { Regions } = await client.send(command); const regionsList = Regions.map((reg) => ` • ${reg.RegionName}`); console.log("Found regions:"); console.log(regionsList.join("\n")); } catch (err) { console.error(err); } };
-
如需 API 詳細資訊,請參閱《AWS SDK for JavaScript API 參考》中的 DescribeRegions。
-
- Ruby
-
- 適用於 Ruby 的開發套件
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 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.to_s print " " * (max_region_string_length - region.region_name.length) print " " print region.endpoint.to_s 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 # Full example call: 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__
-
如需 API 詳細資訊,請參閱《AWS SDK for Ruby API 參考》中的 DescribeRegions。
-
- Rust
-
- SDK for Rust
-
注意
這是適用於預覽版本 SDK 的文件。SDK 可能會變更,請勿用於生產環境。
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 async fn show_regions(client: &Client) -> Result<(), Error> { let rsp = client.describe_regions().send().await?; println!("Regions:"); for region in rsp.regions() { println!(" {}", region.region_name().unwrap()); } Ok(()) }
-
如需 API 詳細資訊,請參閱《適用於 Rust 的 AWS SDK API 參考》中的 DescribeRegions
。
-
- SAP ABAP
-
- 適用於 SAP ABAP 的開發套件
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 TRY. oo_result = lo_ec2->describeregions( ) . " oo_result is returned for testing purposes. " DATA(lt_regions) = oo_result->get_regions( ). MESSAGE 'Retrieved information about Regions.' TYPE 'I'. CATCH /aws1/cx_rt_service_generic INTO DATA(lo_exception). DATA(lv_error) = |"{ lo_exception->av_err_code }" - { lo_exception->av_err_msg }|. MESSAGE lv_error TYPE 'E'. ENDTRY.
-
如需 API 詳細資訊,請參閱《適用於 SAP ABAP 的 AWS SDK API 參考》中的 DescribeRegions。
-
如需完整的 AWS SDK 開發人員指南和程式碼範例清單,請參閱使用 Amazon EC2 搭配 AWS SDK。此主題也包含入門相關資訊和舊版 SDK 的詳細資訊。
描述可用區域
描述執行個體狀態