Using Java with AWS Lambda-Implementation Tips-Get Instance Name from Reagion and Instance ID

Using Java with AWS Lambda Table of Contents

Eclipse Preparation ・ Registration execution (someday) -Implementation-Stop / Start EC2 -Implementation-Check CloudWatch arguments -Implementation Tips --Get Instance Name from Reagion and Instance ID

Get the Instance name from Reagion and InstanceID

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

Using Java with AWS Lambda-Implementation Tips-Get Instance Name from Reagion and Instance ID
Search and execute method by name from instance with processing (java)
Using Java with AWS Lambda-Eclipse Preparation
Using Java with AWS Lambda-Implementation-Check CloudWatch Arguments
Using Java with AWS Lambda-Implementation-Stop / Launch EC2
[Java] Development with multiple files using package and import
Find the address class and address type from the IP address with Java
Log aggregation and analysis (working with AWS Athena in Java)
Problems with Dijkstra's algorithm using PriorityQueue and adjacency list (java)
Socket communication with a web browser using Java and JavaScript ②
Socket communication with a web browser using Java and JavaScript ①
AWS Elastic Beanstalk # 1 with Java starting from scratch-Building a Java web application environment using the EB CLI-
Java, instance starting from beginner
Using Mapper with Java (Spring)
Using Docker from Java Gradle
Sample of using Salesforce's Bulk API from Java client with PK-chunking
Read barometric pressure and temperature with Java from Raspberry Pi 3 & BMP180