AWSSDK を使用して Amazon EC2 インスタンスタイプに関するデータを取得する - AWSSDK コードサンプル

AWSDocAWS SDKGitHub サンプルリポジトリには、さらに多くの SDK サンプルがあります


AWSSDK を使用して Amazon EC2 インスタンスタイプに関するデータを取得する

次のコード例は、Amazon EC2 インスタンスタイプに関するデータを取得する方法を示しています。


他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

/// <summary> /// Describe the instance types available. /// </summary> /// <returns>A list of instance type information.</returns> public async Task<List<InstanceTypeInfo>> DescribeInstanceTypes(ArchitectureValues architecture) { 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; }
  • API の詳細については、AWS SDK for .NETAPI DescribeInstanceTypesリファレンスのを参照してください

SDK for Java 2.x

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

// Get a list of instance types. public static String getInstanceTypes(Ec2Client ec2) { String instanceType=""; try { List<Filter> filters = new ArrayList<>(); Filter filter = Filter.builder() .name("processor-info.supported-architecture") .values("arm64") .build(); filters.add(filter); DescribeInstanceTypesRequest typesRequest = DescribeInstanceTypesRequest.builder() .filters(filters) .maxResults(10) .build(); DescribeInstanceTypesResponse response = ec2.describeInstanceTypes(typesRequest); List<InstanceTypeInfo> instanceTypes = response.instanceTypes(); for (InstanceTypeInfo type: instanceTypes) { System.out.println("The memory information of this type is "+type.memoryInfo().sizeInMiB()); System.out.println("Network information is "+type.networkInfo().toString()); instanceType = type.instanceType().toString(); } return instanceType; } catch (SsmException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; }
  • API の詳細については、AWS SDK for Java 2.xAPI DescribeInstanceTypesリファレンスのを参照してください

SDK forJavaScript (v3)

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

import { paginateDescribeInstanceTypes, DescribeInstanceTypesCommand, } from "@aws-sdk/client-ec2"; import { client } from "../libs/client.js"; // List at least the first arm64 EC2 instance type available. export const main = async () => { // 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: 25 }, { Filters: [ { Name: "processor-info.supported-architecture", Values: ["x86_64"] }, { Name: "free-tier-eligible", Values: ["true"] }, ], } ); try { const instanceTypes = []; for await (const page of paginator) { if (page.InstanceTypes.length) { instanceTypes.push(; // When we have at least 1 result, we can stop. if (instanceTypes.length >= 1) { break; } } } console.log(instanceTypes); } catch (err) { console.error(err); } };
  • API の詳細については、AWS SDK for JavaScriptAPI DescribeInstanceTypesリファレンスのを参照してください

SDK for Kotlin



他にもありますGitHub。用例一覧を検索し、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 { 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 } }
  • API の詳細については、「AWSSDK for Kotlin API リファレンス」を参照してくださいDescribeInstanceTypes

SDK for Python (Boto3)

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

class InstanceWrapper: """Encapsulates Amazon Elastic Compute Cloud (Amazon EC2) instance actions.""" def __init__(self, ec2_resource, instance=None): """ :param ec2_resource: A Boto3 Amazon EC2 resource. This high-level resource is used to create additional high-level objects that wrap low-level Amazon EC2 service actions. :param instance: A Boto3 Instance object. This is a high-level object that wraps instance actions. """ self.ec2_resource = ec2_resource self.instance = instance @classmethod def from_resource(cls): ec2_resource = boto3.resource('ec2') return cls(ec2_resource) def get_instance_types(self, architecture): """ Gets instance types that support the specified architecture and are designated as either 'micro' or 'small'. When an instance is created, the instance type you specify must support the architecture of the AMI you use. :param architecture: The kind of architecture the instance types must support, such as 'x86_64'. :return: A list of instance types that support the specified architecture and are either 'micro' or 'small'. """ try: inst_types = [] it_paginator = self.ec2_resource.meta.client.get_paginator('describe_instance_types') for page in it_paginator.paginate( Filters=[{ 'Name': 'processor-info.supported-architecture', 'Values': [architecture]}, {'Name': 'instance-type', 'Values': ['*.micro', '*.small']}]): inst_types += page['InstanceTypes'] except ClientError as err: logger.error( "Couldn't get instance types. Here's why: %s: %s", err.response['Error']['Code'], err.response['Error']['Message']) raise else: return inst_types
  • API の詳細については、「AWSSDK for Python (Boto3) API リファレンス」を参照してくださいDescribeInstanceTypes