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.
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.
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
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
After installed the Amplify CLI, run the following command to setup your AWS account.
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!
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.
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:
When you run the Amplify CLI, it will present you with a series of questions to help you configure your project. These questions include:
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!"
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:
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.
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
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.
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.
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.
To generate unique IDs for articles, we recommend using the UUID package. Please navigate to the lambda folder and install the UUID package.
npm install --save uuid
Please ensure that you have imported the UUID package at the top of the app.js file
Then, modify the HTTP PUT method for inserting objects to generate a random article ID and a timestamp for the creation date.
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!
To deploy the new REST API to the cloud, navigate to the root directory of your project and run the following command:
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.
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.