Disable Spring Cloud AWS autoconfiguration for local development
Asked Answered
R

3

25

I use the following Maven dependency which autoconfigures all necessary parameters to make my project work on AWS:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-aws</artifactId>
    <version>1.2.2.RELEASE</version>
</dependency>

I don't have any critical functionality depending on AWS though, it's just to load a few files from S3 at runtime. So during local development (and also testing), I don't need any AWS autoconfiguration.

The logical error I get when running locally is:

...
Caused by: java.lang.IllegalStateException: There is no 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
    at org.springframework.util.Assert.state(Assert.java:392) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.cloud.aws.core.region.Ec2MetadataRegionProvider.getRegion(Ec2MetadataRegionProvider.java:39) ~[spring-cloud-aws-core-1.2.2.RELEASE.jar:1.2.2.RELEASE]
    at org.springframework.cloud.aws.core.config.AmazonWebserviceClientFactoryBean.createInstance(AmazonWebserviceClientFactoryBean.java:98) ~[spring-cloud-aws-core-1.2.2.RELEASE.jar:1.2.2.RELEASE]
    at org.springframework.cloud.aws.core.config.AmazonWebserviceClientFactoryBean.createInstance(AmazonWebserviceClientFactoryBean.java:44) ~[spring-cloud-aws-core-1.2.2.RELEASE.jar:1.2.2.RELEASE]
...

Is there a clean, working solution for both testing and local development?

Reason answered 8/6, 2018 at 15:19 Comment(0)
R
25

I've solved this for tests using the surefire plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.21.0</version>
    <configuration>
        <classpathDependencyExcludes>
            <classpathDependencyExcludes>org.springframework.cloud:spring-cloud-aws-autoconfigure</classpathDependencyExcludes>
        </classpathDependencyExcludes>
    </configuration>
</plugin>

Local development was solved setting the following variables as VM parameters or in the Spring Cloud Config Server:

cloud.aws.region.auto=false
cloud.aws.region.static=us-east-1

You can use any value for cloud.aws.region.static, but there has to be one.

I read both solutions in different places and thought it might help someone in the future to see them combined here.

Reason answered 8/6, 2018 at 15:19 Comment(1)
I was wondering if there was a solution based on spring profiles. For instances I have disabled the spring security for a specific profile by adding this: spring: autoconfigure: exclude: org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfigurationIchang
M
2

This error occurs when you run an application locally with aws cloud autoconfig enabled

For me, following settings helped:

spring:
    autoconfigure:
        exclude:
            - org.springframework.cloud.aws.autoconfigure.context.ContextInstanceDataAutoConfiguration
            - org.springframework.cloud.aws.autoconfigure.context.ContextStackAutoConfiguration
            - org.springframework.cloud.aws.autoconfigure.context.ContextRegionProviderAutoConfiguration

cloud:
    aws:
        region:
            static: ap-south-1
        stack:
            auto: false

You can learn more about it here: Doc

Montanez answered 12/11, 2022 at 1:33 Comment(0)
I
1

An alternative solution to disabling it, would be to use testcontainers:

package com.example;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.localstack.LocalStackContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import static org.testcontainers.containers.localstack.LocalStackContainer.Service.S3;
import static org.testcontainers.containers.localstack.LocalStackContainer.Service.SQS;

@SpringBootTest
@Testcontainers
class ApplicationIT {

    @Container
    static final LocalStackContainer localStack = new LocalStackContainer(DockerImageName.parse("localstack/localstack:3.0.2"))
            .withServices(SQS, S3);

    @DynamicPropertySource
    static void overrideProperties(DynamicPropertyRegistry registry) {
        registry.add("spring.cloud.aws.region.static", localStack::getRegion);
        registry.add("spring.cloud.aws.credentials.access-key", localStack::getAccessKey);
        registry.add("spring.cloud.aws.credentials.secret-key", localStack::getSecretKey);
        registry.add("spring.cloud.aws.sqs.endpoint", () -> localStack.getEndpointOverride(SQS).toString());
        registry.add("spring.cloud.aws.s3.endpoint", () -> localStack.getEndpointOverride(S3).toString());
    }

    @Test
    void contextLoads() {
    }
}
Idonah answered 6/2 at 17:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.