{"id":26584120,"url":"https://github.com/chrispsheehan/weighted-target-traffic","last_synced_at":"2025-03-23T09:19:04.829Z","repository":{"id":258279187,"uuid":"871262181","full_name":"chrispsheehan/weighted-target-traffic","owner":"chrispsheehan","description":"Incrementally move traffic from Lambda to ECS using weighted routing in AWS. Manage deployments and resources with Terraform","archived":false,"fork":false,"pushed_at":"2024-10-29T11:36:20.000Z","size":203,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-29T13:25:47.565Z","etag":null,"topics":["aws","ecs","lambda","terraform","weighted-routing"],"latest_commit_sha":null,"homepage":"","language":"HCL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chrispsheehan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2024-10-11T15:33:30.000Z","updated_at":"2024-10-29T11:36:24.000Z","dependencies_parsed_at":"2024-10-21T13:37:48.238Z","dependency_job_id":null,"html_url":"https://github.com/chrispsheehan/weighted-target-traffic","commit_stats":null,"previous_names":["chrispsheehan/weighted-target-traffic"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrispsheehan%2Fweighted-target-traffic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrispsheehan%2Fweighted-target-traffic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrispsheehan%2Fweighted-target-traffic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrispsheehan%2Fweighted-target-traffic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chrispsheehan","download_url":"https://codeload.github.com/chrispsheehan/weighted-target-traffic/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245078812,"owners_count":20557362,"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":["aws","ecs","lambda","terraform","weighted-routing"],"created_at":"2025-03-23T09:19:04.168Z","updated_at":"2025-03-23T09:19:04.805Z","avatar_url":"https://github.com/chrispsheehan.png","language":"HCL","funding_links":[],"categories":[],"sub_categories":[],"readme":"# weighted-target-traffic\n\nSend weighted (percentage of) traffic to different aws resources. \n\nIn this use case we can incrementally move endpoints to ECS from Lambda.\n\n![Infrastructure](docs/infra.drawio.png)\n\n`Deploy` workflow - push on `main` trigger\n\n1. **validate** Check terraform code for any errors.\n2. **repo** Setup ECR and S3 repositories.\n3. **network** Apply vpc link and api gateway ingress along with load balancer and rules.\n4. **image** Build image if changes to `src/*` detected\n5. **code** Build Lambda code as zip if changes to `src/*` detected\n6. **ecs** Apply ECS cluster, service and task. Rolling deployment only.\n7. **lambda** Apply Lambda.\n\n\n`Destroy` workflow - manual trigger\n\n1. **ecs** Destroy ecs service and task.\n2. **lambda** Destroy lambda.\n3. **network** Destroy vpc link and api gateway ingress resources.\n4. **repo** Destroy ecr, images and lambda s3 zips.\n\n## path weighting rules\n\n- Passed in as `terraform apply -var='weighted_rules={}'` default value json shown below.\n- For each path define weighting to lambda and/or ecs.\n- In the below:\n  - `host` will be weighted 50/50 to ecs/lambda.\n  - `small-woodland-creature` will go to ecs only.\n  - `ice-cream-flavour` will go to lambda only.\n\n```hcl\n{\n  \"host\" = {\n    ecs_percentage_traffic    = 50\n    lambda_percentage_traffic = 50\n    priority                  = 300\n  },\n  \"small-woodland-creature\" = {\n    ecs_percentage_traffic    = 100\n    lambda_percentage_traffic = 0\n    priority                  = 200\n  },\n  \"ice-cream-flavour\" = {\n    ecs_percentage_traffic    = 0\n    lambda_percentage_traffic = 100\n    priority                  = 100\n  }\n}\n```\n\n- Default values are set with `terraform apply -var='default_weighting'` the below example sends all traffic to lambda.\n\n```hcl\n{\n  ecs_percentage_traffic    = 0\n  lambda_percentage_traffic = 100\n}\n```\n\n## terraform\n\nRequired deployment iam privileges.\n\n```json\n[\n    \"dynamodb:*\", \n    \"s3:*\", \n    \"ecr:*\", \n    \"iam:*\", \n    \"ecs:*\", \n    \"ec2:*\", \n    \"elasticloadbalancing:*\", \n    \"logs:*\", \n    \"cloudwatch:*\", \n    \"apigateway:*\", \n    \"lambda:*\"\n]\n```\n\n## ci config\n\nRequired github action variables.\n- `AWS_ACCOUNT_ID`\n- `AWS_REGION`\n- `AWS_ROLE` role with above deployment privileges\n\n\n## additional spice\n\nUnable to destroy lambda security group (sg). Terraform attempts to delete the sg and is blocked which causes a timeout failure.\n\n**Cause**: lambda is deployed within a VPC by specifying `vpc_config` in the `aws_lambda_function` terraform block.\n\n**Reason**: elastic network interfaces (enis) associated with the sg are still in use on the lambda. This blocks a deletion.\n\n**Solution**: remove the `vpc_config` from the lambda via cli prior to a destroy. This is done via `just detach-function`.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrispsheehan%2Fweighted-target-traffic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchrispsheehan%2Fweighted-target-traffic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrispsheehan%2Fweighted-target-traffic/lists"}