使用C#获取ec2-instance标签

时间:2014-07-09 20:19:13

标签: c# sdk amazon-ec2

我不是开发人员,所以也许答案是针对不同的解决方案,但我无法将其从python或其他内容翻译出来。

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

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

            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) ;

1 个答案:

答案 0 :(得分:2)

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

    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方法:

        // 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);   
            }
        }

这是完整来源的链接,取自:

https://github.com/escherrer/EC2Utilities

普通\管理器

普通\ ResourceAccess