如何使用.Net API在EC2实例上创建标记

时间:2011-02-10 23:04:04

标签: .net amazon-ec2

如果我直接在亚马逊网站上设置标签,我可以在runningInstance.Tag属性中访问它。使用RunInstancesResponse启动实例时,有没有办法设置它?

1 个答案:

答案 0 :(得分:4)

变量ec2是Amazon.EC2.AmazonEC2的实例

 public RunInstancesResult RunInstances()
        {
            RunInstancesRequest request;
            RunInstancesResponse response;
            RunInstancesResult result;

            request = new RunInstancesRequest();

            request
                .WithInstanceType(InstanceType)
                .WithImageId(ImageId)
                .WithKeyName(KeyName)
                .WithPlacement(new Placement() {AvailabilityZone = AvailabilityZone})
                .WithMaxCount(1)
                .WithMinCount(1)
                .WithSecurityGroup(SecurityGroups);

            response = ec2.RunInstances(request);

            InstanceId = response.RunInstancesResult.Reservation.RunningInstance[0].InstanceId;

            var trequest = new CreateTagsRequest();

            trequest.WithResourceId(InstanceId);
            trequest.WithTag(new Tag[] {
                    new Tag { Key = "#Name", Value = ServerTag },
                    new Tag { Key = "Hosts", Value = "prod data restored"}
            });

            var tresponse = ec2.CreateTags(trequest);

            return response.RunInstancesResult;
        }