diff --git a/lambda_functions/laads_submit_climatology_job.py b/lambda_functions/laads_submit_climatology_job.py new file mode 100644 index 0000000..dd6da12 --- /dev/null +++ b/lambda_functions/laads_submit_climatology_job.py @@ -0,0 +1,34 @@ +import datetime +import json +import os + +import boto3 + +batch_client = boto3.client("batch") + + +jobQueue = os.getenv("JOB_QUEUE") +jobDefinition = os.getenv("JOB_DEFINITION") +lasrc_aux = os.getenv("LASRC_AUX_DIR") +laads_token = os.getenv("LAADS_TOKEN") +laads_bucket = os.getenv("LAADS_BUCKET") + + +def handler(event, context): + current_date = datetime.datetime.now() + response = batch_client.submit_job( + jobName="laads_cron", + jobQueue=jobQueue, + jobDefinition=jobDefinition, + containerOverrides={ + "environment": [ + {"name": "LASRC_AUX_DIR", "value": lasrc_aux}, + {"name": "LAADS_TOKEN", "value": laads_token}, + {"name": "LAADS_BUCKET", "value": laads_bucket}, + {"name": "CLIM_YEAR", "value": current_date.year}, + {"name": "CLIM_MONTH", "value": current_date.month}, + ], + "command": ["sh", "-c", "climatologies.sh"], + }, + ) + return response diff --git a/stack/hlsconstructs/batch_cron.py b/stack/hlsconstructs/batch_cron.py index a05853d..f32dbf9 100644 --- a/stack/hlsconstructs/batch_cron.py +++ b/stack/hlsconstructs/batch_cron.py @@ -7,8 +7,6 @@ class BatchCron(Lambda): - """AWS Batch Event Construct to Run Batch Jobs on a Cron using Lambda.""" - def __init__( self, scope: core.Construct, @@ -20,32 +18,30 @@ def __init__( timeout: int = 10, **kwargs, ) -> None: + if "code_file" not in kwargs: + jobdef = job.job.ref + env_str = aws_env(env) + code_str = f""" + import boto3 + import json + batch_client = boto3.client('batch') - jobdef = job.job.ref - - env_str = aws_env(env) - - code_str = f""" - import boto3 - import json - batch_client = boto3.client('batch') - - def handler(event, context): - print(event) - print({env_str}) - response = batch_client.submit_job( - jobName="{id}-job", - jobQueue="{queue}", - jobDefinition="{jobdef}", - containerOverrides={{ - 'environment': {env_str} - }} - ) - print(response) - return response - """ - code_str = align(code_str) - self.code = aws_lambda.InlineCode(code=code_str) + def handler(event, context): + print(event) + print({env_str}) + response = batch_client.submit_job( + jobName="{id}-job", + jobQueue="{queue}", + jobDefinition="{jobdef}", + containerOverrides={{ + 'environment': {env_str} + }} + ) + print(response) + return response + """ + code_str = align(code_str) + self.code = aws_lambda.InlineCode(code=code_str) super().__init__(scope, id, **kwargs)