gpt4 book ai didi

java - 使用JAVA为EC2实例设置标签

转载 作者:行者123 更新时间:2023-11-30 02:20:10 25 4
gpt4 key购买 nike

这是我尝试的最后一个代码:

System.out.println("Manager not exists. Going to create.");
// Basic 32-bit Amazon Linux AMI 1.0 (AMI Id: ami-08728661)
RunInstancesRequest request = new RunInstancesRequest("ami-acd005d5", 1, 1);
request.setInstanceType(InstanceType.T2Micro.toString());
List<Instance> instances = ec2.runInstances(request).getReservation().getInstances();
System.out.println("Launch instances: " + instances);
Instance instance = instances.get(0);

Collection<String> resources = new ArrayList();
resources.add(instance.getInstanceId());
Collection<Tag> tags = new ArrayList();
tags.add(new Tag("Name", "Manager"));
CreateTagsRequest createTagsRequest = new CreateTagsRequest();
createTagsRequest.setResourceId(instance.getInstanceId());
createTagsRequest.setTags(tags);
CreateTagsResult tagsRsults = createTags(createTagsRequest);

它不会在线编译 createTagsRequest.setTags(tags):

The method setTags(java.util.Collection<com.amazonaws.services.workspaces.model.Tag>) in the type CreateTagsRequest is not applicable for the arguments (java.util.Collection<com.amazonaws.services.ec2.model.Tag>)

它让我将“setTags”更改为“withTags”以及相反的情况(循环情况)。尝试了几种方法,查看了AWS文档和Javadoc。

这是我的导入:

    import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.InstanceStateName;
import com.amazonaws.services.ec2.model.InstanceType;
import com.amazonaws.services.ec2.model.Reservation;
import com.amazonaws.services.ec2.model.RunInstancesRequest;
import com.amazonaws.services.ec2.model.Tag;

编辑:当前代码不起作用:

Collection<Tag> tags = new ArrayList<Tag>();
Tag t = new Tag();
t.setKey("Name");
t.setValue("Manager");
tags.add(t);
CreateTagsRequest createTagsRequest = new CreateTagsRequest();
createTagsRequest.withTags(tags);
createTagsRequest.withResourceId(instance.getInstanceId());
ec2.createTags(createTagsRequest);

最佳答案

您似乎导入了 CreateTagsRequest来自com.amazonaws.services.workspaces.model而不是 com.amazonaws.services.ec2.model 。请注意 wokspaces 之间的区别和ec2在中间。

关于java - 使用JAVA为EC2实例设置标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47100653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com