spring-cloud-starter-aws uses spring-cloud-aws-autoconfigure which provides the following AWS related configurations:
org.springframework.cloud.aws.autoconfigure.context.ContextInstanceDataAutoConfiguration
org.springframework.cloud.aws.autoconfigure.context.ContextCredentialsAutoConfiguration
org.springframework.cloud.aws.autoconfigure.context.ContextRegionProviderAutoConfiguration
org.springframework.cloud.aws.autoconfigure.context.ContextResourceLoaderAutoConfiguration
org.springframework.cloud.aws.autoconfigure.context.ContextStackAutoConfiguration
org.springframework.cloud.aws.autoconfigure.mail.MailSenderAutoConfiguration
org.springframework.cloud.aws.autoconfigure.cache.ElastiCacheAutoConfiguration
org.springframework.cloud.aws.autoconfigure.messaging.MessagingAutoConfiguration
org.springframework.cloud.aws.autoconfigure.jdbc.AmazonRdsDatabaseAutoConfiguration
org.springframework.cloud.aws.autoconfigure.metrics.CloudWatchExportAutoConfiguration
All of them are triggered by EnableAutoConfiguration. If you can live without EnableAutoConfiguration your problem is solved. However, since you are using spring-cloud-starter-aws, I do not expect that this is the case.
Unfortunatelly, only ContextInstanceDataAutoConfiguration and ContextInstanceDataAutoConfiguration employs @ConditionalOnAwsCloudEnvironment, the remaining configurations start regardless of the environment. Most of them are conditioned only with something similar to the following (this example is from ContextCredentialsAutoConfiguration):
@ConditionalOnClass(name = {"com.amazonaws.auth.AWSCredentialsProvider"})
To not start such configuration you need to remove the com.amazonaws.auth.AWSCredentialsProvider class from classpath. Which can be accomplished with maven profiles or fancy modularisation, but I believe that it is usually a suboptimal solution as there are significant side efects of such project restructuration.
I believe that the simplest solution is to remove spring-cloud-aws-autoconfigure from project dependencies and write own versions of the configuration class. Here is a ContextCredentialsAutoConfiguration version that worked for me:
import mu.KLogging
import org.springframework.beans.factory.support.BeanDefinitionRegistry
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
import org.springframework.cloud.aws.context.config.annotation.ContextDefaultConfigurationRegistrar
import org.springframework.cloud.aws.context.config.support.ContextConfigurationUtils
import org.springframework.context.EnvironmentAware
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar
import org.springframework.context.annotation.Profile
import org.springframework.core.env.Environment
import org.springframework.core.type.AnnotationMetadata
@Configuration
@Import(ContextDefaultConfigurationRegistrar::class, AwsContextCredentialsAutoConfiguration.Registrar::class)
@ConditionalOnClass(name = ["com.amazonaws.auth.AWSCredentialsProvider"])
@Profile("!it")
class AwsContextCredentialsAutoConfiguration {
class Registrar : ImportBeanDefinitionRegistrar, EnvironmentAware {
companion object: KLogging()
private var environment: Environment? = null
override fun setEnvironment(environment: Environment) {
this.environment = environment
}
override fun registerBeanDefinitions(importingClassMetadata: AnnotationMetadata, registry: BeanDefinitionRegistry) {
val useDefaultCredentialsChain = this.environment!!.getProperty("cloud.aws.credentials.useDefaultAwsCredentialsChain", Boolean::class.java, false)
if (useDefaultCredentialsChain) {
logger.debug("Using default AWS credentials provider")
ContextConfigurationUtils.registerDefaultAWSCredentialsProvider(registry)
} else {
logger.debug("Using custom credentials provider (based on environment properties)")
ContextConfigurationUtils.registerCredentialsProvider(
registry,
this.environment!!.getProperty("cloud.aws.credentials.accessKey"),
this.environment!!.getProperty("cloud.aws.credentials.secretKey"),
this.environment!!.getProperty("cloud.aws.credentials.instanceProfile", Boolean::class.java, true) as Boolean && !this.environment!!.containsProperty("cloud.aws.credentials.accessKey"),
this.environment!!.getProperty("cloud.aws.credentials.profileName", "default"),
this.environment!!.getProperty("cloud.aws.credentials.profilePath"))
}
}
}
}
Please note the @Profile("!it") annotation, it is the profile enabled for integration tests. As a consequence, the configuration is ignored during the integration tests, but you can replace the it with the profile you are using for your local context.