Package: ecs-task
ecs-taskecs-taskis an opinionated, but flexible tool for deploying toAmazon Web Service's Elastic Container Service.It is built on the following premises:ECS Services, load balancers, auto-scaling, etc. are managed elsewhere, e.g. Terraform, Cloudformation, etc.Deploying to ECS is defined as:Update task definition with new image tag[Optional] Running any number of one-off Tasks, e.g. Django database migrations.[Optional] Updating Services to use the new Task Definition.[Optional] Updating Cloudwatch Event Targets to use the new Task Definition.Deregister old Task Definitions.Applications manage their own Task/Container definitions and can deploy themselves to a pre-defined ECS Cluster.The ability to rollback is important and should be as easy as possible.Installationpip install ecs-task(Optionally, just copyecs_task.pyto your project and installboto3).UsageThis module is made up of a single class,ecs_task.ECSTaskwhich is designed to be extended in your project. A basic example:#!/usr/bin/env pythonfromecs_taskimportECSTaskclassWebTask(ECSTask):task_definition={"family":"web","executionRoleArn":EXECUTION_ROLE_ARN,"containerDefinitions":[{"name":"web","image":"my_image:{image_tag}","portMappings":[{"containerPort":8080}],"cpu":1024,"memory":1024,}],}update_services=[{"service":"web","cluster":"my_cluster",}]if__name__=="__main__":WebTask().main()You could save this as_ecs/web_dev.pyand then execute it withpython -m _ecs.web_dev --helpusage: web_dev.py [-h] {deploy,rollback,debug} ...

ECS Task

positional arguments:
  {deploy,rollback,debug}
    deploy              Register new task definitions using `image_tag`.
                        Update defined ECS Services, Event Targets, and run
                        defined ECS Tasks
    rollback            Deactivate current task definitions and rollback all
                        ECS Services and Event Targets to previous active
                        definition.
    debug               Dump JSON generated for class attributes.

optional arguments:
  -h, --help            show this help message and exitClass attributesA sub-class ofECSTaskmust include atask_definitionto do anything. Any other attributes are optional. The following attributes are designed to be a 1-to-1 mapping to an AWS API endpoint viaboto3. 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.task_definition: (dict)ecs.register_task_definitiondocsupdate_services(list)ecs.update_servicedocsrun_tasks(list)ecs.run_taskdocsevents__put_targets(list)events.put_targetsdocsA few additional attributes are available:active_task_count: (int) the number of task definitions to keep active after a deployment. Default is10.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:{"client":client,# boto3 client (usually "ecs")"method":method,# method called (e.g., "update_service")"input":kwargs,# method input as a dictionary"result":result# results from AWS API}notification_method_blacklist_regex(re.Pattern) a pattern of methods to avoid sending notifications for. Default isre.compile(r"^describe_|get_|list_|.*register_task")Command InterfaceEach class is intended to be "executable" by calling.main(). Multiple class instances can be called in a given file by using:if__name__=="__main__":forklassin[WebTask,WorkerTask]:klass().main()debugJust 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.deployThedeploysubcommand accepts an additional argument,image_tagwhich is used to update any Container Definitions in the task which have the{image_tag}placeholder. It will:Register a new Task DefinitionRun Tasks (as defined inrun_tasks)Update Services (as defined inupdate_services)Update Event Targets (as defined inevents__put_targets)Deregister any active Task Definitions older thanactive_task_count(by default,10)rollbackDeregister the latest active Task DefinitionUpdate Services (as defined inupdate_services) with the previous active Task DefinitionUpdate Event Targets (as defined inevents__put_targets) with the previous active Task Definition