Skip to content

This repo contains a simple web application that runs on AWS Lambda and prints the request header, method, and body.

Notifications You must be signed in to change notification settings

GerrardE/terraform-lambda

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

terraform-lambda

CircleCI

Getting Started

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.

Architecture Diagram

Architecture diagram

Required Technologies

This application makes use of the following technologies:

Installation

1. Installation Steps

These instructions assumes that you have a working installation of git and nodejs. See the instructions git here and node here to install them.

LOCAL

Provision the necessary services needed for running the application locally:

  1. You will need AWS credentials to run this app:

    • Create an account on AWS for free here
    • Install the AWS CLI following the instructions here
    • Create your AWS security credentials(access key id & secret access key) here
  2. Follow the instructions here to install terraform cli on your PC

  3. Clone this repository to your PC and

  4. Run yarn or npm install in the root folder to install dependencies

  5. Checkout into the terraform directory and run the following commands:

    • terraform init to initialize terraform locally
    • terraform plan -var "provider_access_key=<awsaccesskeyidhere>" -var "provider_secret_key=<awssecretaccesskeyhere>" to create a terraform plan
    • terraform apply -var "provider_access_key=<awsaccesskeyidhere>" -var "provider_secret_key=<awssecretaccesskeyhere>" and then when prompted to enter a value, type in yes to create the necessary resources on aws.
  6. 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>
    
  7. 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.

  8. 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 command
    • terraform destroy -var "provider_access_key=<awsaccesskeyidhere>" -var "provider_secret_key=<awssecretaccesskeyhere>" and then when prompted to enter a value, type in yes to clean up the resources on aws.

CICD

Cicd

  1. Create a git repository here and push the source code to the repository using the instructions from the repository creation result
  2. You will need AWS credentials to run this app:
    • Create an account on AWS for free here
    • Create your AWS security credentials(access key id & secret access key) here
  3. Follow the instructions at the Terraform Cloud docs:
    • Create the workspace: (In the Advanced options tab set the Terraform Working Directory to terraform)
    • link the git repository from the first step and setup aws credentials as variables like so:
      provider_access_key = `<awsaccesskeyidhere>`
      provider_secret_key = `<awssecretaccesskeyhere>`
      
  4. Using these instructions:
    • Plan infrastructure changes
    • Apply planned changes: The outputs of from this step would be used to configure the CircleCi project.
  5. 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>
  6. Make changes in the code on the repository and watch the pipeline initialize and complete the test_build_deploy process.

Tests

This project contains some tests. Follow these steps to run the tests locally.

  1. Run yarn test in the root directory

Cost Implication

  • 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.

References

About

This repo contains a simple web application that runs on AWS Lambda and prints the request header, method, and body.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages