/** * Get the metadata sent to the instance */ public static String getUserData() { return getData(EC2_USERDATA_ROOT); }
public static String getData(String path) { return getData(path, DEFAULT_QUERY_RETRIES); }
private static String fetchData(String path, boolean force) { try { if (force || !cache.containsKey(path)) cache.put(path, getData(path)); return cache.get(path); } catch (Exception e) { return null; } }
/** * Get information about the last time the instance profile was updated, * including the instance's LastUpdated date, InstanceProfileArn, and * InstanceProfileId. */ public static IAMInfo getIAMInstanceProfileInfo() { String json = getData(EC2_METADATA_ROOT + "/iam/info"); if (null == json) { return null; } try { return mapper.readValue(json, IAMInfo.class); } catch (Exception e) { log.warn("Unable to parse IAM Instance profile info (" + json + "): " + e.getMessage(), e); return null; } }
/** * Returns the current region of this running EC2 instance; or null if * it is unable to do so. The method avoids interpreting other parts of the * instance info JSON document to minimize potential failure. * <p> * The instance info is only guaranteed to be a JSON document per * http://docs * .aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html */ public static String getEC2InstanceRegion() { return doGetEC2InstanceRegion(getData( EC2_DYNAMICDATA_ROOT + INSTANCE_IDENTITY_DOCUMENT)); }
/** * Get the virtual devices associated with the ami, root, ebs, and swap. */ public static Map<String, String> getBlockDeviceMapping() { Map<String, String> blockDeviceMapping = new HashMap<String, String>(); List<String> devices = getItems(EC2_METADATA_ROOT + "/block-device-mapping"); if (devices != null) { for (String device : devices) { blockDeviceMapping.put(device, getData(EC2_METADATA_ROOT + "/block-device-mapping/" + device)); } } return blockDeviceMapping; }
/** * The instance info is only guaranteed to be a JSON document per * http://docs * .aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html * <p> * This method is only a best attempt to capture the instance info as a * typed object. * <p> * Get an InstanceInfo object with dynamic information about this instance. */ public static InstanceInfo getInstanceInfo() { return doGetInstanceInfo(getData( EC2_DYNAMICDATA_ROOT + INSTANCE_IDENTITY_DOCUMENT)); }
private String getData(String key) { if (data.containsKey(key)) return data.get(key); // Since the keys are variable, cache a list of which ones are available to prevent unnecessary trips to the service. if (null == availableKeys) { availableKeys = EC2MetadataUtils.getItems(EC2_METADATA_ROOT + path); } if (availableKeys != null && availableKeys.contains(key)) { data.put(key, EC2MetadataUtils.getData(EC2_METADATA_ROOT + path + key)); return data.get(key); } else { return null; } }
/** * Returns the temporary security credentials (AccessKeyId, SecretAccessKey, * SessionToken, and Expiration) associated with the IAM roles on the * instance. */ public static Map<String, IAMSecurityCredential> getIAMSecurityCredentials() { Map<String, IAMSecurityCredential> credentialsInfoMap = new HashMap<String, IAMSecurityCredential>(); List<String> credentials = getItems(EC2_METADATA_ROOT + "/iam/security-credentials"); if (credentials != null) { for (String credential : credentials) { String json = getData(EC2_METADATA_ROOT + "/iam/security-credentials/" + credential); try { IAMSecurityCredential credentialInfo = mapper.readValue(json, IAMSecurityCredential.class); credentialsInfoMap.put(credential, credentialInfo); } catch (Exception e) { log.warn("Unable to process the credential (" + credential + "). " + e.getMessage(), e); } } } return credentialsInfoMap; }
/** * Get the metadata sent to the instance */ public static String getUserData() { return getData(EC2_USERDATA_ROOT); }
public static String getData(String path) { return getData(path, DEFAULT_QUERY_RETRIES); }
private static String fetchData(String path, boolean force) { try { if (force || !cache.containsKey(path)) cache.put(path, getData(path)); return cache.get(path); } catch (Exception e) { return null; } }
/** * Get information about the last time the instance profile was updated, * including the instance's LastUpdated date, InstanceProfileArn, and * InstanceProfileId. */ public static IAMInfo getIAMInstanceProfileInfo() { String json = getData(EC2_METADATA_ROOT + "/iam/info"); if (null == json) { return null; } try { return mapper.readValue(json, IAMInfo.class); } catch (Exception e) { log.warn("Unable to parse IAM Instance profile info (" + json + "): " + e.getMessage(), e); return null; } }
/** * Returns the current region of this running EC2 instance; or null if * it is unable to do so. The method avoids interpreting other parts of the * instance info JSON document to minimize potential failure. * <p> * The instance info is only guaranteed to be a JSON document per * http://docs * .aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html */ public static String getEC2InstanceRegion() { return doGetEC2InstanceRegion(getData( EC2_DYNAMICDATA_ROOT + INSTANCE_IDENTITY_DOCUMENT)); }
/** * The instance info is only guaranteed to be a JSON document per * http://docs * .aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html * <p> * This method is only a best attempt to capture the instance info as a * typed object. * <p> * Get an InstanceInfo object with dynamic information about this instance. */ public static InstanceInfo getInstanceInfo() { return doGetInstanceInfo(getData( EC2_DYNAMICDATA_ROOT + INSTANCE_IDENTITY_DOCUMENT)); }
/** * Get the virtual devices associated with the ami, root, ebs, and swap. */ public static Map<String, String> getBlockDeviceMapping() { Map<String, String> blockDeviceMapping = new HashMap<String, String>(); List<String> devices = getItems(EC2_METADATA_ROOT + "/block-device-mapping"); if (devices != null) { for (String device : devices) { blockDeviceMapping.put(device, getData(EC2_METADATA_ROOT + "/block-device-mapping/" + device)); } } return blockDeviceMapping; }
private String getData(String key) { if (data.containsKey(key)) return data.get(key); // Since the keys are variable, cache a list of which ones are available to prevent unnecessary trips to the service. if (null == availableKeys) { availableKeys = EC2MetadataUtils.getItems(EC2_METADATA_ROOT + path); } if (availableKeys != null && availableKeys.contains(key)) { data.put(key, EC2MetadataUtils.getData(EC2_METADATA_ROOT + path + key)); return data.get(key); } else { return null; } }
/** * Returns the temporary security credentials (AccessKeyId, SecretAccessKey, * SessionToken, and Expiration) associated with the IAM roles on the * instance. */ public static Map<String, IAMSecurityCredential> getIAMSecurityCredentials() { Map<String, IAMSecurityCredential> credentialsInfoMap = new HashMap<String, IAMSecurityCredential>(); List<String> credentials = getItems(EC2_METADATA_ROOT + "/iam/security-credentials"); if (credentials != null) { for (String credential : credentials) { String json = getData(EC2_METADATA_ROOT + "/iam/security-credentials/" + credential); try { IAMSecurityCredential credentialInfo = mapper.readValue(json, IAMSecurityCredential.class); credentialsInfoMap.put(credential, credentialInfo); } catch (Exception e) { log.warn("Unable to process the credential (" + credential + "). " + e.getMessage(), e); } } } return credentialsInfoMap; }
public static boolean isRunningOnCloudEnvironment() { if (isCloudEnvironment == null) { try { isCloudEnvironment = EC2MetadataUtils.getData(EC2_METADATA_ROOT + "/instance-id", 1) != null; } catch (AmazonClientException e) { isCloudEnvironment = false; } } return isCloudEnvironment; } }
public static boolean isRunningOnCloudEnvironment() { if (isCloudEnvironment == null) { try { isCloudEnvironment = EC2MetadataUtils.getData(EC2_METADATA_ROOT + "/instance-id", 1) != null; } catch (AmazonClientException e) { isCloudEnvironment = false; } } return isCloudEnvironment; } }