This repo contains a simple web application that runs on AWS Lambda and prints the request header, method, and body.
It is integrated with CircleCI CI/CD and Terraform for Infractructure as code (IaC) management using AWS as the cloud solutions provider.
This application makes use of the following technologies:
- AWS API Gateway for proxying lambda requests
- AWS Lambda for serving Lambda functions
- AWS S3 for static file storage
- Typescript for Javascript type safety
- Node for creating the APIs
- Circle CI for continuous integration and delivery
- Terraform as an infrastructure as code tool.
These instructions assumes that you have a working installation of git
and nodejs
. See the instructions git here and node here to install them.
Provision the necessary services needed for running the application locally:
-
You will need AWS credentials to run this app:
-
Follow the instructions here to install
terraform
cli on your PC -
Clone this repository to your PC and
-
Run
yarn
ornpm install
in the root folder to install dependencies -
Checkout into the
terraform
directory and run the following commands:terraform init
to initialize terraform locallyterraform plan -var "provider_access_key=<awsaccesskeyidhere>" -var "provider_secret_key=<awssecretaccesskeyhere>"
to create a terraform planterraform apply -var "provider_access_key=<awsaccesskeyidhere>" -var "provider_secret_key=<awssecretaccesskeyhere>"
and then when prompted to enter a value, type inyes
to create the necessary resources on aws.
-
The result of the previous step on the cli would be:
artifact = <artifact object key> artifact_store = <artifact store id> function_name = <name of the function> function_url = <The endpoint i.e function invocation url>
-
The endpoints from above can be hit using the following format:
curl --header "Content-Type: application/json" --data '{"username":"xyz","password":"xyz"}' <function_2_url>
App Endpoints: POST endpoint -
https://lpzv90eo0m.execute-api.us-east-1.amazonaws.com/prod-terraform-lambda/createEvent
GET endpoint -https://lpzv90eo0m.execute-api.us-east-1.amazonaws.com/prod-terraform-lambda/getEvent
Or using Postman.
-
To clean up the stack: Run the following commands
terraform plan -destroy
would let you know resources about to be cleaned and if you are okay, proceed to clean up with the following commandterraform destroy -var "provider_access_key=<awsaccesskeyidhere>" -var "provider_secret_key=<awssecretaccesskeyhere>"
and then when prompted to enter a value, type inyes
to clean up the resources on aws.
- Create a git repository here and push the source code to the repository using the instructions from the repository creation result
- You will need AWS credentials to run this app:
- Follow the instructions at the Terraform Cloud docs:
- Create the workspace: (In the
Advanced options
tab set theTerraform Working Directory
toterraform
) - link the git repository from the first step and setup aws credentials as variables like so:
provider_access_key = `<awsaccesskeyidhere>` provider_secret_key = `<awssecretaccesskeyhere>`
- Create the workspace: (In the
- Using these instructions:
- Plan infrastructure changes
- Apply planned changes: The outputs of from this step would be used to configure the CircleCi project.
- Create a Circle CI project using the docs here.
NB: skip the creation of a circle ci config file
.circleci/config.yml
since we already have one here. Add the following to the project environment variables:- AWS_ACCESS_KEY_ID:
<awsaccesskeyidhere>
- AWS_ACCOUNT_ID:
<awsaccountidhere>
- AWS_DEFAULT_REGION:
us-east-1
- AWS_REGION:
us-east-1
- AWS_SECRET_ACCESS_KEY:
<awssecretaccesskeyhere>
- F1:
<function_name>
- S3_BUCKET:
<artifact store>
- S3_BUCKET_KEY:
<artifact object key>
- AWS_ACCESS_KEY_ID:
- Make changes in the code on the repository and watch the pipeline initialize and complete the
test_build_deploy
process.
This project contains some tests. Follow these steps to run the tests locally.
- Run
yarn test
in the root directory
- AWS Lambda free tier, 1 million always free requests monthly
- AWS API Gateway free tier, 1 million free api calls monthly for 1 year
- 10 always free AWS CloudWatch custom metrics, here we have just 3 custom metrics
- 5 GB AWS S3 bucket free for 1 year
- CircleCI provides 6000 build minutes/month for free always
In conclusion, it would not cost anything to get this simple app running for a whole year, as long as we remain within free tier limits. For us to scale, we need to consider cost implications of moving to the next tier for each of the service considered.