{"id":15365440,"url":"https://github.com/epiphone/lambda-terraform-analytics","last_synced_at":"2025-03-27T20:42:29.119Z","repository":{"id":151575037,"uuid":"116951877","full_name":"epiphone/lambda-terraform-analytics","owner":"epiphone","description":"A simple serverless analytics service deployed with Terraform","archived":false,"fork":false,"pushed_at":"2018-01-30T10:06:47.000Z","size":573,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-01T23:14:16.289Z","etag":null,"topics":["analytics","aws-lambda","aws-lambda-python","aws-terraform","sample-app","serverless","serverless-applications","terraform"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/epiphone.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-10T11:58:31.000Z","updated_at":"2018-01-15T11:39:47.000Z","dependencies_parsed_at":null,"dependency_job_id":"d4991fc0-8cf7-4656-8863-3f20751cc389","html_url":"https://github.com/epiphone/lambda-terraform-analytics","commit_stats":{"total_commits":26,"total_committers":2,"mean_commits":13.0,"dds":"0.038461538461538436","last_synced_commit":"b569ee6a7bcb56c8c3c41b875edd4859350010ee"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/epiphone%2Flambda-terraform-analytics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/epiphone%2Flambda-terraform-analytics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/epiphone%2Flambda-terraform-analytics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/epiphone%2Flambda-terraform-analytics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/epiphone","download_url":"https://codeload.github.com/epiphone/lambda-terraform-analytics/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245924297,"owners_count":20694726,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["analytics","aws-lambda","aws-lambda-python","aws-terraform","sample-app","serverless","serverless-applications","terraform"],"created_at":"2024-10-01T13:14:46.217Z","updated_at":"2025-03-27T20:42:29.097Z","avatar_url":"https://github.com/epiphone.png","language":"Python","readme":"# Simple Lambda/Terraform analytics service\n\n[![Build Status](https://travis-ci.org/epiphone/lambda-terraform-analytics.svg?branch=master)](https://travis-ci.org/epiphone/lambda-terraform-analytics) [![codecov](https://codecov.io/gh/epiphone/lambda-terraform-analytics/branch/master/graph/badge.svg)](https://codecov.io/gh/epiphone/lambda-terraform-analytics)\n\nSets up A) a general messaging topic in which to hook up additional event listeners and B) a little analytics service that feeds all events into a database. The database can be further plugged into a dashboard frontend like [Superset](https://github.com/apache/incubator-superset) or [Metadash](https://github.com/metabase/metabase).\n\nFeatures include\n- Terraform-managed Lambdas (inspired by [this blog post](https://medium.com/build-acl/aws-lambda-deployment-with-terraform-24d36cc86533)) + a helper script for most common management tasks\n- Multi-environment deployment with separate Terraform states\n- Local tests using [moto](https://github.com/spulec/moto) to mock AWS endpoints\n- Keeping development dependencies out of deployed Lambda zips with pipenv\n- Overriding dependencies with Lambda-compatible precompiled packages on deployment (e.g. [`precompiled/psycopg2/`](precompiled/psycopg2))\n\n![Architecture](doc/cloudcraft.png)\n\nThe infra consists of the following Terraform modules:\n- `shared` sets up a Postgres micro-instance and a security group allowing external access. As the name suggests, we're sharing one database with all stages in order to not exceed AWS free tier's 1 micro instance limit; instead stage separation happens at schema level.\n- `messaging` sets up an SNS topic for events and an IAM user with publishing permissions. Event producers should use this IAM user's API keys.\n- `analytics_db` sets up stage-specific schemas and roles in the shared database.\n- `analytics_queue` sets up an SQS queue consuming the events SNS topic. A scheduled Cloudwatch event triggers a consumer Lambda function every 5 minutes. The consumer pulls events out of the SQS queue and fans them out to a worker Lambda which in turn feeds events into the shared database.\n\n## Dependencies\n\n- AWS Command Line Interface\n- [invoke](https://github.com/pyinvoke/invoke)\n- pip-env \u003e= 8.3.0\n- terraform\n- [awslogs](https://github.com/jorgebastida/awslogs) (optional)\n- An S3 bucket for [storing Terraform remote state](https://www.terraform.io/docs/state/remote.html)\n\n## Set up infrastructure\n\nFirst set up the shared environment:\n\n1. Navigate to the shared environment directory `infrastructure/shared`.\n2. Create a `terraform.tfvars` [secret variables file](https://www.terraform.io/intro/getting-started/variables.html#from-a-file). Check [`infrastructure/shared/terraform.tfvars.sample`](infrastructure/shared/terraform.tfvars.sample) for an example. These files should naturally be kept outside version control.\n3. Run `terraform init` to set up a Terraform working directory. You'll be prompted for the name of your remote state bucket. Alternatively you can define the bucket with a `-backend-config='bucket=[BUCKET NAME]'` argument.\n4. Run `terraform apply` to build the infra.\n\nNow with the shared infra set up, you can provision individual stages. For example to set up `dev`:\n\n1. First build the Lambda functions: `inv build`.\n2. Navigate to the environment directory: e.g. `cd infrastructure/dev`.\n3. Repeat the above steps 2-4 to set up the stage-specific resources.\n4. Initialize the database: `inv init-db --env dev`.\n\nAnd we're set! Replace `dev` with `staging`, `prod`, etc to set up additional stages.\n\n## Tasks\n\nVarious management tasks are defined in [`tasks.py`](tasks.py). The default environment (`--env`) and AWS profile can be configured in [`invoke.yaml`](invoke.yaml).\n\nRun `inv --list` to see a summary of all available tasks. The most important tasks are as follows:\n\n### Build\n\n`inv build --func [FUNCTION]`: Build a function. Builds all if `--func` is not specified.\n\n### Test\n\n`inv test --func [FUNCTION]`: Run a function's tests. Tests all functions if `--func` is not specified.\n\n### Invoke\n\n`inv invoke [FUNCTION] --env [ENV] --payload [PAYLOAD]`: Invoke a deployed function.\n\nExample:\n```bash\ninv invoke analytics_worker --env staging --payload '[{\"event_id\": 12}]'\n```\n\n### Update\n\n`inv update [FUNCTION] --env [ENV]`: Quickly update function code without rebuilding dependencies.\n\n## Limitations\n\nDue to [a bug in the Terraform Postgres provider](https://github.com/terraform-providers/terraform-provider-postgresql/issues/16), changing db password variables doesn't actually result in a password update. As a workaround you can manually `DROP ROLE ...` via `psql` and re-apply terraform.\n\n## TODO\n\n- Local invocation\n- Set up alembic or similar to manage DB migrations\n- An .ignore file to configure files kept out of Lambda zips\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fepiphone%2Flambda-terraform-analytics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fepiphone%2Flambda-terraform-analytics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fepiphone%2Flambda-terraform-analytics/lists"}