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

发布于 2024-10-17 01:29:54 字数 100 浏览 1 评论 0原文

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

If I set up a tag on the Amazon website directly I can access it in the runningInstance.Tag property. Is there any way to set it up when I start the instance using the RunInstancesResponse?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半城柳色半声笛 2024-10-24 01:29:54

其中变量 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;
        }

Where the variable ec2 is an instance of 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;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文