・ Eclipse Preparation ・ Registration execution (someday) -Implementation-Stop / Start EC2 -Implementation-Check CloudWatch arguments -Implementation Tips --Get Instance Name from Reagion and Instance ID
If you pass the Reagion code and Instance ID as arguments, the Instance name will be returned. Also, if the same Reagion code is passed from the second time onward, the value stored in Map will be returned.
// <reagion id <<incetance id, incetance key name>>
Map<String, Map<String, String>> resolveInstance = new HashMap<>();
private String getEC2InstanceKeyName(String targetReagion, String targetInstanceId) {
if (!resolveInstance.containsKey(targetReagion)) {
// <incetance id, incetance key name>
Map<String, String> incetanceMap = new HashMap<>();
//Get Instance Id and Instance Name(I feel like I can get it without doing such a big thing. .. However, it didn't seem to be possible with genuine EC2 Metadata Utils.)
AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withRegion(targetReagion).build();
DescribeInstancesResult ec2Info = ec2.describeInstances();
for(Reservation res : ec2Info.getReservations()) {
for(Instance ins : res.getInstances()) {
incetanceMap.put(ins.getInstanceId(), ins.getKeyName());
}
}
resolveInstance.put(targetReagion, incetanceMap);
}
return resolveInstance.get(targetReagion).get(targetInstanceId);
}
Recommended Posts