The Ultimate Guide to Scaling Your Web Projects: Creating an Amplify App Using Terraform

Overview :-

Imagine you’ve got this brilliant idea for a web project. You start off small, but then it begins to grow… and grow. Suddenly, you’re not just managing a website; you’re architecting an entire ecosystem. That’s where tools like AWS Amplify and Terraform come into play. They work wonders for scaling, managing, and deploying web applications with ease. AWS Amplify focuses on the creation and management of digital projects, while Terraform allows you to script the infrastructure required for those projects in a safe, repeatable manner. Combining these two, you get a powerhouse for your web projects.

Pre-requisites :-

Before we dive into the nitty-gritty, let’s make sure you’ve got the basics covered:

  • AWS Account: You’ll need an AWS account since Amplify is a service provided by AWS.

  • Terraform Installed: Make sure Terraform is installed on your machine. It’s what you’ll use to script your infrastructure.

  • Basic Knowledge: A bit of background in AWS services and an understanding of Terraform will go a long way.

Don’t worry if you’re not a pro just yet. Like building your favorite Lego set, following the instructions step by step will help you create something amazing.

Procedure :-

Setting Up Your Terraform Configuration

First things first, let’s set up your Terraform environment:

  1. Initialize a Terraform Project: Create a new directory named amplify for your project .

  2. Write Your Terraform Configuration: You’ll create a main.tf file where you define your infrastructure as code. Here, you specify the AWS resources you need for your Amplify app, like your app instance and backend environments.

#main.tf
resource "aws_amplify_app" "amplify" {
  name  = "mahira-amplify-app"
  access_token  = var.token
  enable_auto_branch_creation   = false
  enable_basic_auth             = false
  enable_branch_auto_build      = false
  enable_branch_auto_deletion   = false
  environment_variables         = {
    "AMPLIFY_DIFF_DEPLOY"       = "false"
    "AMPLIFY_MONOREPO_APP_ROOT" = "account"
    "ENV"                       = "test"
    "_CUSTOM_IMAGE"             = "amplify:al2"
  }
  iam_service_role_arn   = var.role_arn
  repository   = var.github_repository
  custom_rule {
    source = "</^[^.]+$|\\.(?!(css|gif|ico|jpg|js|png|txt|svg|woff|ttf|map|json)$)([^.]+$)/>"
    status = "200"
    target = "/index.html"
  }
}

resource "aws_amplify_branch" "master" {
  app_id      = aws_amplify_app.amplify.id
  branch_name = "master"
  framework = "React"
  stage     = "PRODUCTION"
}
#variables.tf
variable "github_repository" {
  type = string
  description = "github repository url"
}

variable "token" {
  type = string
  description = "github oauth token"
}

variable "role_arn" {
  type = string
  description = "amplifyconsole-backend-role arn"
}
  • Define the Amplify App Resource: In your Terraform configuration, define an aws_amplify_app resource. This is where you outline the specifics of your app, including name, repository, and environment variables.

  • Connect Your Repository: Amplify integrates with GitHub, Bitbucket, and GitLab. Connect your repository to automatically deploy changes when you push.

Deploying Your Web Project :-

With your Amplify app created, it’s time to deploy your web project:

  1. Plan and Apply: Open a command prompt window and configure your aws credentials. Run terraform init to intialize your working directory. Next run terraform plan to preview the actions Terraform will take. Happy with what you see? Execute terraform apply to create your Amplify app in AWS. And voilà, you've set the stage for your app to come to life.

Conclusion :-

Scaling web projects doesn’t have to be a daunting task. With the right tools and a bit of patience, you can create scalable, efficient, and easily manageable web applications. By leveraging the power of Terraform with AWS Amplify, you’re setting yourself up for success, able to scale your projects with ease and confidence.

0
Subscribe to my newsletter

Read articles from Mahira Technology Private Limited directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mahira Technology Private Limited
Mahira Technology Private Limited

A leading tech consulting firm specializing in innovative solutions. Experts in cloud, DevOps, automation, data analytics & more. Trusted technology partner.