This is a serverless solution that leverages Amazon Web Services (AWS) to build a sample scalable and cost-effective API in order to get/create/delete products. It uses AWS API Gateway, AWS Lambda, and AWS DynamoDB to manage the API calls, execute the previously mentioned actions, and store the data. This solution can be deployed manually, using Terraform or CDK. See each of the sections for more details.
The API Gateway serves as the entry point to the serverless API. As per mentioned above, this is a sample API and authorization/authentication are currently not used for simplication purposes.
Lambda functions are the core of your serverless application. They execute the logic in response to the API Gateway requests. In this solution, the lambda function handle create, get and delete operations.
DynamoDB is a fully managed NoSQL database service by AWS. It is used to store and retrieve data related to your serverless application. In this solution, DynamoDB is used to store the products data.
Before you begin, if you would like to use this repository, ensure you have the following prerequisites in place:
- AWS Account: You must have an AWS account. If you don't have one, you can create one here.
- AWS CLI: Install and configure the AWS Command Line Interface (CLI) with your AWS access and secret keys. You can install the AWS CLI by following the instructions here.
- (Optional) Terraform: If you would like to use Terraform to deploy this solution, make sure you have Terraform installed on your local machine. You can download it from here.
- (Optional) CDK: If you would like to use CDK to deploy this solution, make sure you have Node.js and CDK installed locally, see below more details. You can also refer to the Getting started with the AWS CDK AWS Documentation on this for a more detailed overview.
- Git: If you want to clone this repository, you need Git installed. You can download it from here.
Finally, note that the Terraform template is by default configured to deploy the resources in the eu-west-1
region, you can easily change this on the second line of the main.tf
file to your preferred AWS region.
To deploy this serverless solution using Terraform, follow the steps below:
-
Clone the Repository: Clone this repository to your local environment:
git clone https://github.com/jasag/serverless-api-aws.git
-
Move into the repository folder.
cd serverless-api-aws
-
Initialize Terraform: Run the following command to initialize Terraform in your project directory:
terraform init
-
Plan and Apply: Run the following commands to plan and apply the configuration using Terraform.
terraform plan terraform apply
-
Destroy Resources: Optionally, run the following command to remove all the resources deployed by this Terraform configuration.
terraform destroy
To deploy this serverless solution using AWS CDK, follow the steps below. Kindly note that the DynamoDB table has been created with the RemovalPolicy.DESTROY
, this will result in the deletion of the DynamoDB table. Kindly remove this property prior creation if you would like to keep the table and its data when you delete the stack.
-
Clone the Repository: Clone this repository to your local environment:
git clone https://github.com/jasag/serverless-api-aws.git
-
Move into the repository folder.
cd serverless-api-aws/cdk
-
Activate the app's Python virtual environment and install the AWS CDK core dependencies
source .venv/bin/activate python3 -m pip install -r requirements.txt
-
Bootstrap the environment: Run the below command to bootstrap the environment. See more details about this step here.
cdk bootstrap
-
Deploy the CDK Stack: Run the below command to deploy the solution.
cdk deploy
-
Destroy the CDK Stack: Run the below command to deploy the solution.
cdk destroy
Alternatively, you can choose to deploy this manually to familiarize further with the services/steps required.
-
Create Lambda function. Code is included under the "serverless-api" folder. Kindly see the screenshots previously provided for mode details.
-
Create API Gateway. Please, see sample method configuration below.
- Create DynamoDB table.
This project is licensed under the MIT License - see the LICENSE file for more details.