- Spring Boot 2.7
- Spring Cloud v2021
- Spring Cloud Function Adapter AWS (from Spring Cloud v2021)
mvn -U clean install
- Please, set up your AWS CLI with Account before deployment
- After, update env.sh file with your account id and AWS Region
./build-and-deploy.sh
Examples
@Component
public class LambdaHandler implements Consumer<ScheduledEvent> {
private final PetRepository petRepository;
public LambdaHandler(PetRepository petRepository) {
this.petRepository = petRepository;
}
@Override
public void accept(ScheduledEvent scheduledEvent) {
log.info(scheduledEvent.getRegion());
log.info(scheduledEvent.getSource());
log.info(scheduledEvent.getTime().toString());
petRepository.save(Pet.builder().build());
}
}
Monitoring
@SpringBootTest
class LambdaHandlerTests {
@MockBean
PetRepository petRepository;
@Autowired
LambdaHandler lambdaHandler;
@Test
void shouldSaveAPetWhenFiring() {
var scheduledEvent = new ScheduledEvent();
scheduledEvent.setTime(DateTime.now());
scheduledEvent.setRegion("us-east-1");
scheduledEvent.setSource("aws.events");
Mockito.doNothing().when(petRepository).save(Mockito.any());
lambdaHandler.accept(scheduledEvent);
Mockito.verify(petRepository, Mockito.times(1)).save(Mockito.any());
}
}