Ada lebih banyak contoh AWS SDK yang tersedia di GitHub repo Contoh
Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
Gunakan DescribeInstanceTypesdengan AWS SDK atau CLI
Contoh kode berikut menunjukkan cara menggunakanDescribeInstanceTypes.
Contoh tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Anda dapat melihat tindakan ini dalam konteks dalam contoh kode berikut:
- .NET
-
- SDK untuk .NET
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode AWS
. /// <summary> /// Describe the instance types available. /// </summary> /// <returns>A list of instance type information.</returns> public async Task<List<InstanceTypeInfo>> DescribeInstanceTypes(ArchitectureValues architecture) { try { var request = new DescribeInstanceTypesRequest(); var filters = new List<Filter> { new Filter("processor-info.supported-architecture", new List<string> { architecture.ToString() }) }; filters.Add(new Filter("instance-type", new() { "*.micro", "*.small" })); request.Filters = filters; var instanceTypes = new List<InstanceTypeInfo>(); var paginator = _amazonEC2.Paginators.DescribeInstanceTypes(request); await foreach (var instanceType in paginator.InstanceTypes) { instanceTypes.Add(instanceType); } return instanceTypes; } catch (AmazonEC2Exception ec2Exception) { if (ec2Exception.ErrorCode == "InvalidParameterValue") { _logger.LogError( $"Parameters are invalid. Ensure architecture and size strings conform to DescribeInstanceTypes API reference."); } throw; } catch (Exception ex) { Console.WriteLine($"Couldn't delete the security group because: {ex.Message}"); throw; } }-
Untuk detail API, lihat DescribeInstanceTypesdi Referensi AWS SDK untuk .NET API.
-
- Bash
-
- AWS CLI dengan skrip Bash
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode AWS
. ############################################################################### # ec2_describe_instance_types # # This function describes EC2 instance types filtered by processor architecture # and optionally by instance type. It takes the following arguments: # # -a, --architecture ARCHITECTURE Specify the processor architecture (e.g., x86_64) # -t, --type INSTANCE_TYPE Comma-separated list of instance types (e.g., t2.micro) # -h, --help Show the usage help # # The function prints the instance type and supported architecture for each # matching instance type. ############################################################################### function ec2_describe_instance_types() { local architecture="" local instance_types="" # bashsupport disable=BP5008 function usage() { echo "Usage: ec2_describe_instance_types [-a|--architecture ARCHITECTURE] [-t|--type INSTANCE_TYPE] [-h|--help]" echo " -a, --architecture ARCHITECTURE Specify the processor architecture (e.g., x86_64)" echo " -t, --type INSTANCE_TYPE Comma-separated list of instance types (e.g., t2.micro)" echo " -h, --help Show this help message" } while [[ $# -gt 0 ]]; do case "$1" in -a | --architecture) architecture="$2" shift 2 ;; -t | --type) instance_types="$2" shift 2 ;; -h | --help) usage return 0 ;; *) echo "Unknown argument: $1" return 1 ;; esac done if [[ -z "$architecture" ]]; then errecho "Error: Architecture not specified." usage return 1 fi if [[ -z "$instance_types" ]]; then errecho "Error: Instance type not specified." usage return 1 fi local tmp_json_file="temp_ec2.json" echo -n '[ { "Name": "processor-info.supported-architecture", "Values": [' >"$tmp_json_file" local items IFS=',' read -ra items <<<"$architecture" local array_size array_size=${#items[@]} for i in $(seq 0 $((array_size - 1))); do echo -n '"'"${items[$i]}"'"' >>"$tmp_json_file" if [[ $i -lt $((array_size - 1)) ]]; then echo -n ',' >>"$tmp_json_file" fi done echo -n ']}, { "Name": "instance-type", "Values": [' >>"$tmp_json_file" IFS=',' read -ra items <<<"$instance_types" local array_size array_size=${#items[@]} for i in $(seq 0 $((array_size - 1))); do echo -n '"'"${items[$i]}"'"' >>"$tmp_json_file" if [[ $i -lt $((array_size - 1)) ]]; then echo -n ',' >>"$tmp_json_file" fi done echo -n ']}]' >>"$tmp_json_file" local response response=$(aws ec2 describe-instance-types --filters file://"$tmp_json_file" \ --query 'InstanceTypes[*].[InstanceType]' --output text) local error_code=$? rm "$tmp_json_file" if [[ $error_code -ne 0 ]]; then aws_cli_error_log $error_code echo "ERROR: AWS reports describe-instance-types operation failed." return 1 fi echo "$response" return 0 }Fungsi utilitas yang digunakan dalam contoh ini.
############################################################################### # function errecho # # This function outputs everything sent to it to STDERR (standard error output). ############################################################################### function errecho() { printf "%s\n" "$*" 1>&2 } ############################################################################## # function aws_cli_error_log() # # This function is used to log the error messages from the AWS CLI. # # The function expects the following argument: # $1 - The error code returned by the AWS CLI. # # Returns: # 0: - Success. # ############################################################################## function aws_cli_error_log() { local err_code=$1 errecho "Error code : $err_code" if [ "$err_code" == 1 ]; then errecho " One or more S3 transfers failed." elif [ "$err_code" == 2 ]; then errecho " Command line failed to parse." elif [ "$err_code" == 130 ]; then errecho " Process received SIGINT." elif [ "$err_code" == 252 ]; then errecho " Command syntax invalid." elif [ "$err_code" == 253 ]; then errecho " The system environment or configuration was invalid." elif [ "$err_code" == 254 ]; then errecho " The service returned an error." elif [ "$err_code" == 255 ]; then errecho " 255 is a catch-all error." fi return 0 }-
Untuk detail API, lihat DescribeInstanceTypesdi Referensi AWS CLI Perintah.
-
- CLI
-
- AWS CLI
-
Contoh 1: Untuk menjelaskan tipe instans
Contoh
describe-instance-typesberikut menampilkan detail untuk tipe instans tertentu.aws ec2 describe-instance-types \ --instance-typest2.microOutput:
{ "InstanceTypes": [ { "InstanceType": "t2.micro", "CurrentGeneration": true, "FreeTierEligible": true, "SupportedUsageClasses": [ "on-demand", "spot" ], "SupportedRootDeviceTypes": [ "ebs" ], "BareMetal": false, "Hypervisor": "xen", "ProcessorInfo": { "SupportedArchitectures": [ "i386", "x86_64" ], "SustainedClockSpeedInGhz": 2.5 }, "VCpuInfo": { "DefaultVCpus": 1, "DefaultCores": 1, "DefaultThreadsPerCore": 1, "ValidCores": [ 1 ], "ValidThreadsPerCore": [ 1 ] }, "MemoryInfo": { "SizeInMiB": 1024 }, "InstanceStorageSupported": false, "EbsInfo": { "EbsOptimizedSupport": "unsupported", "EncryptionSupport": "supported" }, "NetworkInfo": { "NetworkPerformance": "Low to Moderate", "MaximumNetworkInterfaces": 2, "Ipv4AddressesPerInterface": 2, "Ipv6AddressesPerInterface": 2, "Ipv6Supported": true, "EnaSupport": "unsupported" }, "PlacementGroupInfo": { "SupportedStrategies": [ "partition", "spread" ] }, "HibernationSupported": false, "BurstablePerformanceSupported": true, "DedicatedHostsSupported": false, "AutoRecoverySupported": true } ] }Untuk informasi selengkapnya, lihat Jenis Instans di Panduan Pengguna Amazon Elastic Compute Cloud untuk Instans Linux.
Contoh 2: Untuk memfilter tipe instans yang tersedia
Anda dapat menentukan filter guna mencakup hasil untuk tipe instans yang memiliki karakteristik khusus. Contoh
describe-instance-typesberikut mencantumkan tipe instans yang mendukung hibernasi.aws ec2 describe-instance-types \ --filtersName=hibernation-supported,Values=true--query 'InstanceTypes[*].InstanceType'Output:
[ "m5.8xlarge", "r3.large", "c3.8xlarge", "r5.large", "m4.4xlarge", "c4.large", "m5.xlarge", "m4.xlarge", "c3.large", "c4.8xlarge", "c4.4xlarge", "c5.xlarge", "c5.12xlarge", "r5.4xlarge", "c5.4xlarge" ]Untuk informasi selengkapnya, lihat Jenis Instans di Panduan Pengguna Amazon Elastic Compute Cloud untuk Instans Linux.
-
Untuk detail API, lihat DescribeInstanceTypes
di Referensi AWS CLI Perintah.
-
- Java
-
- SDK untuk Java 2.x
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode AWS
. /** * Asynchronously retrieves the instance types available in the current AWS region. * <p> * This method uses the AWS SDK's asynchronous API to fetch the available instance types * and then processes the response. It logs the memory information, network information, * and instance type for each instance type returned. Additionally, it returns a * {@link CompletableFuture} that resolves to the instance type string for the "t2.2xlarge" * instance type, if it is found in the response. If the "t2.2xlarge" instance type is not * found, an empty string is returned. * </p> * * @return a {@link CompletableFuture} that resolves to the instance type string for the * "t2.2xlarge" instance type, or an empty string if the instance type is not found */ public CompletableFuture<String> getInstanceTypesAsync() { DescribeInstanceTypesRequest typesRequest = DescribeInstanceTypesRequest.builder() .maxResults(10) .build(); CompletableFuture<DescribeInstanceTypesResponse> response = getAsyncClient().describeInstanceTypes(typesRequest); response.whenComplete((resp, ex) -> { if (resp != null) { List<InstanceTypeInfo> instanceTypes = resp.instanceTypes(); for (InstanceTypeInfo type : instanceTypes) { logger.info("The memory information of this type is " + type.memoryInfo().sizeInMiB()); logger.info("Network information is " + type.networkInfo().toString()); logger.info("Instance type is " + type.instanceType().toString()); } } else { throw (RuntimeException) ex; } }); return response.thenApply(resp -> { for (InstanceTypeInfo type : resp.instanceTypes()) { String instanceType = type.instanceType().toString(); if (instanceType.equals("t2.2xlarge")) { return instanceType; } } return ""; }); }-
Untuk detail API, lihat DescribeInstanceTypesdi Referensi AWS SDK for Java 2.x API.
-
- JavaScript
-
- SDK untuk JavaScript (v3)
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode AWS
. import { EC2Client, paginateDescribeInstanceTypes } from "@aws-sdk/client-ec2"; /** * Describes the specified instance types. By default, all instance types for the * current Region are described. Alternatively, you can filter the results. * @param {{ pageSize: string, supportedArch: string[], freeTier: boolean }} options */ export const main = async ({ pageSize, supportedArch, freeTier }) => { pageSize = Number.parseInt(pageSize); const client = new EC2Client({}); // The paginate function is a wrapper around the underlying command. const paginator = paginateDescribeInstanceTypes( // Without limiting the page size, this call can take a long time. pageSize is just sugar for // the MaxResults property in the underlying command. { client, pageSize }, { Filters: [ { Name: "processor-info.supported-architecture", Values: supportedArch, }, { Name: "free-tier-eligible", Values: [freeTier ? "true" : "false"] }, ], }, ); try { /** * @type {import('@aws-sdk/client-ec2').InstanceTypeInfo[]} */ const instanceTypes = []; for await (const page of paginator) { if (page.InstanceTypes.length) { instanceTypes.push(...page.InstanceTypes); // When we have at least 1 result, we can stop. if (instanceTypes.length >= 1) { break; } } } console.log( `Memory size in MiB for matching instance types:\n\n${instanceTypes.map((it) => `${it.InstanceType}: ${it.MemoryInfo.SizeInMiB} MiB`).join("\n")}`, ); } catch (caught) { if (caught instanceof Error && caught.name === "InvalidParameterValue") { console.warn(`${caught.message}`); return []; } throw caught; } };-
Untuk detail API, lihat DescribeInstanceTypesdi Referensi AWS SDK untuk JavaScript API.
-
- Kotlin
-
- SDK untuk Kotlin
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode AWS
. // Get a list of instance types. suspend fun getInstanceTypesSc(): String { var instanceType = "" val filterObs = ArrayList<Filter>() val filter = Filter { name = "processor-info.supported-architecture" values = listOf("arm64") } filterObs.add(filter) val typesRequest = DescribeInstanceTypesRequest { filters = filterObs maxResults = 10 } Ec2Client.fromEnvironment { region = "us-west-2" }.use { ec2 -> val response = ec2.describeInstanceTypes(typesRequest) response.instanceTypes?.forEach { type -> println("The memory information of this type is ${type.memoryInfo?.sizeInMib}") println("Maximum number of network cards is ${type.networkInfo?.maximumNetworkCards}") instanceType = type.instanceType.toString() } return instanceType } }-
Untuk detail API, lihat DescribeInstanceTypes
di AWS SDK untuk referensi API Kotlin.
-
- Python
-
- SDK untuk Python (Boto3)
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode AWS
. class EC2InstanceWrapper: """Encapsulates Amazon Elastic Compute Cloud (Amazon EC2) instance actions using the client interface.""" def __init__( self, ec2_client: Any, instances: Optional[List[Dict[str, Any]]] = None ) -> None: """ Initializes the EC2InstanceWrapper with an EC2 client and optional instances. :param ec2_client: A Boto3 Amazon EC2 client. This client provides low-level access to AWS EC2 services. :param instances: A list of dictionaries representing Boto3 Instance objects. These are high-level objects that wrap instance actions. """ self.ec2_client = ec2_client self.instances = instances or [] @classmethod def from_client(cls) -> "EC2InstanceWrapper": """ Creates an EC2InstanceWrapper instance with a default EC2 client. :return: An instance of EC2InstanceWrapper initialized with the default EC2 client. """ ec2_client = boto3.client("ec2") return cls(ec2_client) def get_instance_types( self, architecture: str = "x86_64", sizes: List[str] = ["*.micro", "*.small"] ) -> List[Dict[str, Any]]: """ Gets instance types that support the specified architecture and size. See https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeInstanceTypes.html for a list of allowable parameters. :param architecture: The architecture supported by instance types. Default: 'x86_64'. :param sizes: The size of instance types. Default: '*.micro', '*.small', :return: A list of dictionaries representing instance types that support the specified architecture and size. """ try: inst_types = [] paginator = self.ec2_client.get_paginator("describe_instance_types") for page in paginator.paginate( Filters=[ { "Name": "processor-info.supported-architecture", "Values": [architecture], }, {"Name": "instance-type", "Values": sizes}, ] ): inst_types += page["InstanceTypes"] except ClientError as err: logger.error( f"Failed to get instance types: {architecture}, {','.join(map(str, sizes))}" ) error_code = err.response["Error"]["Code"] if error_code == "InvalidParameterValue": logger.error( "Parameters are invalid. " "Ensure architecture and size strings conform to DescribeInstanceTypes API reference." ) raise else: return inst_types-
Untuk detail API, lihat DescribeInstanceTypesdi AWS SDK for Python (Boto3) Referensi API.
-
- Rust
-
- SDK for Rust
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode AWS
. /// List instance types that match an image's architecture and are free tier eligible. pub async fn list_instance_types(&self, image: &Image) -> Result<Vec<InstanceType>, EC2Error> { let architecture = format!( "{}", image.architecture().ok_or_else(|| EC2Error::new(format!( "Image {:?} does not have a listed architecture", image.image_id() )))? ); let free_tier_eligible_filter = Filter::builder() .name("free-tier-eligible") .values("false") .build(); let supported_architecture_filter = Filter::builder() .name("processor-info.supported-architecture") .values(architecture) .build(); let response = self .client .describe_instance_types() .filters(free_tier_eligible_filter) .filters(supported_architecture_filter) .send() .await?; Ok(response .instance_types .unwrap_or_default() .into_iter() .filter_map(|iti| iti.instance_type) .collect()) }-
Untuk detail API, lihat DescribeInstanceTypes
referensi AWS SDK for Rust API.
-
- SAP ABAP
-
- SDK for SAP ABAP
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode AWS
. " Create filters for architecture and instance type patterns " iv_architecture = 'x86_64' DATA lt_filters TYPE /aws1/cl_ec2filter=>tt_filterlist. APPEND NEW /aws1/cl_ec2filter( iv_name = 'processor-info.supported-architecture' it_values = VALUE /aws1/cl_ec2valuestringlist_w=>tt_valuestringlist( ( NEW /aws1/cl_ec2valuestringlist_w( iv_architecture ) ) ) ) TO lt_filters. " Filter for instance type patterns like '*.micro', '*.small' APPEND NEW /aws1/cl_ec2filter( iv_name = 'instance-type' it_values = VALUE /aws1/cl_ec2valuestringlist_w=>tt_valuestringlist( ( NEW /aws1/cl_ec2valuestringlist_w( '*.micro' ) ) ( NEW /aws1/cl_ec2valuestringlist_w( '*.small' ) ) ) ) TO lt_filters. TRY. oo_result = lo_ec2->describeinstancetypes( it_filters = lt_filters ). " oo_result is returned for testing purposes. " DATA(lt_instance_types) = oo_result->get_instancetypes( ). MESSAGE 'Retrieved information about EC2 instance types.' 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.-
Untuk detail API, lihat DescribeInstanceTypesdi AWS SDK untuk referensi SAP ABAP API.
-
- Swift
-
- SDK para Swift
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode AWS
. import AWSEC2 /// Return a list of instance types matching the specified architecture /// and instance sizes. /// /// - Parameters: /// - architecture: The architecture of the instance types to return, as /// a member of `EC2ClientTypes.ArchitectureValues`. /// - sizes: An array of one or more strings identifying sizes of /// instance type to accept. /// /// - Returns: An array of `EC2ClientTypes.InstanceTypeInfo` records /// describing the instance types matching the given requirements. func getMatchingInstanceTypes(architecture: EC2ClientTypes.ArchitectureValues = EC2ClientTypes.ArchitectureValues.x8664, sizes: [String] = ["*.micro", "*.small"]) async -> [EC2ClientTypes.InstanceTypeInfo] { var instanceTypes: [EC2ClientTypes.InstanceTypeInfo] = [] let archFilter = EC2ClientTypes.Filter( name: "processor-info.supported-architecture", values: [architecture.rawValue] ) let sizeFilter = EC2ClientTypes.Filter( name: "instance-type", values: sizes ) do { let pages = ec2Client.describeInstanceTypesPaginated( input: DescribeInstanceTypesInput( filters: [archFilter, sizeFilter] ) ) for try await page in pages { guard let types = page.instanceTypes else { return [] } instanceTypes += types } } catch { print("*** Error getting image types: \(error.localizedDescription)") return [] } return instanceTypes }-
Untuk detail API, lihat referensi DescribeInstanceTypes AWS
SDK untuk Swift API.
-