{"id":23436805,"url":"https://github.com/lincolnloop/ecs-task","last_synced_at":"2025-04-13T04:41:54.426Z","repository":{"id":57425555,"uuid":"224257573","full_name":"lincolnloop/ecs-task","owner":"lincolnloop","description":"Helper for registering new task definitions on AWS ECS and updating associated services.","archived":false,"fork":false,"pushed_at":"2020-03-06T21:03:51.000Z","size":46,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-10T18:02:38.386Z","etag":null,"topics":["aws","container","ecs","service","task"],"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/lincolnloop.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-11-26T18:16:39.000Z","updated_at":"2021-01-29T15:17:03.000Z","dependencies_parsed_at":"2022-08-29T22:00:16.749Z","dependency_job_id":null,"html_url":"https://github.com/lincolnloop/ecs-task","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lincolnloop%2Fecs-task","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lincolnloop%2Fecs-task/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lincolnloop%2Fecs-task/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lincolnloop%2Fecs-task/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lincolnloop","download_url":"https://codeload.github.com/lincolnloop/ecs-task/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248665783,"owners_count":21142123,"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","container","ecs","service","task"],"created_at":"2024-12-23T13:34:03.276Z","updated_at":"2025-04-13T04:41:54.406Z","avatar_url":"https://github.com/lincolnloop.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Tests](https://github.com/lincolnloop/ecs-task/workflows/Test/badge.svg)](https://github.com/lincolnloop/ecs-task/actions?query=workflow%3ATest)\n\n# `ecs-task`\n\n`ecs-task` is an opinionated, but flexible tool for deploying to [Amazon Web Service's Elastic Container Service](https://aws.amazon.com/ecs/).\n\nIt is built on the following premises:\n\n* ECS Services, load balancers, auto-scaling, etc. are managed elsewhere, e.g. Terraform, Cloudformation, etc.\n* Deploying to ECS is defined as:\n    1. Update task definition with new image tag\n    2. [Optional] Running any number of one-off Tasks, e.g. Django database migrations.\n    3. [Optional] Updating Services to use the new Task Definition.\n    4. [Optional] Updating Cloudwatch Event Targets to use the new Task Definition.\n    5. Deregister old Task Definitions.\n* Applications manage their own Task/Container definitions and can deploy themselves to a pre-defined ECS Cluster.\n* The ability to rollback is important and should be as easy as possible.\n\n# Installation\n\n```\npip install ecs-task\n``` \n\n(Optionally, just copy `ecs_task.py` to your project and install `boto3`).\n\n# Usage\n\nThis module is made up of a single class, `ecs_task.ECSTask` which is designed to be extended in your project. A basic example:\n\n```python\n#!/usr/bin/env python\nfrom ecs_task import ECSTask\n\nclass WebTask(ECSTask):\n    task_definition = {\n        \"family\": \"web\",\n        \"executionRoleArn\": EXECUTION_ROLE_ARN,\n        \"containerDefinitions\": [\n            {\n                \"name\": \"web\",\n                \"image\": \"my_image:{image_tag}\",\n                \"portMappings\": [{\"containerPort\": 8080}],\n                \"cpu\": 1024,\n                \"memory\": 1024,\n            }\n        ],\n    }\n    update_services = [{\"service\": \"web\", \"cluster\": \"my_cluster\",}]\n\nif __name__ == \"__main__\":\n    WebTask().main()\n```\n\nYou could save this as `_ecs/web_dev.py` and then execute it with `python -m _ecs.web_dev --help`\n\n```\nusage: web_dev.py [-h] {deploy,rollback,debug} ...\n\nECS Task\n\npositional arguments:\n  {deploy,rollback,debug}\n    deploy              Register new task definitions using `image_tag`.\n                        Update defined ECS Services, Event Targets, and run\n                        defined ECS Tasks\n    rollback            Deactivate current task definitions and rollback all\n                        ECS Services and Event Targets to previous active\n                        definition.\n    debug               Dump JSON generated for class attributes.\n\noptional arguments:\n  -h, --help            show this help message and exit\n```\n\n## Class attributes\n\nA sub-class of `ECSTask` must include a `task_definition` to do anything. Any other attributes are optional. The following attributes are designed to be a 1-to-1 mapping to an AWS API endpoint via [`boto3`](https://boto3.amazonaws.com/v1/documentation/api/latest/index.html). The values you provide will be passed as keyword arguments to the associated method with the correct Task Definition inserted. Any attribute that takes a list can make multiple calls to the given API.\n\n* `task_definition`: (dict) [`ecs.register_task_definition` docs](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ecs.html#ECS.Client.register_task_definition)\n* `update_services` (list) [`ecs.update_service` docs](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ecs.html#ECS.Client.update_service)\n* `run_tasks` (list) [`ecs.run_task` docs](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ecs.html#ECS.Client.run_task)\n* `events__put_targets` (list) [`events.put_targets` docs](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/events.html#EventBridge.Client.put_targets)\n\nA few additional attributes are available:\n\n* `active_task_count`: (int) the number of task definitions to keep active after a deployment. Default is `10`.\n* `sns_notification_topic_arn`: (str) the ARN for an SNS topic which will receive a message whenever an AWS API call is executed. This can be used to trigger notifications or perform additional tasks related to the deployment. The message is in the format:\n\n    ```python\n      {\n        \"client\": client,  # boto3 client (usually \"ecs\")\n        \"method\": method,  # method called (e.g., \"update_service\")\n        \"input\": kwargs,   # method input as a dictionary\n        \"result\": result   # results from AWS API\n      }\n    ```\n* `notification_method_blacklist_regex` (re.Pattern) a pattern of methods to avoid sending notifications for. Default is `re.compile(r\"^describe_|get_|list_|.*register_task\")`\n\n## Command Interface\n\nEach class is intended to be \"executable\" by calling `.main()`. Multiple class instances can be called in a given file by using:\n\n```python\nif __name__ == \"__main__\":\n    for klass in [WebTask, WorkerTask]:\n        klass().main()\n```\n\n### `debug`\n\nJust prints the value of each class attribute to the console. Useful if you're doing some class inheritance and want to verify what you have before running against AWS. \n\n### `deploy`\n\nThe `deploy` subcommand accepts an additional argument, `image_tag` which is used to update any Container Definitions in the task which have the `{image_tag}` placeholder. It will:\n\n1. Register a new Task Definition\n2. Run Tasks (as defined in `run_tasks`)\n3. Update Services (as defined in `update_services`)\n4. Update Event Targets (as defined in `events__put_targets`)\n5. Deregister any active Task Definitions older than `active_task_count` (by default, `10`)\n\n### `rollback`\n\n1. Deregister the latest active Task Definition\n2. Update Services (as defined in `update_services`) with the previous active Task Definition\n3. Update Event Targets (as defined in `events__put_targets`) with the previous active Task Definition\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flincolnloop%2Fecs-task","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flincolnloop%2Fecs-task","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flincolnloop%2Fecs-task/lists"}