0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWS Lambda で Javaを使う ー実装編Tips - ReagionとInstanceIDからInstance名を取得する

Last updated at Posted at 2020-03-29

##AWS Lambda で Javaを使う 目次
Eclipse準備編
・登録実行編(いつか)
実装編 - EC2を止める/立ち上げる
実装編 - CloudWatchの引数を確認する
実装編Tips - ReagionとInstanceIDからInstance名を取得する

ReagionとInstanceIDからInstance名を取得する

引数にReagionコードとInstanceIDを渡すと、Instance名を返してあげる。
また、二回目以降、同じReagionコードを渡した場合、Mapに格納している値を返却する。

// <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<>();
		// Instance Id と Instance名を取得 (こんな大層なことしなくても取れそうな気もする。。でも、純正のEC2MetadataUtilsでは取れなさそうだった。)
		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);
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?