I am working on a Java / Spring Boot / Spring application which was using Version 1 of the AWS Java SDK and version 2.4.4 of io.awspring.cloud.
This application is using the class io.awspring.cloud.core.env.ec2.AmazonEc2InstanceDataPropertySource
like this, to get the AMI ID for the EC2 instance:
var amazonEc2InstanceDataPropertySource = new AmazonEc2InstanceDataPropertySource("xxx");
String amiId = getInstanceDataProperty(amazonEc2InstanceDataPropertySource,"ami-id", "N/A");
..and so on for other properties.
I have now upgraded this application to version 2 of the AWS Java SDK and version 3.0.2 of io.awspring.cloud
. In the new version of io.awspring.cloud
there is no class called AmazonEc2InstanceDataPropertySource
.
How can I rewrite the existing code to work in the new version of this library to get EC2 instance metadata?
The
AmazonEc2InstanceDataPropertySource
class fetches EC2 instance metadata properties via the IMDS (Instance Metadata Service), for the current EC2 instance.It seems to have been replaced with
@EnableContextInstanceData
(not needed in Spring Boot with the right configuration) & field injection usingValue
annotations.In your case, as you're looking to get the AMI ID and other properties, inject these properties directly using the @Value annotation.