Strapi Deployment in AWS Amplify

System Information
  • Strapi Version:
  • Operating System:
  • Database:
  • Node Version:
  • NPM Version:
  • Yarn Version:

Has anyone tried to deploy Strapi app using AWS amplify console “deliver” feature? I have no issues while hosting a normal react app but when I try hosting strapi app, the build files are not accessible (404) … Something went wrong with the path. Please find my build settings below. Any help is much appreciated.

Build Settings in Amplify :

version: 1
frontend:
  phases:
    preBuild:
      commands:
        - npm install
    build:
      commands:
        - npm run build
  artifacts:
    baseDirectory: build
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*

Code seems ok, it is similar to React’s example. index.html file should be accessible even without the backend, but the js files which are included from /admin/{name}.js are not accessible directly. In that case you will get 404 for these files.
You should also launch the backend, with npm run start command:

backend:
  phases:
    postBuild:
        commands:
        - npm run start

@sunnyson Thanks for the quick response. I tried the postBuild command but the deployment got stuck in build phase in amplify console. Please find the build logs below.

2020-12-14T17:13:33.939Z [INFO]: # Completed phase: preBuild
                             # Starting phase: build
                             # Executing command: npm run build
2020-12-14T17:13:34.110Z [INFO]: > event-marketing-strapi-app@0.1.0 build /codebuild/output/src058873696/src/ema-serverless-app
                             > strapi build
2020-12-14T17:13:34.977Z [INFO]: Building your admin UI with development configuration ...
2020-12-14T17:13:37.302Z [INFO]: [info] [webpackbar] Compiling Webpack
2020-12-14T17:14:29.497Z [INFO]: [success] [webpackbar] Webpack: Compiled successfully in 52.19s
2020-12-14T17:14:29.517Z [INFO]: # Completed phase: build
                             # Starting phase: postBuild
2020-12-14T17:14:29.518Z [INFO]: # Executing command: npm run start
2020-12-14T17:14:29.677Z [INFO]: > event-marketing-strapi-app@0.1.0 start /codebuild/output/src058873696/src/ema-serverless-app
                             > strapi start
2020-12-14T17:14:32.124Z [WARNING]: /bin/sh: hostname: command not found
2020-12-14T17:14:32.750Z [INFO]:

@sunnyson Still couldn’t get past this. Any thoughts on this?

I’m not really familiar with CI/CD, I used amplify just a few times for testing purposes. But maybe @DMehaffy has some experience?

I’m not largely familiar with amplify but based on that error, you are trying to pass a hostname into the HOST var in your ./config/server.js.

Can you provide us your ./config/server.js and are you using the environment system (AKA ./config/env/production/server.js)?

@DMehaffy Please find the server.js below

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
  admin: {
    auth: {
      secret: env('ADMIN_JWT_SECRET', '2ad515f650dc204010a86703e490d7d8'),
    },
  },
});

And no, we are not using the environment system for now.

Looks like this is related.

But on a side point I do recommend you use the environment system, and assuming you have some kind of proxy application (Nginx, HAProxy AWS ELB, ect) configure the url in that file.

It could be related to how we generate our telemetry :thinking: and the missing command from whatever container service amplify is using. We did have a bug report about that here:

@DMehaffy Thanks. The issue raised in amplify was done by one of my teammates. :see_no_evil: We will follow that thread for answers … Meanwhile, it looks like a non-shell command issue too. Do we have a workaround for this until Strapi fix this bug?

@Prabhu_Raj Currently I need to patch the node_modules/strapi/lib/services/metrics/sender.js on the Dockerfile after npm install is done, something like this

  // ./patches/strapi/lib/services/metrics/sender.js
   ...
  // machineIdSync() will not work on none-shell host
  // We should disable it when opt-out telemetry
  // https://strapi.io/documentation/v3.x/getting-started/usage-information.html#commitment-to-our-users-data-collection
  if (uuid){
    deviceId = machineIdSync();
  }

Then in the Dockerfile

...
RUN yarn install

# Patches
COPY ./patches ./node_modules
...

It works as a work around for us now.

@hxhieu and @Prabhu_Raj I’ve raised the issue directly with our backend engineers. We are currently in the middle of a feature development sprint (among a few other things :stuck_out_tongue:) but I’ll see if they can find a patch soon.

However @hxhieu if you are willing to submit a pull request, please do. It’s been a busy month for us :see_no_evil: and on a limited team of devs, especially with the holidays.

Thanks for your input. I tried this workaround, added some log statements as in the screenshot.

I was able to see the logs in AWS Amplify logs. I think issue is happening somewhere else too. Do we have to add some other change? Let me know if i miss something.

Deploy log:
2020-12-21T16:07:03.383Z [INFO]: # Executing command: npm start
2020-12-21T16:07:03.539Z [INFO]: > event-marketing-strapi-app@0.1.0 start /codebuild/output/src655975257/src/ema-serverless-app
> strapi start
2020-12-21T16:07:05.783Z [INFO]: Sender JS - UUID : 49cac414-4f23-49e1-9044-1abd4a57f3c2
2020-12-21T16:07:05.784Z [INFO]: Sender JS - If
2020-12-21T16:07:05.820Z [WARNING]: /bin/sh: hostname: command not found
2020-12-21T16:07:05.821Z [INFO]: Sender JS - deviceId : a4c040f31f2eeba3074170cef0446ee4187ae16324f69da8678bc7f1b21f0455
2020-12-21T16:07:06.461Z [INFO]:

@Santhosh222 Unfortunately you need to opt-out the telemetry i.e. remove the uuid from the package.json, then the machineIdSync() method will not fire.

There is no other way to enable telemetry until the dependency on machineIdSync() is removed or update with one that does not rely on shell script.

1 Like

Thanks for the input!

I removed the uuid from package json. This resolved the issue /bin/sh: hostname: command not found. But the deploy is still getting stuck after the command npm start. There are also no errors after this command.

hello! Is there any solution? i have delete uuid from package json and the /bin/sh: hostname: command not found it continues to appear in the amplify deploy…
is there any other way? thanks!! :smiley:

version: 1
backend:
  phases:
    preBuild:
      commands:
        - cd cms
        - npm install
    build:
      commands:
        - npm run build
    postBuild:
        commands:
        - npm run start
  artifacts:
    baseDirectory: build
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*
1 Like

It’s not working… :cry:

Hey everyone need some serious help! I am at the last of the project which I have to deliver.
at the start I build my next js frontend and deploy it on aws amplify successfully.
after that I start working on strapi, setup the project and now I want to deploy it on aws amplify.
getting same issues described abve.
this is the log message.


here is the package.json

and this is the result i am getting in my browser.
empty screen white.

hello !
How did you resolve this 404 error to deploy strapi on aws amplify please consider it and help me to solve this 404 error.

Hi @gAutam,

I had similar problem. Before contacting AWS techsupport was doing random search. Then decided to contact aws for support, and then came to know AWS Amplify does not support Strapi