title: Java API Health check with Spring Boot for Elasticsearch/AWS
body: <p>I'm an software engineering intern at my company where we use AWS (Amazon Web Services), EC2/ECS/Fargate/NLB and I was tasked with adding a Health check monitor for our Elasticsearch service to our backend API that we have written in Java w/ Spring Boot/MVC/Maven. I followed the guide here to use Springs actuator for the health check monitor and it was fairly straight forward:
  <a href="https://www.amitph.com/custom-health-check-spring-boot-actuator/" rel="nofollow noreferrer">https://www.amitph.com/custom-health-check-spring-boot-actuator/</a></p>
  
  <p>All you have to do is implement the HealthIndicator interface in SpringBoot, here was my IndexExists class that I created with the basic code:</p>
  
  <pre><code>@Component
  public class IndexExists implements HealthIndicator {
      private final ESClient esClient;
      private static final Logger LOGGER = LoggerFactory.getLogger(IndexExists.class);
  
  
      @Autowired
      public IndexExists(ESClient esClient) {
          this.esClient = esClient;
      }
  
      @Override
      public Health health() {
          try {
              if (!esClient.hasIndex(Indices.INDEX_ASSETS)) {
                  return Health.down().withDetail("index", Indices.INDEX_ASSETS + " index does not exist").build();
              }
          } catch (IOException e) {
              LOGGER.error("Error checking if Elasticsearch index {} exists , with exception", Indices.INDEX_ASSETS,  e);
          }
          return Health.up().withDetail("index", Indices.INDEX_ASSETS + " exists").build();
      }
  }
  </code></pre>
  
  <p>Furthermore, I added <strong>rest Uris</strong> for localhost to test and for dev environment. I used postman to test the API actuator/health endpoint and it worked <strong>locally</strong>. This configuration code is setup in our application.yml file and here is a screenshot:
  <a href="https://i.stack.imgur.com/BPVNo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BPVNo.png" alt="enter image description here"></a></p>
  
  <p>However, once I deployed the build to dev, it failed and I was trying to debug. Another engineer had me to go the AWS logs and there are a bunch of "connection refused" messages. The engineer told me he thinks its either the formatting of the uri (should the url have a https:// prefix???) in the application.yml file (screenshot above) or something to do with security/privileges causing the connection refused. Here a portion of the AWS logs, it basically causes an infinite loop because the health check fails and Fargate/LB/ECS tries to make more containers because it keeps failing (the engineer said something like that, I'm still learning AWS so sorry if I butchered that):
  <a href="https://i.stack.imgur.com/xvaKu.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xvaKu.jpg" alt="enter image description here"></a></p>
  
  <p>I know it's a long shot but I was hoping someone with AWS/Elasticsearch experience maybe setup a Health monitor endpoint before and ran into an issue like this.... It anyone needs more info from me, I'm glad to help. This is my first User Story at my internship and I don't want to fail. I already have all the code written and it seems sound but I'm running into this networking/connection issue with AWS unless I have a trivial error in my config setup (application.yml) that I am missing.</p>