Run a NodeJS Shopify app on AWS

Felipe López
Towards AWS
Published in
3 min readJun 6, 2021

--

The following post explains how to deploy and run a simple NodeJS Shopify app on AWS as an ECS fargate service with a dedicated domain and SSL certificate.

The app is based on the NodeJS sample from Shopify CLI. The resources are deployed with CDK.

For more information about the Shopify app itself see:

The full code is stored in this repository: https://github.com/felipeloha/aws-shopify-app

If you are not familiar with AWS, CDK, and specifically with ECS, I recommend reading about it beforehand.

Shopify-app-node Modifications

The following modifications are necessary to make the shopify-cli-app work with ECS. The code in the repository has the modifications already implemented.

  • The app created by Shopify CLI is based on a .env file to read the configuration.
    Our project separates the code/docker image from the configuration so that the same docker image can be deployed to different instances and it still works.
    For this purpose, the next.config.js was modified to use publicRuntimeConfig so that it receives the SHOPIFY_API_KEY as an environment variable and makes it available on the frontend.
  • A health endpoint /health is implemented which is used from the AWS ECS

Infrastructure

The infrastructure project creates the following resources

  • ECS service with an application load balancer from ecs_patterns
  • An SSL certificate
  • An API gateway with a custom domain (linked to the certificate) that proxies all calls to the load balancer
  • A route 53 alias pointing to the API gateway

This a simplified diagram of the generated/involved resources:

Preconditions to deploy the app

  • You have a Shopify partner account and store with products and orders
  • A Shopify app with “read all orders” and “read orders” permissions and an api key/secret is created in the partner account. The permissions must be requested and accepted by Shopify. The process takes about a day.
  • You have an AWS account with: user with admin rights, a VPC and a hosted zone connected to a domain

Get the app up and running

Follow the next steps to get the app working:

  1. The code build projects have bitbucket as the source and a specific repository name. It should be modified to your needs.

2. Build a docker image of the Shopify app and push it to an ECR repository

  • Deploy the code build project on ci-cd/docker. this project creates an ECR repository Shopify-app and a code build project that build and push the docker image of the current branch
  • Run the code build project with the version of the docker as a parameter like BRANCH_NAME-COMMIT_ID. e.g. “develop-765876”. the commit ID is used to make the docker image unique so that the same branch can build multiple times

3. Create the app configuration in AWS secrets with the name “STACK_NAME-secrets” in the same region that the app is going to be deployed so that it can be read on ECS start.

The content of the secret should have the following form:

SHOPIFY_API_KEY: "xxx",
SHOPIFY_API_SECRET: "xxxx",
SHOP: "shop.myshopify.com",
SCOPES: "read_orders,read_all_orders",
HOST: `https://${subdomain}`,

4. Modify and deploy the infrastructure as CDK which will automatically deploy the generated docker image to the ECS service

npm i && npm run build && npm testSTACK_NAME=shopify cdk synthSTACK_NAME=shopify cdk deploy --parameters version=master-244fc86

5. Configure and run the app in Shopify

  • Go to the Shopify app setup and enter the URL of the deployed application
  • Run the app

After these steps, you will have a Shopify app running on shopify.domain.com with an SSL certificate which can be invoked from the app you created in the Shopify partner account.

This architecture can be extended to deploy multiple applications by creating further ECS services, route53 entries, and proxying the requests accordingly.

I hope this helps to get an app up and running in an easy way.

If you have any comments or improvement ideas, feel free to comment or contact me.

If you liked this post, please follow me. I would be very grateful ;)

--

--