Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GetSpotInstancesAction to retrieve action from spot instances like on demand instances #5142

Open
1 of 2 tasks
RayaneABDI opened this issue Apr 24, 2024 · 1 comment
Open
1 of 2 tasks
Assignees
Labels
closing-soon This issue will close in 4 days unless further comments are made. feature-request A feature should be added or improved.

Comments

@RayaneABDI
Copy link

RayaneABDI commented Apr 24, 2024

Describe the feature

Currently, the aws-sdk-java includes the method getInstanceAction(), which retrieves data from the URL "/latest/meta-data/instance-action". However, to access the action of a spot instance, we must retrieve it from the URL "/latest/meta-data/spot/instance-action".

Use Case

I aim to determine if AWS will claim my spot instance, and when, adjusting my calculations accordingly. I prefer not to utilize the AWS CLI since I'm already employing the AWS SDK for Java.

Proposed Solution

diff --git a/core/regions/src/main/java/software/amazon/awssdk/regions/internal/util/EC2MetadataUtils.java b/core/regions/src/main/java/software/amazon/awssdk/regions/internal/util/EC2MetadataUtils.java
index 83c41052da9..d2746f18b94 100644
--- a/core/regions/src/main/java/software/amazon/awssdk/regions/internal/util/EC2MetadataUtils.java
+++ b/core/regions/src/main/java/software/amazon/awssdk/regions/internal/util/EC2MetadataUtils.java
@@ -80,6 +80,7 @@ public final class EC2MetadataUtils {
     private static final String EC2_METADATA_ROOT = "/latest/meta-data";
     private static final String EC2_USERDATA_ROOT = "/latest/user-data/";
     private static final String EC2_DYNAMICDATA_ROOT = "/latest/dynamic/";
+    private static final String EC2_SPOT_METADATA_ROOT = "/latest/meta-data/spot";

     private static final String EC2_METADATA_TOKEN_HEADER = "x-aws-ec2-metadata-token";

@@ -139,6 +140,12 @@ public final class EC2MetadataUtils {
         return fetchData(EC2_METADATA_ROOT + "/instance-action");
     }

+    /**
+     * Notifies the instance that it will be stopped.
+     * Valid values: none | stop | terminate.
+     */
+    public static String getSpotInstanceAction() { return fetchData(EC2_SPOT_METADATA_ROOT + "/instance-action"); }
+
     /**
      * Get the ID of this instance.
      */

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

AWS Java SDK version used

2.25.17

JDK version used

openjdk version "17.0.8.1"

Operating System and version

Linux

@RayaneABDI RayaneABDI added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Apr 24, 2024
@debora-ito debora-ito added needs-review This issue or PR needs review from the team. and removed needs-triage This issue or PR still needs to be triaged. labels Apr 30, 2024
@debora-ito
Copy link
Member

@RayaneABDI you can use the Ec2MetadataClient to retrieve the instance metadata you need:
https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/examples-ec2-IMDS.html

Note that EC2MetadataUtils is an internal API class, you should not depend on it because it can introduce breaking changes.

@debora-ito debora-ito removed the needs-review This issue or PR needs review from the team. label May 10, 2024
@debora-ito debora-ito self-assigned this May 10, 2024
@debora-ito debora-ito added the closing-soon This issue will close in 4 days unless further comments are made. label May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closing-soon This issue will close in 4 days unless further comments are made. feature-request A feature should be added or improved.
Projects
None yet
Development

No branches or pull requests

2 participants