英文:
AWS SDK in Go. Parsing data
问题
我正在使用aws-sdk来进行Go语言开发。我想根据实例类型过滤器来描述EC2实例。
以下是我的代码:
ec2Svc := ec2.New(sess, &aws.Config{Credentials: creds})
params := &ec2.DescribeInstanceTypesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("instance-type"),
Values: []*string{aws.String("t2*")},
},
},
}
result, err := ec2Svc.DescribeInstanceTypes(params)
if err != nil {
fmt.Println("Error", err)
} else {
fmt.Println(result.String())
}
这段代码运行良好。result
变量的类型是ec2.DescribeInstanceTypeOutput。以下是result
的样式:
{
"InstanceTypes": [
{
"AutoRecoverySupported": true,
"BareMetal": false,
"BurstablePerformanceSupported": true,
"CurrentGeneration": true,
"DedicatedHostsSupported": false,
"EbsInfo": {
"EbsOptimizedSupport": "unsupported",
"EncryptionSupport": "supported",
"NvmeSupport": "unsupported"
},
"FreeTierEligible": false,
"InstanceStorageSupported": false,
"InstanceType": "t2.2xlarge",
"MemoryInfo": {
"SizeInMiB": 32768
},
"NetworkInfo": {
"DefaultNetworkCardIndex": 0,
"MaximumNetworkCards": 1,
"MaximumNetworkInterfaces": 3,
"NetworkCards": [
{
"MaximumNetworkInterfaces": 3
}
],
"NetworkPerformance": "Moderate"
},
"PlacementGroupInfo": {
"SupportedStrategies": [
"partition",
"spread"
]
},
"ProcessorInfo": {
"SupportedArchitectures": [
"x86_64"
]
},
"SupportedBootModes": [
"legacy-bios"
],
"SupportedVirtualizationTypes": [
"hvm"
],
"VCpuInfo": {
"DefaultCores": 8
}
}
],
"NextToken": "asadasdasd"
}
如你所见,这个JSON非常长,我已经删除了其中一些内容以便更易读。我想从中获取一个信息 - MemoryInfo.SizeInMiB
,但我不知道如何做到。我尝试使用json.Marshal
和json.Unmarshal
,但由于ec2.DescribeInstanceTypeOutput类型的原因,我遇到了错误。
获取这个值的唯一方法是创建一个相应的结构,然后进行处理吗?
英文:
I'm using aws-sdk for go. I want to describe EC2 instance based on instance-type filter.
Here's my code:
ec2Svc := ec2.New(sess, &aws.Config{Credentials: creds})
params := &ec2.DescribeInstanceTypesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("instance-type"),
Values: []*string{aws.String("t2*")},
},
},
}
result, err := ec2Svc.DescribeInstanceTypes(params)
if err != nil {
fmt.Println("Error", err)
} else {
fmt.Println(result.String())
}
This works fine. The type of result variable is *ec2.DescribeInstanceTypeOutput. Here's how result looks like:
{
InstanceTypes: [{
AutoRecoverySupported: true,
BareMetal: false,
BurstablePerformanceSupported: true,
CurrentGeneration: true,
DedicatedHostsSupported: false,
EbsInfo: {
EbsOptimizedSupport: "unsupported",
EncryptionSupport: "supported",
NvmeSupport: "unsupported"
},
FreeTierEligible: false,
InstanceStorageSupported: false,
InstanceType: "t2.2xlarge",
MemoryInfo: {
SizeInMiB: 32768
},
NetworkInfo: {
DefaultNetworkCardIndex: 0,
MaximumNetworkCards: 1,
MaximumNetworkInterfaces: 3,
NetworkCards: [{
MaximumNetworkInterfaces: 3,
}],
NetworkPerformance: "Moderate"
},
PlacementGroupInfo: {
SupportedStrategies: ["partition","spread"]
},
ProcessorInfo: {
SupportedArchitectures: ["x86_64"],
},
SupportedBootModes: ["legacy-bios"],
SupportedVirtualizationTypes: ["hvm"],
VCpuInfo: {
DefaultCores: 8,
}
}],
NextToken: "asadasdasd"
}
As you can see this json is quite long and I've removed some content from it to make more readable. I want to get one information from it - MemoryInfo.SizeInMiB
but I don't know how to. I've tried using json.Marshal
and json.Unmarshal
but I get error because of *ec2.DescribeInstanceTypeOutput type.
Is the only way to get this value is to create some long structure and then using it somehow?
答案1
得分: 1
您不需要创建新的结构,SDK已经提供了这个功能。DescribeInstanceTypes
的返回类型是一个指向DescribeInstanceTypesOutput
对象的指针。
您可以通过以下方式访问MemoryInfo.SizeInMiB
:
func main() {
// 创建一个会话
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
ec2Svc := ec2.New(sess)
params := &ec2.DescribeInstanceTypesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("instance-type"),
Values: []*string{aws.String("t2*")},
},
},
}
// 使用NextToken循环检索实例类型。实例的过滤将在本地进行
for {
result, err := ec2Svc.DescribeInstanceTypes(params)
if err != nil {
fmt.Println("错误", err)
break
} else {
// InstanceTypes是一个切片,所以我们需要遍历它
for _, instance := range result.InstanceTypes {
// 获取实例的内存大小
fmt.Printf("%s - %d MiB\n", *instance.InstanceType, *instance.MemoryInfo.SizeInMiB)
}
// 检查是否还有实例剩余
if result.NextToken != nil {
params.NextToken = result.NextToken
} else {
break
}
}
}
}
这应该会输出类似以下内容:
t2.2xlarge - 32768 MiB
t2.large - 8192 MiB
t2.micro - 1024 MiB
t2.medium - 4096 MiB
t2.xlarge - 16384 MiB
t2.small - 2048 MiB
t2.nano - 512 MiB
英文:
You don't have to create a new structure, this is already provided by the SDK. The return type of DescribeInstanceTypes
is a pointer to a DescribeInstanceTypesOutput
object.
You can access MemoryInfo.SizeInMiB
in the following way:
func main() {
// Build a session
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
ec2Svc := ec2.New(sess)
params := &ec2.DescribeInstanceTypesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("instance-type"),
Values: []*string{aws.String("t2*")},
},
},
}
// Retrieve the instance types in a loop using NextToken. The filtering of instances will happen locally
for {
result, err := ec2Svc.DescribeInstanceTypes(params)
if err != nil {
fmt.Println("Error", err)
break
} else {
// InstanceTypes is slice, so we have to iterate over it
for _, instance := range result.InstanceTypes {
// Grab the memory size of the instance
fmt.Printf("%s - %d MiB\n", *instance.InstanceType, *instance.MemoryInfo.SizeInMiB)
}
// Check if they are any instances left
if result.NextToken != nil {
params.NextToken = result.NextToken
} else {
break
}
}
}
}
This should output something similar to this:
t2.2xlarge - 32768 MiB
t2.large - 8192 MiB
t2.micro - 1024 MiB
t2.medium - 4096 MiB
t2.xlarge - 16384 MiB
t2.small - 2048 MiB
t2.nano - 512 MiB
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论