Setting the file. One moment. Framework Processing · AWS AI ML · aws/agent-toolkit-for-aws · Skills Docs93
Creating Amazon Aurora Db Cluster With Instances
Shieldadvanced
Resilience Program Design
Connecting Lambda To DynamoDB
(opens in a new tab)
references/processing-jobs/code_templates/framework_processing.py
Python·101 lines·3 KB
FrameworkProcessor, logs_for_processing_job
11from sagemaker.core.shapes import (
12 ProcessingInput,
13 ProcessingOutput,
14 ProcessingS3Input,
15 ProcessingS3Output,
16)
17
18set_attribution(Attribution.SAGEMAKER_AGENT_PLUGIN)
19
20REGION = ""
21ROLE_ARN = "[ROLE_ARN]"
22FRAMEWORK = "[FRAMEWORK]"
23FRAMEWORK_VERSION = ""
24PYTHON_VERSION = ""
25SOURCE_DIR = "[SOURCE_DIR]"
26ENTRY_SCRIPT = "[ENTRY_SCRIPT]"
27REQUIREMENTS = "[REQUIREMENTS]"
28CUSTOM_IMAGE_URI = ""
29INPUT_S3_URI = "[INPUT_S3_URI]"
30OUTPUT_S3_URI = "[OUTPUT_S3_URI]"
31INSTANCE_TYPE = "[INSTANCE_TYPE]"
32INSTANCE_COUNT = 1
33MAX_RUNTIME_SECONDS = 86400
34BASE_JOB_NAME = "[BASE_JOB_NAME]"
35ARGUMENTS = []
36WAIT_FOR_COMPLETION = False
37
38boto_session = boto3.Session(region_name=REGION or None)
39region = boto_session.region_name
40sm_client = boto_session.client("sagemaker")
41sm_session = Session(boto_session=boto_session, sagemaker_client=sm_client)
42role = ROLE_ARN or get_execution_role()
43image_uri = CUSTOM_IMAGE_URI or image_uris.retrieve(
44 framework=FRAMEWORK,
45 region=region,
46 version=FRAMEWORK_VERSION or None,
47 py_version=PYTHON_VERSION or None,
48 instance_type=INSTANCE_TYPE,
49)
50print(f"Resolved processing image URI: {image_uri}")
51
52# Cell 3: Build and submit the processor
53processor = FrameworkProcessor(
54 image_uri=image_uri,
55 role=role,
56 command=["python3"],
57 instance_type=INSTANCE_TYPE,
58 instance_count=INSTANCE_COUNT,
59 max_runtime_in_seconds=MAX_RUNTIME_SECONDS,
60 base_job_name=BASE_JOB_NAME,
61 sagemaker_session=sm_session,
62)
63processor.run(
64 code=ENTRY_SCRIPT,
65 source_dir=SOURCE_DIR,
66 requirements=REQUIREMENTS or None,
67 inputs=[
68 ProcessingInput(
69 input_name="input",
70 s3_input=ProcessingS3Input(
71 s3_uri=INPUT_S3_URI,
72 local_path="/opt/ml/processing/input",
73 s3_data_type="S3Prefix",
74 s3_input_mode="File",
75 ),
76 )
77 ],
78 outputs=[
79 ProcessingOutput(
80 output_name="output",
81 s3_output=ProcessingS3Output(
82 s3_uri=OUTPUT_S3_URI,
83 local_path="/opt/ml/processing/output",
84 s3_upload_mode="EndOfJob",
85 ),
86 )
87 ],
88 arguments=ARGUMENTS,
89 wait=False,
90 logs=False,
91)
92
93# Cell 4: Report status and optionally wait
94job_name = processor.latest_job.processing_job_name
95job_arn = sm_client.describe_processing_job(ProcessingJobName=job_name)["ProcessingJobArn"]
96print(f"Processing Job Name: {job_name}")
97print(f"Processing Job ARN: {job_arn}")
98
99if WAIT_FOR_COMPLETION:
100 logs_for_processing_job(sm_session, job_name, wait=True)
101 print(f"Output: {OUTPUT_S3_URI}")