Skip to content

How to expose port 3030 in AWS container website?

0

I have a react project which includes a 'server.js' file which I am going to use to make data requests with important security secrets, at the moment I'm trying to make it work with simple string responses. https://i.sstatic.net/itkTJbtj.png When I run both npm start and node src/server/server.js locally, it works great, clicking the 'fetch data' react button makes a request to localhost:3030 (where the server is running) and displays the result. https://i.sstatic.net/7NghCieK.png

When I try the same thing on my amazon web service container website, it fails, saying that it failed trying to fetch localhost:3030

https://i.sstatic.net/LhShLrHd.png

How do I fix this so that in my live production AWS instance, I can make api requests to my server.js file ? My code is here: https://github.com/MartinBarker/aws-react-docker-ghactions

And was built by following this guide on aws container web setup for creating the task / service rules. https://medium.com/@nwosuonyedikachi/deploying-a-react-app-to-aws-ecs-with-github-actions-5c74c1869800

asked 2 years ago702 views

3 Answers
0

Hi mparker,

Please go through the below steps i hope it will helps to resolve your issue.

1. Update Dockerfile

Ensure your Dockerfile exposes port 3030:

FROM node:14

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000 3030

CMD ["npm", "start"]

2. Update ECS Task Definition

Make sure your ECS task definition specifies port 3030 in the container definitions:

"containerDefinitions": [
  {
    "name": "your-container-name",
    "image": "your-image",
    "portMappings": [
      {
        "containerPort": 3000,
        "hostPort": 3000
      },
      {
        "containerPort": 3030,
        "hostPort": 3030
      }
    ],
    ...
  }
]

3. Update Security Group

Ensure that the security group associated with your ECS service allows inbound traffic on port 3030:

  • Go to the EC2 Console.
  • Navigate to Security Groups.
  • Select the security group associated with your ECS service.
  • Edit the inbound rules to allow traffic on port 3030 (e.g., from 0.0.0.0/0 for testing purposes).

4. Update Application Load Balancer (if used)

If you are using an Application Load Balancer (ALB):

  • Go to the EC2 Console.
  • Navigate to Load Balancers.
  • Select your load balancer.
  • Edit the listener to include port 3030.
  • Create a target group for port 3030 and register your ECS instances.

5. Adjust Your React App API Requests

Update your React app to make API requests to the correct endpoint. Instead of using localhost:3030, use the public DNS of your ECS instance or the DNS name of your load balancer:


fetch('http://your-ecs-instance-or-load-balancer:3030/api-endpoint')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

EXPERT

reviewed 2 years ago

  • How would I make a request to my load balancer URL? Would I need to specify the port or just fetch mydomain.com/db/getCreds? Trying to implement with this code:

    const fetchData = async () => { console.log('fetching data..') try { const response = await fetch('http://rendertune.com:3030/db/getCreds'); if (!response.ok) { throw new Error(HTTP error! status: ${response.status}); } const result = await response.text(); // Adjust this if your server responds with JSON or another format setData(result); setError(''); // Clear any previous errors } catch (error) { console.error('Error fetching data:', error); setError(error.message); } };

    I've gone through the rest of your help to try and make sure the port is exposed in all the listeners / tasks.

0

Hello,

I think your src/App.js requests localhost:3030 on your computer instead of the EC2 server. If you run src/server/server.js locally, I think it will work.

If you want to expose this api, you should create a reverse proxy in nginx.conf:

worker_processes 1;

events {
  worker_connections 1024;
}

http {
  include mime.types;
  default_type application/octet-stream;

  sendfile on;
  keepalive_timeout 65;

  server {
    listen 80;

    location /internal-api {
        proxy_pass http://localhost:3030/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location / {
      add_header 'Access-Control-Allow-Origin' '*';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';

      root /usr/share/nginx/html;
      index index.html;
      try_files $uri $uri/ /index.html;
    }
  }
}

And then change http://localhost:3030 in src/App.js to http://example.com/internal-api

answered 2 years ago

0

Solved, had ot ensure my node-server was running first, I did this my building and running the docker image locally, after making sure it was running on aws, i checked all the port info to make sure it was reachable.

answered 2 years 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.