The Amplify CLI provides a guided workflow to easily add, develop, test and manage REST APIs to access your AWS resources from your web and mobile applications.
A REST API or HTTP endpoint will be composed by one or more paths. Eg: /items
. Each path will use a Lambda function to handle HTTP requests and responses. Amplify CLI creates a single resource in Amazon API Gateway so you can handle all routes, HTTP Methods and paths, with a single Lambda function via a Lambda Proxy integration. HTTP proxy integrations forward all requests and responses directly through to your HTTP endpoint.
Amplify CLI let's you choose either an existing Lambda function or create a new one. To kickstart your implementation, you can choose between the following templates:
Lambda templates use serverless-express and provide the building blocks to start your REST API development.
See the list of all supported Lambda runtimes.
Amplify CLI allows you to restrict REST API access to
See a description of these user types below
For each user type you can further specify what actions it has access to.
REST APIs have support for multiple environments (e.g. dev, qa, and prod). This means that you can easily isolate different versions of your REST API by using different Amplify environments.
Because Amplify environments could be in separate AWS accounts, you cannot use the environment feature of API Gateway. Each Amplify environment will have a separate API Gateway resource associated with it. For example:
https://<API ID 1>.execute-api.eu-west-2.amazonaws.com/dev/items
https://<API ID 2>.execute-api.eu-west-2.amazonaws.com/prod/items
Create a REST API
Navigate into the root of a JavaScript, iOS, or Android project and run:
Follow the wizard to create a new app. After finishing the wizard run:
Select the following options:
This will be the configuration for /items
path in API Gateway:
|_ /items Main resource. Eg: /items
ANY Includes methods: DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT
OPTIONS Allow pre-flight requests in CORS by browser
|_ /\{proxy+} Proxy resource. Eg: /items/, /items/id, items/object/\{id}
ANY Includes methods: DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT
OPTIONS Allow pre-flight requests in CORS by browser
By default Amplify CLI creates a greedy path variable /items/\{proxy+}
that catches all child resources for a path and forwards them to your Lambda. This will match all child routes including /items/id
and /items/object/id
.
The Lambda function template Serverless ExpressJS function implements route handlers for GET
, POST
, PUT
and DELETE
Http Methods and paths for /items
and /items/*
. Some possible routes examples include:
GET /items List all items
GET /items/1 Load an item by id
POST /items Create an item
PUT /items Update an item
DELETE /items/1 Delete an item by id
You are not going to change this template but it's good that you have it open as you follow the next steps.
When configuration of your API is complete, the CLI displays a message confirming that you have configured local CLI metadata for this category. You can confirm this by running amplify status
. Finally deploy your changes to the cloud:
Amplify CLI restricts API access combining Amazon Cognito for authentication and AWS IAM (Identity and Access Management) for granting execution permissions on routes.
Deploy your new API.
At the end of this command you can take note of your new REST API url.
REST API endpoint: https://a5b4c3d2e1.execute-api.eu-west-2.amazonaws.com/dev
REST APIs follow this pattern
https://{restapi-id}.execute-api.\{region}.amazonaws.com/\{environment}/\{path}
.
Let's see an overview of all the resources created by Amplify CLI.
|_ itemsApi (Amazon API Gateway)
|_ itemsLambda (AWS Lambda)
|_ Logs (Amazon CloudWatch)
Create REST API and restrict specific routes to specific User Pool Groups
If your app uses User Pool Groups to manage different user types and would like to restrict access of specific routes to specific User Pool Groups. You can accomplish this by the following flow:
The following example flow assumes the existence of two User Pool Groups : AdminUsers and GuestUsers for a Book store. The app would like to limit admin functionality like updating book records to the AdminUsers User Pool Group, while borrowing and returning books would be limited to the GuestUsers User Pool Group.
- Path : /book/admin is restricted to AdminUsers and commands are handled by the bookAdminHandler lambda function
- Path : /book/guest is restricted to GuestUsers and commands are handled by the bookGuestHandler lambda function
At the end of this command you can verify the routes and their respective User Pool Group restrictions in the cli-inputs.json
file at the following path.
During the CLI setup, you'll be guided through to create a new Lambda function with a predefined serverless-express template with routing enabled for your REST API paths.
? Please select from one of the below mentioned services REST
? Provide a friendly name for your resource to be used as a label for this category in the project: itemsApi
? Provide a path (e.g., /book/\{isbn}) /items
? Choose a Lambda source Create a new Lambda function
? Provide a friendly name for your resource to be used as a label for this category in the project: itemsLambda
? Provide the AWS Lambda function name: itemsLambda
? Choose the function template that you want to use:
CRUD function for Amazon DynamoDB
⯠Serverless ExpressJS function
REST endpoint that triggers existing Lambda functions
During the CLI setup, you'll be guided through to use your own Lambda functions which you've initialized as a part of your CLI project using the amplify add function
command. This would allow you to have custom logic in your Lambda function and not use the predefined serverless-express templates generated by the CLI as in the examples above.
? Please select from one of the below mentioned services REST
? Provide a friendly name for your resource to be used as a label for this category in the project: itemsApi
? Provide a path (e.g., /book/\{isbn}) /items
Create a new Lambda function
⯠Use a Lambda function already added in the current Amplify project
Set up a REST API with Amazon DynamoDB
During the CLI setup, you'll be guided through to create a new Lambda function with a predefined serverless-express template with routing enabled for your REST API paths with support for CRUD operations to DynamoDB tables (which you can create by following the CLI prompts or use the tables which you've already configured using the amplify add storage
command).
? Please select from one of the below mentioned services REST
? Provide a friendly name for your resource to be used as a label for this category in the project: itemsApi
? Provide a path (e.g., /book/\{isbn}) /items
? Choose a Lambda source Create a new Lambda function
? Provide a friendly name for your resource to be used as a label for this category in the project: itemsLambda
? Provide the AWS Lambda function name: itemsLambda
? Choose the function template that you want to use:
⯠CRUD function for Amazon DynamoDB
Serverless ExpressJS function
In the example above with /items
path, the following API will be created for you:
When you have a sort key, you can append it to the end of the path, for example: GET /items/object/[ID]/[SORT_KEY_ID]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4