How can I retrieve Glue Workflow parameters from within a glue job?
I have an AWS Glue job of type "python shell" that is triggered periodically from within a glue workflow.
The job's code is to be reused from within a large number of different workflows so I'm looking to retrieve workflow parameters to eliminate the need for redundant jobs.
The AWS Developers guide provides the following tutorial: https://docs.aws.amazon.com/glue/latest/dg/workflow-run-properties-code.html
But I've been unsuccessful in getting the sample code to execute without triggering errors. I suspect that this example may only apply to the scala/ pyspark jobs and not to python shell jobs.
I've tried the following code from within the relevant job
import sys
import boto3
from awsglue.utils import getResolvedOptions
args = getResolvedOptions(sys.argv, ['JOB_NAME','WORKFLOW_NAME', 'WORKFLOW_RUN_ID'])
workflow_name = args['WORKFLOW_NAME']
workflow_run_id = args['WORKFLOW_RUN_ID']
workflow_params = glue_client.get_workflow_run_properties(Name=workflow_name,
RunId=workflow_run_id)["RunProperties"]
print(workflow_name, workflow_run_id, workflow_params)
When I trigger the workflow on demand I receive the following error messages:
> Traceback (most recent call last):
> File "/tmp/runscript.py", line 115, in <module>
> runpy.run_path(temp_file_path, run_name='__main__')
> File "/usr/local/lib/python3.6/runpy.py", line 263, in run_path
> pkg_name=pkg_name, script_name=fname)
> File "/usr/local/lib/python3.6/runpy.py", line 96, in _run_module_code
> mod_name, mod_spec, pkg_name, script_name)
> File "/usr/local/lib/python3.6/runpy.py", line 85, in _run_code
> exec(code, run_globals)
> File "/tmp/glue-python-scripts-w4fbwl3n/map_etl_python_shell_test_env.py", line 10, in <module>
> File "/glue/lib/awsglue/utils.py", line 10, in getResolvedOptions
> parsed, extra = parser.parse_known_args(args)
> File "/usr/local/lib/python3.6/argparse.py", line 1766, in parse_known_args
> namespace, args = self._parse_known_args(args, namespace)
> File "/usr/local/lib/python3.6/argparse.py", line 2001, in _parse_known_args
', '.join(required_actions))
> File "/usr/local/lib/python3.6/argparse.py", line 2393, in error
> self.exit(2, _('%(prog)s: error: %(message)s\n') % args)
> File "/usr/local/lib/python3.6/argparse.py", line 2380, in exit
> _sys.exit(status)
> SystemExit: 2
>
> During handling of the above exception, another exception occurred:
>
> Traceback (most recent call last):
> File "/tmp/runscript.py", line 134, in <module>
> raise e_type(e_value).with_tracsback(new_stack)
> AttributeError: 'SystemExit' object has no attribute 'with_tracsback'
The boto3
library provides you an interesting function
glue = boto3.client(service_name='glue', region_name="my-region")
job = glue.get_job(JobName="my-job-name")
default_parameters = job['Job']['DefaultArguments']
default_parameters[u'--my-parameter']
In this way you should be able to manipulate Glue Job arguments through default_parameter
. I'm not sure it works straightaway in the Glue Job, but an external script should be able to handle the Glue Job arguments.
Running the above code in a workflow gives the error:
usage: workflow-test.py [-h] --JOB_NAME JOB_NAME --WORKFLOW_NAME WORKFLOW_NAME
--WORKFLOW_RUN_ID WORKFLOW_RUN_ID
workflow-test.py: error: the following arguments are required: --JOB_NAME
followed by what you have pasted above.
Seems the AWS documentation is outdated and the JOB_NAME
parameter is not used in jobs that are part of a workflow.
You'll get the workflow parameters with:
args = getResolvedOptions(sys.argv, ['WORKFLOW_NAME', 'WORKFLOW_RUN_ID'])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.