I am working on a Spring Batch application that should use aws cloud only for specific profiles. Currently I have a profile that uses aws and another that shouldn't as it runs the application locally with a local database, local files, etc. (Meanwhile the AWS profile will use RDS, S3 ,etc)
For my configuration using the AWS profile I have the following:
@Configuration
@Profile("!localDev")
public class FileReaderConfigAWS {
@Value("${cloud.aws.s3.bucket}")
private String amazonS3Bucket;
@Autowired
private ResourceLoader resourceLoader;
private static final Logger logger = LoggerFactory.getLogger(FileReaderConfigAWS.class);
@Bean
@StepScope
public FlatFileItemReader<Object> flatFileReader(@Value("#{jobParameters['inputFile']}") String inputFile, LineMapper
lineMapper) {
FlatFileItemReader<Object> flatFileItemReader = new FlatFileItemReader<>();
flatFileItemReader.setResource(resourceLoader.getResource("s3://" + this.amazonS3Bucket + "/" + inputFile));
flatFileItemReader.setLineMapper(lineMapper);
return flatFileItemReader;
}
@Bean
public AbstractFileValidator inputFileValidator() {
InputS3Validator inputS3Validator = new InputS3Validator();
inputS3Validator.setRequiredKeys(new String[]{InputFileSystemValidator.INPUT_FILE});
return inputS3Validator;
}
}
For my localDev Profile I have the following:
@Profile("localDev")
@Configuration
public class FileReaderConfigLocalDev {
@Bean
@StepScope
public FlatFileItemReader<Object> flatFileReader(@Value("#{jobParameters['inputFile']}") String inputFile, LineMapper lineMapper) {
FlatFileItemReader<Object> flatFileItemReader = new FlatFileItemReader<>();
flatFileItemReader.setResource(new FileSystemResource(inputFile));
flatFileItemReader.setLineMapper(lineMapper);
return flatFileItemReader;
}
@Bean
public AbstractFileValidator inputFileValidator() {
InputFileSystemValidator inputFileValidator = new InputFileSystemValidator();
inputFileValidator.setRequiredKeys(new String[]{InputFileSystemValidator.INPUT_FILE});
return inputFileValidator;
}
}
When I try to run the Spring Boot Main class using the localDev
profile (-Dspring.profiles.active=localDev
) I get the following error:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'amazonS3': Invocation of init method failed; nested exception is java.lang.IllegalStateException: 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
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
... 18 common frames omitted
Caused by: java.lang.IllegalStateException: 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
at org.springframework.util.Assert.state(Assert.java:70) ~[spring-core-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.cloud.aws.core.region.Ec2MetadataRegionProvider.getRegion(Ec2MetadataRegionProvider.java:39) ~[spring-cloud-aws-core-1.2.1.RELEASE.jar:1.2.1.RELEASE]
at org.springframework.cloud.aws.core.config.AmazonWebserviceClientFactoryBean.createInstance(AmazonWebserviceClientFactoryBean.java:98) ~[spring-cloud-aws-core-1.2.1.RELEASE.jar:1.2.1.RELEASE]
at org.springframework.cloud.aws.core.config.AmazonWebserviceClientFactoryBean.createInstance(AmazonWebserviceClientFactoryBean.java:44) ~[spring-cloud-aws-core-1.2.1.RELEASE.jar:1.2.1.RELEASE]
at org.springframework.beans.factory.config.AbstractFactoryBean.afterPropertiesSet(AbstractFactoryBean.java:134) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
... 25 common frames omitted
As I started debugging I found that once you add spring-cloud-aws-autoconfigure to your pom.xml then there seems to be no way to disable aws autoconfigure for non-aws environments at runtime.
I tried the following:
@EnableAutoConfiguration(exclude = {ContextCredentialsAutoConfiguration.class, ContextStackAutoConfiguration.class})
but still won't work, any ideas anyone?
Thanks in advance!