Spring boot 2 and AWS DynamoDB health endpoint

Viewed 1925

I am using Spring Boot 2 with DynamoDB. Is it possible to expose health check of dynamoDB on Spring Boot actuator /health endpoint?

Recently I had situation where my application was not able to connect to DynamoDB (exception from underlying connection HTTP pool).

1 Answers

You should be able to do this by defining a custom actuator health indicator in which you perform an dynamoDb operation such as ListTables. Custom indicators are documented here And ListTables here. You should end up with something like:

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class MyHealthIndicator implements HealthIndicator {

@Override
public Health health() {
    int errorCode = check(); // perform some specific health check
    if (errorCode != 0) {
        return Health.down().withDetail("Error Code", errorCode).build();
    }
    return Health.up().build();
}

}

private int check(){
  try{
    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
    ListTablesRequest request = new ListTablesRequest();
    ListTablesResult response = client.listTables(request);
    return 0;
  }catch (Exception e){
     //log exception?
     return -1;
  }
}
Related