我正在尝试使用AWS SDK获取AWS中所有EC2实例的实例ID。
AmazonEC2 ec2Service = null;
String aWSAccessId = "XXXXXXXXXXXXXXXXXXXX";
String aWSSecretKey = "XXXXXXXXXXXXXXXXXXXXX";
if (null != aWSAccessId && !"".equals(aWSAccessId)
&& null != aWSSecretKey && !"".equals(aWSSecretKey)) {
ec2Service = new AmazonEC2Client(new BasicAWSCredentials(aWSAccessId, aWSSecretKey));
}
DescribeInstancesResult result = ec2Service.describeInstances();
String instanceid = EC2MetadataUtils.getInstanceId();
String availabilityZone = EC2MetadataUtils.getAvailabilityZone();
System.out.println("availabilityZone-----> "+availabilityZone);
System.out.println("instanceid-----> "+instanceid);
List<Reservation> reservations = result.getReservations();
System.out.println("reservations-----> "+reservations.toString());
for (Reservation reservation : reservations)
{
List<Instance> instances = reservation.getInstances();
System.out.println("instances-----> "+instances.size());
for (Instance instance : instances)
{
System.out.println("Instances----> "+instance.getInstanceId());
}
}
但是我无法获取实例。我还尝试使用EC2MetadataUtils.java通过在我的eclipse ide中配置sdk来获取详细信息。
EC2MetadataUtils ec2Meta = new EC2MetadataUtils();
String amiId = ec2Meta.getAmiId();
System.out.println("Ec2 Metadata ----amiId---> "+amiId);
String availabilityZone = ec2Meta.getAvailabilityZone();
System.out.println("Ec2 Metadata ----AvailabilityZone---> "+availabilityZone);
/*String eC2InstanceRegion = ec2Meta.getEC2InstanceRegion();
System.out.println("Ec2 Metadata ----eC2InstanceRegion---> "+eC2InstanceRegion);*/
String instanceId = ec2Meta.getInstanceId();
System.out.println("Ec2 Metadata ----instanceId---> "+instanceId);
InstanceInfo instanceInfo = ec2Meta.getInstanceInfo();
System.out.println("Ec2 Metadata "+instanceInfo.getAccountId());
System.out.println("Ec2 Metadata "+instanceInfo.getInstanceId());
System.out.println("Ec2 Metadata "+instanceInfo.getPrivateIp());
System.out.println("Ec2 Metadata "+instanceInfo.getRegion());
System.out.println("Ec2 Metadata "+instanceInfo.getArchitecture());
System.out.println("Ec2 Metadata "+instanceInfo.getInstanceType());
System.out.println("Ec2 Metadata "+instanceInfo.getVersion());
System.out.println("Ec2 Metadata "+instanceInfo.getImageId());
String publicKey = EC2MetadataUtils.getPublicKey();
System.out.println("Ec2 Metadata "+publicKey);
IAMInfo AMInstanceProfileInfo = EC2MetadataUtils.getIAMInstanceProfileInfo();
System.out.println("Ec2 Metadata "+AMInstanceProfileInfo.lastUpdated);
String MacAddress = EC2MetadataUtils.getMacAddress();
System.out.println("Ec2 Metadata "+MacAddress);
但是我只对初始字段使用null,最后得到以下错误。
Ec2 Metadata null
Ec2 Metadata null
Ec2 Metadata null
Exception in thread "main" com.amazonaws.SdkClientException: Unable to contact EC2 metadata service.
答案 0 :(得分:0)
Anthony在他的评论中正确地提到EC2元数据服务仅在您正在运行的EC2实例中提供。我有类似的情况,我需要与AWS账户关联的所有EC2实例的区域信息。这就是我如何读取实例的区域信息:
List<Instance> ec2Instances = describeInstancesResult.getReservations().stream().map(d -> d.getInstances()).flatMap(l -> l.stream()).collect(Collectors.toList());
//describeInstancesResult is the response of describeInstance operation on the aws client generated for a given aws account.
ec2Instances.stream().forEach((instance) -> {
String region = instance.getPlacement().getAvailabilityZone();
//do operations on the region value.
}