AWS target group reports unhealthy, but application is healthy
I have an IIS website running in AWS on an EC2 instance, running on http://localhost:8088. It is behind an ELB that sends all HTTPS traffic to the target group that the EC2 instance is running in, so any request to http://my-dns-name gets rerouted by the ELB to https://my-dns-name, and then on to the target group. I have a health check defined on the target group that checks the /healthcheck
endpoint for a 200 OK response, and the endpoint is configured to allow unauthenticated requests (aka anonymous).
The application itself is running fine; however, AWS is reporting the target group as unhealthy as it is receiving a response code of 302 from the health check endpoint. If I query the endpoint myself directly from my desktop e.g. via PowerShell's Invoke-WebRequest
then I get the expected 200 OK
response. If I remote into the EC2 instance and run the same query then it also returns the expected 200 OK
response.
$> Invoke-WebRequest -Uri https://my-dns-name/healthcheck -Method GET
StatusCode : 200
StatusDescription : OK
Content : {}
RawContent : HTTP/1.1 200 OK
But if I change it to use the local address of http://localhost:8088
, then the query fails:
$> Invoke-WebRequest -Uri http://localhost:8088/healthcheck -Method GET
Invoke-WebRequest : Unable to connect to the remote server
At line:1 char:1
+ Invoke-WebRequest -Uri http://localhost:8088/healthcheck -Method GET
Any ideas why the target group is getting a 302 redirect, but if I query directly then I get 200 OK (unless I use the local address)?
Sorry, this was a coding issue with the application; there was a RequireHttps
filter buried deep in the application start-up code, that was obviously trying doing a redirect in the background; AWS was checking the website using HTTP and thus experiencing the 302 redirect, but when I was checking externally I was going over HTTPS which didn't trigger the redirect.