Class EC2MetadataUtils
- java.lang.Object
-
- software.amazon.awssdk.regions.internal.util.EC2MetadataUtils
-
@SdkInternalApi public final class EC2MetadataUtils extends Object
Utility class for retrieving Amazon EC2 instance metadata.Note: this is an internal API subject to change. Users of the SDK should not depend on this.
You can use the data to build more generic AMIs that can be modified by configuration files supplied at launch time. For example, if you run web servers for various small businesses, they can all use the same AMI and retrieve their content from the Amazon S3 bucket you specify at launch. To add a new customer at any time, simply create a bucket for the customer, add their content, and launch your AMI.
If
SdkSystemSetting.AWS_EC2_METADATA_DISABLEDis set to true, EC2 metadata usage will be disabled andSdkClientExceptionwill be thrown for any metadata retrieval attempt.If
SdkSystemSetting.AWS_EC2_METADATA_V1_DISABLEDorProfileProperty.EC2_METADATA_V1_DISABLEDis set to true, data will only be loaded from EC2 metadata service if a token is successfully retrieved - fallback to load data without a token will be disabled.More information about Amazon EC2 Metadata
- See Also:
- Amazon EC2 User Guide: Instance Metadata
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classEC2MetadataUtils.InstanceInfostatic classEC2MetadataUtils.NetworkInterfaceAll of the metada associated with a network interface on the instance.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidclearCache()static StringfetchData(String path, boolean force, int attempts)Fetch data using the given pathstatic StringgetAmiId()Get the AMI ID used to launch the instance.static StringgetAmiLaunchIndex()Get the index of this instance in the reservation.static StringgetAmiManifestPath()Get the manifest path of the AMI with which the instance was launched.static List<String>getAncestorAmiIds()Get the list of AMI IDs of any instances that were rebundled to created this AMI.static StringgetAvailabilityZone()Get the Availability Zone in which the instance launched.static Map<String,String>getBlockDeviceMapping()Get the virtual devices associated with the ami, root, ebs, and swap.static StringgetData(String path)static StringgetData(String path, int tries)static StringgetEC2InstanceRegion()Returns the current region of this running EC2 instance; or null if it is unable to do so.static StringgetInstanceAction()Notifies the instance that it should reboot in preparation for bundling.static StringgetInstanceId()Get the ID of this instance.static EC2MetadataUtils.InstanceInfogetInstanceInfo()Retrieve some of the data from http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html as a typed object.static StringgetInstanceSignature()Get the signature of the instance.static StringgetInstanceType()Get the type of the instance.static List<String>getItems(String path)static List<String>getItems(String path, int tries)static StringgetLocalHostName()Get the local hostname of the instance.static StringgetMacAddress()Get the MAC address of the instance.static List<EC2MetadataUtils.NetworkInterface>getNetworkInterfaces()Get the list of network interfaces on the instance.static StringgetPrivateIpAddress()Get the private IP address of the instance.static List<String>getProductCodes()Get the list of product codes associated with the instance, if any.static StringgetPublicKey()Get the public key.static StringgetRamdiskId()Get the ID of the RAM disk specified at launch time, if applicable.static StringgetReservationId()Get the ID of the reservation.static List<String>getSecurityGroups()Get the list of names of the security groups applied to the instance.static StringgetToken()static StringgetUserData()Get the metadata sent to the instancestatic voidresetIsFallbackDisableResolved()
-
-
-
Method Detail
-
getAmiId
public static String getAmiId()
Get the AMI ID used to launch the instance.
-
getAmiLaunchIndex
public static String getAmiLaunchIndex()
Get the index of this instance in the reservation.
-
getAmiManifestPath
public static String getAmiManifestPath()
Get the manifest path of the AMI with which the instance was launched.
-
getAncestorAmiIds
public static List<String> getAncestorAmiIds()
Get the list of AMI IDs of any instances that were rebundled to created this AMI. Will only exist if the AMI manifest file contained an ancestor-amis key.
-
getInstanceAction
public static String getInstanceAction()
Notifies the instance that it should reboot in preparation for bundling. Valid values: none | shutdown | bundle-pending.
-
getInstanceId
public static String getInstanceId()
Get the ID of this instance.
-
getInstanceType
public static String getInstanceType()
Get the type of the instance.
-
getLocalHostName
public static String getLocalHostName()
Get the local hostname of the instance. In cases where multiple network interfaces are present, this refers to the eth0 device (the device for which device-number is 0).
-
getMacAddress
public static String getMacAddress()
Get the MAC address of the instance. In cases where multiple network interfaces are present, this refers to the eth0 device (the device for which device-number is 0).
-
getPrivateIpAddress
public static String getPrivateIpAddress()
Get the private IP address of the instance. In cases where multiple network interfaces are present, this refers to the eth0 device (the device for which device-number is 0).
-
getAvailabilityZone
public static String getAvailabilityZone()
Get the Availability Zone in which the instance launched.
-
getProductCodes
public static List<String> getProductCodes()
Get the list of product codes associated with the instance, if any.
-
getPublicKey
public static String getPublicKey()
Get the public key. Only available if supplied at instance launch time.
-
getRamdiskId
public static String getRamdiskId()
Get the ID of the RAM disk specified at launch time, if applicable.
-
getReservationId
public static String getReservationId()
Get the ID of the reservation.
-
getSecurityGroups
public static List<String> getSecurityGroups()
Get the list of names of the security groups applied to the instance.
-
getInstanceSignature
public static String getInstanceSignature()
Get the signature of the instance.
-
getEC2InstanceRegion
public static String getEC2InstanceRegion()
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.The instance info is only guaranteed to be a JSON document per http://docs .aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html
-
getBlockDeviceMapping
public static Map<String,String> getBlockDeviceMapping()
Get the virtual devices associated with the ami, root, ebs, and swap.
-
getNetworkInterfaces
public static List<EC2MetadataUtils.NetworkInterface> getNetworkInterfaces()
Get the list of network interfaces on the instance.
-
getUserData
public static String getUserData()
Get the metadata sent to the instance
-
getInstanceInfo
public static EC2MetadataUtils.InstanceInfo getInstanceInfo()
Retrieve some of the data from http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html as a typed object. This entire class will be removed as part of https://github.com/aws/aws-sdk-java-v2/issues/61, so don't rely on this sticking around. This should not be removed until https://github.com/aws/aws-sdk-java-v2/issues/61 is implemented.
-
clearCache
@SdkTestInternalApi public static void clearCache()
-
resetIsFallbackDisableResolved
@SdkTestInternalApi public static void resetIsFallbackDisableResolved()
-
getToken
public static String getToken()
-
fetchData
public static String fetchData(String path, boolean force, int attempts)
Fetch data using the given path- Parameters:
path- the pathforce- whether to force to override the value in the cacheattempts- the number of attempts that should be executed.- Returns:
- the value retrieved from the path
-
-