46

I am getting the below error when I am trying to run a Spring boot-AWS application locally :

There is not EC2 meta data available, because the application is not running in the EC2 environment. Region detection is only possible if the application is running on a EC2 instance

My aws-config.xml looks like below :

<aws-context:context-credentials>
<aws-context:simple-credentials access-key="*****" secret-key="*****"/>
</aws-context:context-credentials>  
<aws-context:context-region auto-detect="false" region="ap-south-1" />  
<aws-context:context-resource-loader/>  
<aws-messaging:annotation-driven-queue-listener max-number-of-messages="10" wait-time-out="20" visibility-timeout="3600"/> 

I am trying to listen with a SQSListner in the below class :

@Configuration
@EnableSqs
@ImportResource("classpath:/aws-config.xml")
@EnableRdsInstance(databaseName = "******", 
               dbInstanceIdentifier = "*****", 
               password = "******")
public class AwsResourceConfig {
    @SqsListener(value = "souviksqs", deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
    public void receiveNewFileUpload(S3EventNotification event) {
        try {
            if ( event != null && !CollectionUtils.isNullOrEmpty( event.getRecords() ) && event.getRecords().get( 0 ) != null ) {
                S3Entity entry = event.getRecords().get(0).getS3();
                System.out.println("############ File Uploaded to ###################### " + entry.getBucket().getName() + "/" + entry.getObject().getKey());
            }
        } catch (Exception e) {
            System.out.println("Error reading the SQS message " + e);
            
        }
    }
}

Edit : Just noticed that the error comes when I include the following aws-messaging maven dependency :

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-aws-messaging</artifactId>
     <version>${spring-cloud-aws-version}</version>
</dependency>

I am using spring-cloud-aws-version - 1.2.1.RELEASE

HK boy
  • 1,398
  • 11
  • 17
  • 25
souvikc
  • 991
  • 1
  • 10
  • 25

8 Answers8

85

I was using springframework.cloud.aws.autoconfigure, got the same problem. The reason behind it is, we need to configure region manually when we run application in NON AWS ENVIRONMENT, ie. Local. So put this property in your application-local.properties and you should be good.

cloud.aws.region.static=us-east-1
Markus
  • 1,909
  • 4
  • 26
  • 54
Shubham Pandey
  • 1,788
  • 2
  • 18
  • 24
  • 1
    Wow...Waisted a whole Sunday afternoon trying to fix this one. Wrote an application using AWS DynamoDB and the application kept failing. Your "cloud.aws.region.static=us-east-1 was a great find. I implemented as you highlighted and my application ran fine. Now time for dinner. – skmansfield Dec 17 '17 at 22:54
  • That is a lifesaver! Is there any way to turn it off entirely during local development? – delucasvb Jun 06 '18 at 13:48
  • @delucasvb , I didn't get your point of turning it off. Its a property which AWS expects. I used to get this problem on my windows machine, when I shifted to Amazon Workspace, this problem went away automatically and I no longer need to set this property in application-local.properties. – Shubham Pandey Jun 07 '18 at 07:48
  • I'm looking for a way to turn off Cloud AWS completely during testing for instance. When I put the property in my test.properties, it isn't picked up by Spring – delucasvb Jun 07 '18 at 16:00
  • 13
    As a follow up, for anyone fixing this issue they might encounter some additional issues associated to this same reason. You will also need to set on your application.properties the following: cloud.aws.stack.auto = false – Felipe Plazas Apr 25 '19 at 01:11
  • 1
    doesnt work with `application.yml` ? only works with application.properties. even when formatted in the proper yaml settings. – Robbo_UK Jun 05 '19 at 10:47
  • Concerning the ability to place into application.yml or .properties, what happens is that spring scans for classpath:/application.yml (and classpath:/config/application.yml and others) but does not glom multiple results together. Someplace you already had an application.yml getting picked up by spring, while application.properties was open to you. – DanielKWinsor Oct 22 '20 at 23:25
24

Found the Issue. I was using spring-cloud-starter-aws-messaging for SQS messaging. The above dependency includes many Auto Detect classes which eventually was firing up even if they were not required.

Instead I have used spring-cloud-aws-messaging which solved the issue along with many other auto detect issues.

souvikc
  • 991
  • 1
  • 10
  • 25
11

If using application.yml I did it with the following

spring:
  application:
    name: App Name
  autoconfigure:
    exclude:
      - org.springframework.cloud.aws.autoconfigure.messaging.MessagingAutoConfiguration
      - org.springframework.cloud.aws.autoconfigure.context.ContextStackAutoConfiguration
      - org.springframework.cloud.aws.autoconfigure.context.ContextRegionProviderAutoConfiguration
Robbo_UK
  • 11,351
  • 25
  • 81
  • 117
11

adding these properties to my application.properties in spring boot proj helped me.

cloud.aws.region.static=us-west-2
cloud.aws.region.auto=false
cloud.aws.stack.auto=false
Naga
  • 123
  • 1
  • 9
3

I had the same issue and I was able to prevent spring cloud aws from auto configuring the region by adding this exclude on the Spring configuration.

    @SpringBootApplication(exclude = ContextRegionProviderAutoConfiguration.class)
theINtoy
  • 3,388
  • 2
  • 37
  • 60
priortd
  • 49
  • 3
  • 3
    This was not enough for me. I had to use: @SpringBootApplication( exclude = {ContextRegionProviderAutoConfiguration.class, ContextStackAutoConfiguration.class, MessagingAutoConfiguration.class}) public class Application extends SpringBootServletInitializer { – theINtoy Nov 25 '19 at 14:44
2

I also faced the same issue but it did not get resolved after adding the aws region property. I was able to resolved the same when I removed spring-cloud-starter-aws dependency from my pom

Titans
  • 61
  • 6
2

Using the information from souvikc and an answer from another question, I came up with (I'll take the hard coded region out eventually but just elated it finally works!):


@Configuration
@EnableContextInstanceData
@EnableSqs
@Profile("!local")
@Slf4j
public class AwsEc2Config {

    @Bean
    public RegionProvider regionProvider() {
        return new StaticRegionProvider("eu-west-1");
    }

    @Bean
    public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory(AmazonSQSAsync amazonSQS) {
        SimpleMessageListenerContainerFactory factory = new SimpleMessageListenerContainerFactory();
        factory.setAmazonSqs(amazonSQS);
        factory.setMaxNumberOfMessages(10);
        factory.setAutoStartup(true);
        factory.setWaitTimeOut(20);

        return factory;
    }
}

Sled
  • 18,541
  • 27
  • 119
  • 168
theINtoy
  • 3,388
  • 2
  • 37
  • 60
0

using the response of @Shubham-Pandey you can add a VM option to run your app locally :

-Dcloud.aws.region.static=us-east-1

Dharman
  • 30,962
  • 25
  • 85
  • 135
Michaël COLL
  • 1,153
  • 13
  • 18