AWS Amplify – steps towards a full backend AWS implementation

AWS Amplify – steps towards a full backend AWS implementation

AWS Amplify is a cloud development platform that has revolutionized the process of building modern web and mobile applications. With its extensive set of tools and services, AWS Amplify allows developers to create highly scalable, secure, and feature-rich backend services without requiring in-depth knowledge of Amazon services and backend technology.

Designed with a frontend-focused approach, AWS Amplify enables developers to add essential features like data storage, file storage, and authentication to their applications effortlessly using either the AWS Command Line or the Amplify Studio interface. Its user-friendly interface and detailed documentation make it easy to build and deploy high-quality applications rapidly and efficiently.

In this article, we will demonstrate how to use AWS Amplify CLI to create a backend service that enables users to create blog posts with authentication functionalities seamlessly. We will explore the steps involved in building a robust backend service, including creating API endpoints, configuring authentication, and deploying the application. By the end of this article, readers will have a clear understanding of how AWS Amplify can simplify the development process for building scalable and secure web and mobile applications.

Prerequisite

Before proceeding to the next section of this tutorial, it's important to make sure you have an AWS account. If you don't already have one, you can sign up by going to the AWS homepage and clicking on "Create an AWS Account".

Additionally, since we will be using Node.js and Amplify CLI in this tutorial, you will need to have at least version 10.x or later of Node.js installed in order to run the application and install the Amplify CLI.

Development Environment Setup

We need to set up an IAM account with sufficient privileges to use the Amplify account. Amazon Amplify provides two methods for developers to configure their applications: Amplify Studio and Amplify CLI. In this tutorial, we will be using Amplify CLI.

Set up an IAM account for Amazon Amplify

  1. Go to the AWS Management Console and navigate to the IAM service
  2. Click on "Users" in the left-hand navigation menu and then click on "Add user"
  3. Enter a user name for the new User and click “Next”Set Username
  4. In the "Set Permissions" tab, choose the option "Attach policies directly." Look for "Amplify" in the policy search bar and choose "AdministratorAccess-Amplify." Click the "Next" button.Set Permissions
  5. Review the user's information and click "Create user."
  6. After creating the user, select the user name from the user list.
  7. Click on the "Security Credentials" tab, and then scroll down to the "Access keys" section.
  8. Under the "Access key best practices & alternative" setup, choose the "Command Line Interface (CLI)" option. Check the box that says "I understand the above recommendation and want to proceed to create an access key", and then click "Next".Set Permissions
  9. Click on the "Download .csv file" button to download the access key pair. We will use this key pair to create an Amplify profile and run the Amplify CLI.Set Permissions

Install Amplify CLI

The Amplify CLI can be installed by simply running the following command. Depending on your computer's configuration, you may need to run it under sudo mode.

npm install -g @aws-amplify/cli

Configure the Amplify CLI

After installed the Amplify CLI, run the following command to setup your AWS account.

amplify configure
  • Choose the region that is closest to you or the region you prefer to use.Amplify CLI
  • Enter the Access Key ID and Secret Access Key from the CSV file you downloaded, and specify the profile name.Amplify Config

Once you've completed all the steps, your Amplify project is now set up and ready to use. You can begin configuring your project with the Amplify CLI!

Create an Amplify Project

Amplify is a powerful set of tools designed to make it easy for you to create scalable, production-level applications that leverage a variety of different Amazon services. With Amplify, you can quickly add authentication, database functionality, and storage capabilities to your application with just a few lines of code, without needing to have in-depth knowledge of the underlying technologies. This allows you to enjoy the convenience and power of Amazon Web Services without needing to be an expert in every technology that Amplify supports.

Initialize the Amplify project

To initialize your project using Amplify CLI, you can use the command line interface. Navigate to the folder where you want to create your project and run the following command:

amplify init

When you run the Amplify CLI, it will present you with a series of questions to help you configure your project. These questions include:

  • Enter a name for the project Enter a name for your project. By default, it will use the name of your current folder.
  • Initialize the project with the above configuration If you want to use the default configuration provided by Amplify CLI, answer "Yes". Choose “Yes” for this tutorial.
  • Please choose the profile you want to use Choose the profile you previously created using `amplify configure`.

Once you have configured your project using the Amplify CLI, it will create the necessary resources to run your project, deploy the root stack to Amazon, and generate the basic structure of your project. If everything is successful, you should see a message saying "Your project has been successfully initialized and connected to the cloud!"

Amplify Init

