使用C#获取ec2-instance标签 [英] using C# to get an ec2-instance tag

查看:113
本文介绍了使用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:

https://github.com/escherrer/ EC2Utilities

Common\Manager

Common\Manager

公共\资源访问

这篇关于使用C#获取ec2-instance标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