I have made a very simple maven project using Spring Boot. I am trying to connect with AWS SES using Spring cloud. While running the project, I am getting following error:
No valid instance id defined
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cloud.aws.core.env.ResourceIdResolver.BEAN_NAME': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stackResourceRegistryFactoryBean' defined in class path resource [org/springframework/cloud/aws/autoconfigure/context/ContextStackAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.aws.core.env.stack.config.StackResourceRegistryFactoryBean]: Factory method 'stackResourceRegistryFactoryBean' threw exception; nested exception is java.lang.IllegalArgumentException: No valid instance id defined
I am showing snippets of files in use:
pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-aws</artifactId> <version>2.1.0.RELEASE</version> </dependency> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-ses</artifactId> <version>1.11.505</version> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> </dependencies>
SimpleMailAutoConfig.java
@Configuration public class SimpleMailAutoConfig { @Bean public AmazonSimpleEmailService amazonSimpleEmailService(AWSCredentialsProvider credentialsProvider) { return AmazonSimpleEmailServiceClientBuilder.standard() .withCredentials(credentialsProvider) .withRegion(Regions.US_EAST_1).build(); } @Bean public MailSender mailSender(AmazonSimpleEmailService ses) { return new SimpleEmailServiceMailSender(ses); } }
MailSendingService.java
@Service public class MailSendingService { @Autowired private MailSender mailSender; public void sendMailMessage() { SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); simpleMailMessage.setFrom("[email protected]"); simpleMailMessage.setTo("[email protected]"); simpleMailMessage.setSubject("test subject"); simpleMailMessage.setText("test content"); this.mailSender.send(simpleMailMessage); } }
Application.java
@SpringBootApplication @ComponentScan("com.example") public class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } }
application.properties
cloud.aws.credentials.accessKey=${MyAccessKey} cloud.aws.credentials.secretKey=${MySecretKey} cloud.aws.region.static=us-east-1
I am not trying to connect to any EC2 instance. Not able to find any proper documentation for using spring cloud for SES
Spring Cloud for AWS can automatically detect this based on your environment or stack once you enable the Spring Boot property cloud.
Spring boot is a java based framework to work con auto-configuration in Web Application. Spring cloud is part of Spring boot, where Spring boot is Stand Alone, App – Centric Application framework. The main purpose of Spring Cloud maintains the traffic of the network.
Go to https://aws.amazon.com/console/ and login to the AWS Console. Navigate to IAM section->Dashboard->Manage Security Credentials → AccessKeys Tab and extract your Access Key ID and Secret Access Key. Go ahead and Create on if you don't have one. After jotting down the keys, let's install AWS CLI v2 on your system.
Spring Cloud is just a set of tools (software) commonly used in the cloud, AWS is one of many cloud options, a place where you can deploy your apps.
You've almost got it, you're just missing one more configuration flag to stop that exception creeping in.
Add cloud.aws.stack.auto = false
to your application.properties file thus it will not enable the automatic stack name detection for the application.
You can read up more about it in the docs: http://cloud.spring.io/spring-cloud-aws/spring-cloud-aws.html#_cloudformation_configuration_in_spring_boot
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With