Skip to content

AWS calling ec2 on private subnet via ELB (with cognito) on 2 public subnets shows "An error was encountered with the requested page."

0

Hi,

I have a EC2 instance running a docker. I have currently deployed a nginx container which runs a React app. I further plan to deploy micro services on this same docker.

The EC2 instance described above is on a private subnet. I have set up a ELB on 2 public subnets which calls the EC2 on private subnet.

My hurdle is with setting up security.

  1. If I set up the ELB with Cognito, App client as Traditional-web-app, I get the message "An error was encountered with the requested Page"

  2. If I remove the Cognito from the ELB & have the React app directly use the Cognito (by providing login page & using SPA-Cognito-client), it does seem to work. But then, I am unsure if this is a great way to do it.

My understanding is that it would be best to place the Cognito on the ELB. Unsure if I am missing some thing but then I am unable to reach the EC2 as mentioned at point 1).

My Question. Will it be a must to put the React application on the ELB public subnet to do the authentication. Or, are there some better or other approaches. I would always prefer to have all my application & services running on one EC2 docker.

Regards, Prakash

3 Answers
3
Accepted Answer

For ALB Cognito setup, you can refer to How do I set up an Application Load Balancer to authenticate users through an Amazon Cognito user pool?

Alternatively, do not enable Cognito on the ALB. Let your EC2 React app handle Cognito authentication. Setup ALB with EC2 in private subnet as target. This option ensures that your EC2 instance is not directly exposed to internet. You can use ACM to request SSL/TLS cert and associate it with ALB, and associate AWS WAF with ALB to protect against web application attacks.

AWS
EXPERT
answered a year ago
0

Based on your setup and the issues you're encountering, there are a few points to consider and potential solutions to explore:

  1. Using Amazon Cognito with Elastic Load Balancer (ELB): It's generally a good practice to handle authentication at the edge of your network, which in this case would be the ELB. However, the error you're encountering suggests there might be a misconfiguration in how Cognito is integrated with your ELB.

  2. ELB and EC2 communication: Ensure that your ELB security group allows incoming traffic from the internet and that the EC2 instance's security group allows incoming traffic from the ELB. Also, check that your VPC routing is correctly set up to allow traffic between the public subnets (where your ELB is) and the private subnet (where your EC2 instance is).

  3. Cognito configuration: When using Cognito with ELB, make sure you've correctly set up the Cognito User Pool, App Client, and Domain. The callback URLs in your Cognito App Client should match the URLs that your application will use after authentication.

  4. Alternative approach: While it's possible to have the React app handle Cognito authentication directly (as you've found in your second approach), this isn't typically recommended for security reasons. It exposes your Cognito App Client credentials in the frontend code.

  5. Recommended approach: A common and secure pattern is to keep your React application on the EC2 instance in the private subnet, use the ELB with Cognito for authentication, and then pass the authenticated requests to your EC2 instance. This way, your application remains secure in the private subnet while still benefiting from Cognito's authentication.

  6. Troubleshooting: For the "An error was encountered with the requested page" message, check your ELB logs and EC2 instance logs for more detailed error messages. Ensure that your ELB health checks are passing and that your EC2 instance is correctly responding to requests from the ELB.

  7. Microservices consideration: As you plan to deploy microservices on the same Docker instance, consider whether this aligns with best practices for microservice architecture. Typically, microservices are deployed independently for better scalability and isolation.

In conclusion, you don't need to move your React application to a public subnet. The recommended approach is to keep using the ELB in public subnets with Cognito for authentication, while your EC2 instance with the React app and future microservices remains in the private subnet. Focus on correctly configuring the Cognito integration with your ELB and ensuring proper network connectivity between your ELB and EC2 instance.

Sources
Common scenarios - AWS Identity and Access Management
Amazon Cognito is now available in Asia Pacific (Melbourne) Region

answered a year ago
EXPERT
reviewed a year ago
0

This is a bit old but should still be valid example of configuring ALB+Cognito and decode JWT token at backend. In my case I used Lambda function as ALB backend to keep the demo as simple as possible but that shouldn't make a difference.

https://carriagereturn.nl/aws/cognito/alb/lambda/authentication/jwt/2019/06/15/pleased-to-meet-you.html

EXPERT
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.