diff --git a/.gitignore b/.gitignore index 75d74f2..cf86ab9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ tests/fixtures __pycache__ samconfig.toml +packaged.yaml diff --git a/functions/training_info_getter/__init__.py b/functions/training_info_getter/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/functions/training_info_getter/app.py b/functions/training_info_getter/app.py new file mode 100644 index 0000000..1bd68d2 --- /dev/null +++ b/functions/training_info_getter/app.py @@ -0,0 +1,11 @@ +import boto3 + +from common.json_utils import clean_json + +sagemaker_client = boto3.client('sagemaker') + +def lambda_handler(event, context): + training_name = event['training_info']['TrainingJobName'] + training_info = sagemaker_client.describe_training_job(TrainingJobName=training_name) + event['training_info'] = clean_json(training_info) + return event diff --git a/functions/training_info_getter/requirements.txt b/functions/training_info_getter/requirements.txt new file mode 100644 index 0000000..e69de29 diff --git a/layers/utils/__init__.py b/layers/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/layers/utils/common/__init__.py b/layers/utils/common/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/layers/utils/common/json_utils.py b/layers/utils/common/json_utils.py new file mode 100644 index 0000000..75931e7 --- /dev/null +++ b/layers/utils/common/json_utils.py @@ -0,0 +1,12 @@ +import json +from datetime import datetime, date + +def json_serial(obj): + '''JSON serializer for objects not serializable by default json code''' + if isinstance(obj, (datetime, date)): + return obj.isoformat() + raise TypeError(f'Type {type(obj)} not serializable') + +def clean_json(obj): + ''' Cleans a json object from datettimes. ''' + return json.loads(json.dumps(obj, default=json_serial)) diff --git a/layers/utils/requirements.txt b/layers/utils/requirements.txt new file mode 100644 index 0000000..e69de29 diff --git a/template.yaml b/template.yaml index adada36..f19f326 100644 --- a/template.yaml +++ b/template.yaml @@ -180,7 +180,7 @@ Resources: This lambda function gets training results and other information after the training has finished. Properties: - CodeUri: ../common/functions/training_info_getter/ + CodeUri: functions/training_info_getter/ Handler: app.lambda_handler Runtime: python3.8 Timeout: 10 @@ -249,7 +249,7 @@ Resources: Type: AWS::Serverless::LayerVersion Description: Lambda layer containing common utils Properties: - ContentUri: ../common/layers/utils + ContentUri: layers/utils CompatibleRuntimes: - python3.8 Metadata: