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"
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.
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
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!