The purpose of this repository is to provide basic demo code as a starting point for building a persistent API using Terraform. This repository makes use of open-source Terraform modules from the Terraform Registry. Please note, this repository should only be used for example purposes, and all modules being referenced from the Terraform Registry should be fully vetted if you plan to re-purpose this code to your own use case.
The included Terraform code will deploy 1 PUBLIC Amazon API Gateway (without any authorizer), 1 AWS Lambda Function (python based), 1 DynamoDB Table, and other supporting resources such as IAM roles, policies, etc.
- Terraform installed w/ versions >=
v1.0.10
- Valid AWS Credentials configured locally for Terraform to utilize
- Python >=
3.10.1
- Clone down this repository
git clone git@github.com:spitzzz/demo-persistent-api.git
- cd into the newly cloned repository directory
cd demo-persistent-api
- Initialize Terraform
terraform init
-
Run a
terraform plan
and analyze the results are as you would expect. -
Run a
terraform apply
- typeyes
if the results are as you would expect.
After a successful Terraform apply, you should receive the api_invoke_url
as an output in the result.
- Using the
api_invoke_url
open a tool such as Postman that you can use to build a request body.
Build a POST
request with the body
of the request following this format:
{
"id": string || number,
"date": string,
"type": string,
"data": string
}
Example Object:
{
"id": 1,
"date": "2022-01-02T23:48:59+0000",
"type": "info",
"data": "test"
}
The current example lambda code also supports a list of objects like so:
[
{
"id": 1,
"date": "2022-01-02T23:48:59+0000",
"type": "info",
"data": "test"
},
{
"id": 2,
"date": "2022-01-02T23:48:59+0000",
"type": "error",
"data": "uh oh"
},
{
"id": 3,
"date": "2022-01-02T23:48:59+0000",
"type": "info",
"data": "test2"
}
]
Once you are finished testing the API, and want to destroy the resources, run the following commands/steps:
- cd into repository you cloned the code
cd demo-persistent-api
- Run Terraform Destroy
terraform destroy
Please feel free to open a pull request for any improvements, suggestions, or bug fixes you might have.