使用C#获取ec2

编程入门 行业动态 更新时间:2024-10-10 12:26:56
本文介绍了使用C#获取ec2-instance标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我不是开发人员,所以也许答案是其他解决方案,但是我不能真正从python或其他东西翻译出来。

I'm not a developer so maybe the answer is out there for a different solution but I can't really translate it from python or something else.

我正在尝试使用AWS .NET SDK查找实例,然后获取实例的标签。我已经能够确定实例是否已启动并正在运行。我还将看到如何创建和删除标签(下面的代码示例中没有)。但是我看不到一种简单的方法来实际检查标签是否存在并获取标签的值(如果标签确实存在)。

I'm trying to use the AWS .NET SDK to find an instance and then get the instance's tags. I've gotten as far as being able to determine if an instance is up and running or not. I also see how I can create and delete tags (not in code example below). But I don't see an easy way to actually check if a tag exists and get the value of the tag if it does exist.

很抱歉,如果我错过了显而易见,但这对我来说是全新的。这是我用来检查实例是否正在运行的代码的示例。

Sorry if I'm missing the obvious but this is all new to me. Here's an example of the code I'm using to check if an instance is running.

instanceID = "i-myInstanceID"; do { var myrequest = new DescribeInstanceStatusRequest(); DescribeInstanceStatusResponse myresponse = ec2.DescribeInstanceStatus(myrequest); int isCount = myresponse.DescribeInstanceStatusResult.InstanceStatuses.Count; for (int isc=0; isc < isCount; isc++) { InstanceStatus instanceStatus = myresponse.DescribeInstanceStatusResult.InstanceStatuses[isc]; if (instanceStatus.InstanceId.Contains(instanceID)) { Console.WriteLine("It looks like instance "+instanceID+" is running."); idIdx = isc; foundID = true; break; } } if ((foundID==false) && (secondCounter==1)) { Console.Write("Looking for instance "+instanceID); } else { Console.Write("."); } Thread.Sleep(1000); secondCounter++; if (secondCounter > 5) { break; } } while (foundID == false) ;

推荐答案

首先发送DescribeInstancesRequest以获取实例列表:

First send a DescribeInstancesRequest to get the list of Instances:

public DescribeInstancesResult GetInstances(Ec2Key ec2Key) { _logger.Debug("GetInstances Start."); AmazonEC2 ec2 = CreateAmazonEc2Client(ec2Key); var ec2Request = new DescribeInstancesRequest(); DescribeInstancesResponse describeInstancesResponse = ec2.DescribeInstances(ec2Request); DescribeInstancesResult result = describeInstancesResponse.DescribeInstancesResult; _logger.Debug("GetInstances End."); return result; }

然后遍历实例,直到找到所需的实例,然后使用Tag.GetTagValueByKey方法:

Then loop through the instances until you find the one you want, and then use the Tag.GetTagValueByKey method:

// This just calls the above code DescribeInstancesResult ec2Instances = _ec2ResourceAccess.GetInstances(ec2Key); var returnInstances = new List<Ec2UtilityInstance>(); foreach (var reservation in ec2Instances.Reservation) { foreach (var runningInstance in reservation.RunningInstance) { var returnInstance = new Ec2UtilityInstance(); returnInstance.InstanceId = runningInstance.InstanceId; returnInstance.InstanceName = runningInstance.Tag.GetTagValueByKey("Name"); returnInstance.Status = (Ec2UtilityInstanceStatus)Enum.Parse(typeof(Ec2UtilityInstanceStatus), runningInstance.InstanceState.Name, true); returnInstance.DefaultIp = runningInstance.Tag.GetTagValueByKey("DefaultIp"); returnInstance.InstanceType = runningInstance.InstanceType; returnInstance.ImageId = runningInstance.ImageId; returnInstances.Add(returnInstance); } }

以下是全文的链接来自:

Here is the link for full source that this was taken from:

github/escherrer/ EC2Utilities

Common\Manager

Common\Manager

公共\资源访问

更多推荐

使用C#获取ec2

本文发布于:2023-11-24 12:09:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1625147.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!