AWS Amplify recently announced support for containers using AWS Fargate. Using the new feature, you are able to deploy APIs (REST & GraphQL) as well as hosting to AWS Fargate directly from the Amplify CLI.
In this post, I will walk you through how to deploy a Next.js app to AWS Fargate with a custom domain using the Amplify CLI.
To follow this tutorial, you will first need to install and configure the Amplify CLI.
First, create a new Next.js app:
npx create-next-app nextonfargate
Next, initialize a new Amplify project:
Next, reconfigure the project to enable container deployments:
Next, open the Route53 dashboard and click on Hosted zones in the left-hand menu.
Click on Create Hosted Zone and enter the name of the domain that you will be using, then click Create hosted zone.
Route53 should now give you 4 nameservers that you can use for configuring the DNS of your domain name.
Next, visit your domain name registration service and configure your domain with these nameservers.
Next, add Fargate Hosting by running the following command:
amplify add hosting ? Select the plugin module to execute: Container-based hosting with AWS Fargate ? Provide your web app endpoint: www.yourdomain.com ? Do you want to automatically protect your web app using Amazon Cognito Hosted UI: No
This should configure your project locally, and you should now see a Dockerfile created at the root of the project.
Update the Dockerfile with the following and save the file:
You should now be ready to deploy. To do so, you can run the following command:
Once the deployment is successful, you should be able to view the app on your live domain.
You should be able to view the deployment and build happening as well as any error logging in the CodeBuild dashboard.
Once you make a change and are ready to deploy, you should be able to run
amplify publish at any time to deploy updates.
Check out this video for a complete walkthrough of this process.