English 中文(简体)
如何制止在java使用Taga名字的EC2例?
原标题:How to stop EC2 instance using Tag name in java?

I know how to stop an instance with its id, but its very difficult to give EC2 instance id every time. How can I stop the instance with Tag Name

AmazonEC2 ec2 = new AmazonEC2Client(credentials);

List<String> instancesToStop = new ArrayList<String>();
instancesToStop.add("INSTANCE_ID");
StopInstancesRequest stoptr = new StopInstancesRequest();       
stoptr.setInstanceIds(instancesToStop);
ec2.stopInstances(stoptr);

我如何通过<条码>停止审理。 Tag name <>/code>?

问题回答

You can create a help method to get running instance with matching tag:

public List<String> getRunningInstancesByTags(String tagName, String value) {
    List<String> instances = new ArrayList<String>();
    for (Reservation reservation : ec2client.describeInstances().getReservations()) {
        for (Instance instance : reservation.getInstances()) {
            if (!instance.getState().getName().equals(InstanceStateName.Running.toString())) {
                continue;
            }
            for (Tag tag : instance.getTags()) {
                if (tag.getKey().equals(tagName) && tag.getValue().equals(value)) {
                    instances.add(instance.getInstanceId());
                }
            }
        }
    }
    return instances;
}

这种方法仅仅与唯一的对口吻相匹配,就可以加以改进,支持更多的对口。

this is just an extension of @qrtt1 code to support multiple tags :

private static final AWSCredentials AWS_CREDENTIALS = new BasicAWSCredentials("ABCDEF", "MNOPQRSTUVWXYZ");
private static final Map<String, String> ec2Tags = new LinkedHashMap<String, String>();
static {
    //ADD YOUR EC2 TAGS
    ec2Tags.put("stage", "test"); 
    ec2Tags.put("canBeStopped", "true");
}
public static List<Instance> getRunningInstancesByTags(Map<String, String> ec2Tags) {
    AmazonEC2 ec2Client = AmazonEC2ClientBuilder.standard()
                         .withCredentials(new AWSStaticCredentialsProvider(AWS_CREDENTIALS))
                         .withRegion(Regions.EU_CENTRAL_1).build();
    List<Instance> instances = new ArrayList<Instance>();
    Map<String, String> instanceTags = new LinkedHashMap<String, String>();
    for (Reservation reservation : ec2Client.describeInstances().getReservations()) {
        for (Instance instance : reservation.getInstances()) {
            if (!instance.getState().getName().equals(InstanceStateName.Running.toString())) {
                continue;
            }
            instanceTags = instance.getTags().stream()
                       .collect(Collectors.toMap(Tag::getKey, Tag::getValue));
            if (instanceTags.entrySet().containsAll(ec2Tags.entrySet())) {
                logger.info("{}", instanceTags.entrySet().toString());
                instances.add(instance);
            }
        }
    }
    return instances;
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签