This repository contains the Terraform Infrastructure as Code (IaC) to provision and manage the AWS resources required for the Market Financials Lambda project. It automates the setup of ECR repositories, Lambda functions, API Gateway with custom authentication, and necessary permissions in the af-south-1 region.
- βοΈ Complete Infrastructure: Provisions ECR, Lambda, API Gateway, DynamoDB, and IAM roles.
- π Secure Access: Implements a Custom Lambda Authorizer that validates API keys against a DynamoDB table.
- π Automated Deployment: Includes GitHub Actions for planning, applying, and destroying infrastructure.
- π³ Container Support: Configures ECR repositories with lifecycle policies to manage image retention.
- π HTTP API: Sets up an API Gateway (HTTP) to expose the Lambda function securely.
- πͺ΅ Observability: Configured CloudWatch logging for API Gateway requests and Lambda executions.
The Terraform scripts provision the following AWS resources:
- Amazon ECR: Two mutable repositories with lifecycle policies to keep only the last 5 images:
market-financials: For the main application logic.market-financials-auth: For the custom authorizer function.
- AWS Lambda:
market_financials_function: The main application deployed from ECR (512MB memory).market_financials_auth_function: The custom authorizer deployed from ECR, configured with the DynamoDB table name.
- Amazon DynamoDB: A
PAY_PER_REQUESTtable namedMarketFinancialsAuthKeys(Hash Key:ApiKey) to store valid API keys. - Amazon API Gateway: An HTTP API (
MarketFinancialsGW) with aprodstage and auto-deployment enabled.- Route:
POST /market-financials. - Auth: Protected by a Custom Request Authorizer using the
market_financials_auth_function(Expectsx-api-keyheader).
- Route:
- CloudWatch Logs: Log groups with 7-day retention for both Lambda functions and API Gateway access logs.
- IAM Roles: Roles and policies allowing the Lambdas to execute, the Authorizer to read from DynamoDB, and API Gateway to invoke them.
| Name | Description | Default |
|---|---|---|
region |
AWS Region to deploy resources into. | af-south-1 |
tag |
Project tag used for resource tagging. | market-financials |
After applying the Terraform configuration, the following values are output:
repository_url: The URL of the created Amazon ECR repository (for the main app).api_endpoint: The endpoint of the HTTP API Gateway.base_url: The invoke URL for theprodstage.
This project uses a manual GitHub Actions workflow (workflow_dispatch) to manage the infrastructure state.
When running the workflow manually, you must select the job to run via the job_to_run input:
- terraform-apply: Runs
terraform planfollowed byterraform applyto provision or update resources. - terraform-destroy-apply: Runs
terraform plan -destroyfollowed byterraform destroyto tear down all resources.
- Terraform Plan: Validates code and generates an execution plan.
- Terraform Apply: Applies the changes to the
productionenvironment (auto-approved). - Terraform Destroy Plan/Apply: Handles the safe destruction of infrastructure when selected.
The pipeline uses an S3 backend (market-financials-lambda-terrafom-state) to store the Terraform state file securely.