Adding authentication to your project

In order to restrict article creation, updating, and deletion to logged-in users, we need to implement an authentication service. Amplify offers a simple solution to add authentication to our project with just one command.

amplify add auth

You'll then be prompted with a few questions related to authentication and security:

  • Do you want to use the default authentication and security configuration? The tutorial recommends selecting the default authentication and security configuration.
  • How do you want users to be able to sign in? You'll be able to choose the user attribute that will allow users to sign in. For this tutorial, we'll use email as the login credential.
  • Do you want to configure advanced settings? If you want to keep it simple, you can answer "No" to configuring advanced settings for this tutorial.Advanced settings

A set of configuration files will be generated inside the "auth" folder of your project. These files contain the necessary configuration for implementing authentication functionality in your project.

Creating the RESTful APIs by integrating with DynamoDB

One of the benefits of Amplify is that it abstracts away the underlying infrastructure and technology choices required to build such applications. Instead, developers can focus on writing their business logic and let Amplify handle tasks like service selection, auto-scaling, and other operational aspects. This allows for faster development and deployment of applications without worrying about the underlying technical details.

Using Amplify CLI, we can quickly create a set of RESTful APIs that perform CRUD (create, read, update, delete) operations on articles stored in a database. With just a few lines of command, we can generate APIs that enable us to easily create, update, delete, and retrieve articles from the database.

Open your terminal and enter it in the command prompt:

amplify add api

This will initiate a series of questions that prompt you for the necessary information to create a RESTful API

  • Select from one of the below mentioned services: First, you'll be asked to select a service, choose "REST" for this project.
  • Provide a friendly name for your resource to be used as a label for this category in the project: We'll use "article" as a label for this category in the project.
  • Provide a path (e.g., /book/[isbn]): "/articles"
  • Provide an AWS Lambda function name: This question prompts you to enter a name for the AWS Lambda function that handles incoming requests. For this tutorial, you can choose "articleLambda".
  • Choose the runtime that you want to use: Select Node.JS for this tutorial
  • Choose the function template that you want to use: Select "CRUD function for DynamoDB (Integration with API Gateway)" as we'll be using the CRUD template with DynamoDB.
  • Choose a DynamoDB data source option: Select "Create a new DynamoDB table"Advanced settings

Next, you'll be prompted to configure DynamoDB. For this example, we'll create four columns: articleId, title, content, userId, and createdAt. We'll select "articleId" as the partition key (which is similar to the primary key in a relational database) and "createdAt" as the sort key for that table. Keep in mind that DynamoDB only allows sorting the table by the sort key by default.

DynamoDB Sort Key

To improve API security, we will configure access controls so that only authenticated users can perform read, creation, update, and deletion operations on articles. Additionally, we will allow guest users to only read articles.

DynamoDB Permissions

The Amplify CLI does not enable guest access by default when using the 'amplify add auth' flow. However, you can enable this feature by executing the 'auth update' command and selecting the option to walk through all the authentication configurations. From there, choose the 'User Sign-Up, Sign-In connected with AWS IAM controls' option and confirm with a "yes" when prompted to allow unauthenticated logins.

Allow Unauthenticated Logins

To generate unique IDs for articles, we recommend using the UUID package. Please navigate to the lambda folder and install the UUID package.

cd ./amplify/backend/function/articleLambda
npm install --save uuid

Please ensure that you have imported the UUID package at the top of the app.js file

Modify UUID

Then, modify the HTTP PUT method for inserting objects to generate a random article ID and a timestamp for the creation date.

Modify HTTP

We need to update the value of userIdPresent to "true" as well.

const userIdPresent = true;

Great job! With the development of the program completed, it's time to move on to the next step: deploying your application to the cloud!

Deploying your application to the cloud

To deploy the new REST API to the cloud, navigate to the root directory of your project and run the following command:

amplify push

After running the deployment command, you may be prompted to review the changes to your package compared to the existing version on the Amazon console. Please carefully review and confirm these changes. Once confirmed, the Amplify CLI will begin deploying your application. To avoid any issues, it is recommended that you keep your terminal open during the deployment process.

Configuration!

If you have followed the flow mentioned above, you should now be able to push the server to Amazon! Learn more about implementing the frontend application with Amazon Amplify here.

Need help with custom app development in Vancouver? Our mobile app developers in Vancouver are here to help. Contact us today.

Find out how we can work together to bring your ideas to life.

Find out how we can work together to bring your ideas to life.

Thank you for contacting us