import logging import os import json import boto3 from dotenv import load_dotenv import deploy logging.basicConfig(level=logging.INFO) # Load env file if it exists load_dotenv(verbose=True) environment = os.environ.get('Environment', 'dev') aws_region = os.environ.get('AWS_REGION', 'us-east-1') service_name = os.environ.get('SERVICE_NAME') task_family = os.environ.get('TASK_FAMILY') output_file_path = os.environ.get('OUTPUT_FILE_PATH', '') def main(): """ Main entrypoint function This small script exists to create and register task definitions outside the context of an always-on service-based deployment, for use in situations where tasks will be run ad-hoc or invoked separately from services. """ client = boto3.client('ecs', region_name=aws_region) _, new_task_def = deploy.create_new_task_definition( client, task_family) response = deploy.register_new_task_definition(client, new_task_def) new_task_definition_arn = response['taskDefinition']['taskDefinitionArn'] logging.info('Created {}'.format(new_task_definition_arn)) if output_file_path: write_json_output_file(response, output_file_path) def write_json_output_file(task_definition, file_path): """ Write the arn and revision of the task definition to a JSON file to be read by the calling groovy script. """ with open(f'{file_path}', 'w') as f: json_output = { 'arn': task_definition['taskDefinition']['taskDefinitionArn'], 'revision': task_definition['taskDefinition']['revision'] } f.write(json.dumps(json_output, indent=2)) logging.info(f'Wrote task definition to {file_path}') if __name__ == "__main__": main()