Strapi deployment to AWS not working -> https://github.com/strapi/community-content/issues/241#issuecomment-775140466

System Information
  • Strapi Version: 3.4.5
  • Operating System: EC2 Ubuntu 20.04 t2.medium
  • Database: AWS RDS PostgresSQL 12.5-R1 free tier
  • Node Version: v12.20.1
  • NPM Version: 6.14.10
  • Yarn Version: Not installed on EC2 Ubuntu 20.04

Hi,

I’m working on my next tutorial which is “How to deploy Strapi to AWS using S3, AWS RDS, AWS EC2

I’m using AWS RDS service and I have selected postgres as my database for this tutorial.

I’m following Strapi official blog. Link -> Amazon AWS | Strapi Developer Documentation

Problem:

When I open EC2 Public IPv4 DNS URL, it doesn’t work.

Steps that I have taken:

  • Create EC2 instance Ubuntu t2.medium

  • Create S3 bucket

  • Create AWS RDS PostgresSQL 12.5-R1 free tier and set Public Access to Yes

  • Logged into my EC2 instance and clone Github repo and npm install

  • Created .env file and set below variables

DATABASE_PORT=5432
DATABASE_NAME=strapi
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=postgres321
AWS_ACCESS_KEY_ID=<my-access-key-id>
AWS_ACCESS_SECRET=<my-access-secret-key-id> 
BUCKET_NAME=<my-bucket-name>
REGION=<region>
  • Run NODE_ENV=production npm run build inside Strapi repo
  • Installed pm2 globally
  • Next, pm2 init
  • Edit ecosystem.config.js file with below contents:
  apps: [
    {
      name: 'strapi-aws',
      cwd: '/home/ubuntu/strapi-aws',
      script: 'npm',
      args: 'start',
      env: {
        NODE_ENV: 'production',
        DATABASE_HOST:'my-database-url.ap-south-1.rds.amazonaws.com',
        DATABASE_PORT:'5432',
        DATABASE_NAME:'strapi',
        DATABASE_USERNAME:'postgres',
        DATABASE_PASSWORD:'postgres321',
        AWS_ACCESS_KEY_ID:'<my-access-key-id>',
        AWS_ACCESS_SECRET:'<my-access-secret-key-id> ',
        BUCKET_NAME:'<my-access-secret-key-id> ',
        REGION:'<region>',
      },
    },
  ],
};
  • Run this command pm2 start ecosystem.config.js

I see that it has started successfully. See the attachment below. And when I try to access EC2 public url as https://ec2-65-0-105-135.ap-south-1.compute.amazonaws.com:1337/ . The url is not working?

What am I missing? Any help would be appreciated.

2 Likes

Hi @purnimagupta,

I have the same problem here.
Were you able to find a solution?

thankful

I have the same problem, but i think you should use the IPv4 Public IP and not the ‘Public IPv4 DNS’.

Again, I did this and have the same end result as you, but you could try :man_shrugging:

@mitchell @Kleyson_Morais Did you ever manage to solve this? We are having the same issue

Unfortunately no, I just went with a Heroku deployment.

I finally got it working just after I posted the question. I had to add an inbound PostgresSQL rule for the IP address of the ec2 instance.

Running pm2 log helped!

I think the issue was I was accessing it with https but it was actually working with only http. Let me know if this works

Hi I’m currently having this issue as well. I’ve deleted and recreated ec2, rpd and s3 at least 5 times with no luck. Would you be kind enough to detail your solve? Thanks so much in advance.

That looks like a firewall issue, in the EC2 instance config/security policy do you have port 1337 open?

(Looks like it’s filtering ICMP aka ping requests also, best guess your security policy only allows for SSH)

In my case, inbound and outbound security was configured per the documentation

AWS Deployment - Strapi Developer Documentation.

1337 was configured to open but the ipv4 address itself immediately refuses connection from the browser even though the pm2 shows that database is live like @OP’s case. I checked the firewall in the terminal and it returns inactive so I don’t believe it is a firewall issue either.

Edit: *RDS

I managed to get this working. The issue I had was with the strapi config files.

  1. server.js - should have host and port:

host: env(‘HOST’, ‘0.0.0.0’),
port: env.int(‘PORT’, 1337),

  1. database.js - fill in according to aws settings

     host: env('DATABASE_HOST', '[your rds end point]'),
     port: env.int('DATABASE_PORT', 5432),
     database: env('DATABASE_NAME', 'strapi'),
     username: env('DATABASE_USERNAME', 'postgres'),
     password: env('DATABASE_PASSWORD', '[your rds password]'),
    
  2. plugins.js - copy and paste as follows (No need to make changes)

module.exports = ({ env }) => ({
upload: {
provider: ‘aws-s3’,
providerOptions: {
accessKeyId: env(‘AWS_ACCESS_KEY_ID’),
secretAccessKey: env(‘AWS_ACCESS_SECRET’),
region: env(‘AWS_REGION’),
params: {
Bucket: env(‘AWS_BUCKET_NAME’),
},
},
},
});

  1. .env - configure your environment according to your AWS taking note of the APP_HOST and NODE_PORT

APP_HOST=10.0.0.1

HOST_PORT=1338

DATABASE_HOST=[Your RDS end point]

DATABASE_PORT=5432

DATABASE_NAME=strapi

DATABASE_USERNAME=postgres

DATABASE_PASSWORD=[Your RDS password]

AWS_ACCESS_KEY_ID=[Your Access key]

AWS_ACCESS_SECRET=[Your Secret Key]

AWS_REGION=[Your AWS region]

AWS_BUCKET_NAME=[Your Bucket Name]

On your RDS make sure the security group is the same as the group being used by the EC2 Instance. and set the inbound rules per the documentation

Hope this helps anyone else having the same issues.

What exactly did you change that fixed the issue?

In Server.js I changed APP_HOST to HOST and NODE_PORT to PORT. Hope that works for you.