Migrating Strapi from Heroku to AWS
Tags: strapi, heroku, aws, microtica
Published: 2022-10-10 17:38:37


This is a step-by-step tutorial on how to migrate your existing Strapi project from Heroku to AWS, using Microtica’s ready-made Strapi infrastructure template. This guide applies to developers that have a running application on Heroku with PostgreSQL or MySQL database and covers:

1. Setup of Strapi infrastructure on AWS
2. Migrating PostgreSQL/MySQL database from Heroku to AWS

If you don’t have an existing Strapi project on Heroku, here is a guide on [how to deploy a new Strapi Application](https://docs.microtica.com/deploy-a-new-strapi-app?utm_source=devto&utm_medium=blog&utm_campaign=migrate_strapi_from_heroku) on your AWS account.

Before we get started, please make sure you:

- Have access to your Heroku account and CLI
- [Create a Microtica account](https://app.microtica.com/auth/signup?utm_source=devto&utm_medium=blog&utm_campaign=migrate_strapi_from_heroku)

## 1. Setup Strapi infrastructure on AWS
When you deploy infrastructure with the **Strapi Serverless template**, you have the possibility to create a new Strapi application in your Git repository or select an existing Strapi application if you already have one.

By using Git for your Strapi projects, the process of moving content types from one environment to another is much easier and deployments are more predictable.


[Deploy Strapi on AWS Now
](https://app.microtica.com/templates/new?template=https%3A%2F%2Fraw.githubusercontent.com%2Fmicrotica%2Ftemplates%2Fmaster%2Fstrapi-serverless%2F.microtica%2Ftemplate.yaml&utm_source=medium&utm_medium=blog&utm_campaign=migrate_strapi_from_heroku)
##Review your database configuration
Before deploying the Strapi template, please make sure that you have a correct database configuration. This is important to make it right because the template provides some [predefined environment variables](https://docs.microtica.com/deploy-an-existing-strapi-app?utm_source=devto&utm_medium=blog&utm_campaign=migrate_strapi_from_heroku#%3A%7E%3Atext=predefined+environment+variables) in the app runtime that can be used in the Strapi project.

Here is an example of a working database configuration:

```
config/database.js

module.exports = ({
  env
}) => {
  const connections = {
    sqlite: {
      connection: {
        client: 'sqlite',
        connection: {
          filename: env('DATABASE_FILENAME', '.tmp/data.db'),
        },
        useNullAsDefault: true,
      }
    },
    mysql: {
      connection: {
        client: 'mysql',
        connection: {
          host: env('DATABASE_HOST', 'localhost'),
          port: env.int('DATABASE_PORT', 3306),
          database: env('DATABASE_NAME', 'strapi'),
          user: env('DATABASE_USERNAME', 'strapi'),
          password: env('DATABASE_PASSWORD', 'strapi')
        },
        debug: false,
      }
    },
    postgres: {
      connection: {
        client: 'postgres',
        connection: {
          host: env('DATABASE_HOST', 'localhost'),
          port: env.int('DATABASE_PORT', 3306),
          database: env('DATABASE_NAME', 'strapi'),
          user: env('DATABASE_USERNAME', 'strapi'),
          password: env('DATABASE_PASSWORD', 'strapi')
        },
        debug: false,
      }
    }
  };

  return connections[env('DATABASE_CLIENT')];
};
```
### Deploy your app using the [Strapi Serverless Template](https://app.microtica.com/templates/new?template=https%3A%2F%2Fraw.githubusercontent.com%2Fmicrotica%2Ftemplates%2Fmaster%2Fstrapi-serverless%2F.microtica%2Ftemplate.yaml&utm_source=devto&utm_medium=blog&utm_campaign=migrate_strapi_from_heroku#_ga=2.129781324.1388053818.1665405827-1355638904.1663058245).
![select strapi template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wglxk5zak1q378ot1ztb.png)

### 1.1. Import Git Repository
As a first step, you need to connect a Git account (GitHub, Bitbucket, or GitLab). If you already have a Git account connected to Microtica, you can choose it from the list. Select the repository with your existing project, click **Import**, and every Git push will be deployed automatically in Microtica.

![import git](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nml39nzoegfzzldskhqg.png)


### 1.2. Configure Template
The second step is to configure the environment variables for this template. Variables provide a way to customize a template to meet your requirements. For example, here is where you configure the scaling parameters for your Strapi app. You can scale your app vertically and horizontally. For vertical scaling, update the CPU and Memory configuration in this section. For horizontal scaling, update the number of desired replicas in the same section.

In this section you can also choose the database client — sqlite is the default option.

![configure template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2urr8d6wav76powwiua4.png)
You can update the configuration later as well.

### 1.3. Configure Environment
After configuring the template, you will need to select an environment where the template will reside. Environments are a great way to separate your development and production applications. If you need to create a new environment, you can enter the environment name and description and click the **Select** button. You can also choose an existing environment if you have created one previously.

Next, you need to configure the target AWS account where your Strapi infrastructure will be provisioned. You can connect an AWS account right there on the spot, in just a few seconds.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sqmdjvxpk315px2kbma8.png)


### 1.4. Deploy
Once your AWS account is connected and configured in the environment, click on the **Deploy** button to trigger the deployment.

**It takes up to 10 mins to create a live Strapi environment.**

You can follow the pipeline logs inline, and monitor your deployment in real-time.

Once the template deployment finishes, you can find the access URL in **Environment > Infrastructure > Strapi** (Strapi is the AppName we provided in the template configuration step), under Resource outputs, the **AccessUrl** parameter. Copy-paste it in the browser to see your Strapi Demo app.

![deploy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/50ip5nq7dqwagx4v8hl6.png)



When configuring the template make sure that you select **postgres** or **mysql** as **DatabaseClient**.

Once the infrastructure is deployed and ready (which should be in about 10 minutes), you can proceed with migrating your existing data from Heroku to AWS.

## 2. Migrate database from Heroku to AWS
Download all data from the Heroku database locally
To export the data from your Heroku **Postgres database**, create a new backup and download it, using the following commands:

```
PostgreSQL
heroku pg:backups:capture — app example-app
heroku pg:backups:download — app example-app
```
To export the data from your Heroku **MySQL database**, dump the Heroku database on your local development environment using [mysqldump](https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html) tool.

```
MySQL
mysqldump — set-gtid-purged=OFF — single-transaction -hHOST_HERE -uUSER_HERE -P PORT_HERE -p DB_NAME_HERE > heroku_db
# — set-gtid-purged=OFF — required property for RDS because you don’t have full permission in RDS.
# — single-transaction — Use to ensure that all of the data loaded from the local database is consistent with a single point in time. If there are other processes changing the data while mysqldump is reading it, using this option helps maintain data integrity.
# -h — hostname of the DB. No space between -h and the value.
# -u — Use to specify a user name. No space between -u and the value.
# -P — mysql port
# -p — once the command is run you’ll be asked for the DB password
# DB_NAME_HERE — the name of the database you want to download locally
```
If you get this error message **“Unknown table ‘COLUMN_STATISTICS’ in information_schema”**, add the command **— column-statistics=0**. The command adds ANALYZE TABLE statements to the output to generate histogram statistics for dumped tables when the dump file is reloaded.

If you get this error message **“Access denied; you need (at least one of) the PROCESS privilege(s) for this operation”**, add the command **— no-tablespaces**. The suppresses all CREATE LOGFILE GROUP and CREATE TABLESPACE statements in the output of mysqldump.

##Connect to Amazon RDS database
To establish a connection between your local development environment and AWS please follow the [Access the RDS database](https://docs.microtica.com/access-the-rds-database?utm_source=devto&utm_medium=blog&utm_campaign=migrate_strapi_heroku) guide.

Once the connection is established, you can proceed with moving the local copy of the data to AWS.

##Move local database copy to AWS
Load the dump into your **Amazon RDS PostgreSQL** using the [pg_restore](https://www.postgresql.org/docs/current/app-pgrestore.html) tool.

```
PostgreSQL
pg_restore — verbose — clean — no-acl — no-owner -h localhost -U dbuser -d strapi latest.dump
```
Load the dump into your **Amazon RDS MySQL** database using the [mysql](https://dev.mysql.com/doc/refman/8.0/en/mysql.html) tool.

```
MySQL
mysql -udbuser -h localhost -P 3306 -p strapi < heroku_db
```