Skip to content

Commit 6967e21

Browse files
committed
Test cfn construct
1 parent 245f2ec commit 6967e21

File tree

3 files changed

+112
-24
lines changed

3 files changed

+112
-24
lines changed

src/sagemaker/serve/builder/model_builder.py

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
import logging
2121
import os
2222
import re
23+
import aws_cdk as cdk
2324

2425
from pathlib import Path
2526

27+
from constructs import Construct
28+
import sagemaker
2629
from sagemaker.enums import Tag
2730
from sagemaker.s3 import S3Downloader
2831

@@ -1369,28 +1372,6 @@ def _optimize_for_hf(
13691372

13701373
return create_optimization_job_args
13711374
return None
1375+
1376+
13721377

1373-
def _optimize_prepare_for_hf(self):
1374-
"""Prepare huggingface model data for optimization."""
1375-
custom_model_path: str = (
1376-
self.model_metadata.get("CUSTOM_MODEL_PATH") if self.model_metadata else None
1377-
)
1378-
if _is_s3_uri(custom_model_path):
1379-
# Remove slash by the end of s3 uri, as it may lead to / subfolder during upload.
1380-
custom_model_path = (
1381-
custom_model_path[:-1] if custom_model_path.endswith("/") else custom_model_path
1382-
)
1383-
else:
1384-
if not custom_model_path:
1385-
custom_model_path = f"/tmp/sagemaker/model-builder/{self.model}"
1386-
download_huggingface_model_metadata(
1387-
self.model,
1388-
os.path.join(custom_model_path, "code"),
1389-
self.env_vars.get("HUGGING_FACE_HUB_TOKEN"),
1390-
)
1391-
1392-
self.pysdk_model.model_data, env = self._prepare_for_mode(
1393-
model_path=custom_model_path,
1394-
should_upload_artifacts=True,
1395-
)
1396-
self.pysdk_model.env.update(env)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from aws_cdk import (
2+
Stack,
3+
aws_sagemaker as sagemaker,
4+
CfnOutput,
5+
App,
6+
)
7+
from constructs import Construct
8+
from aws_cdk.cloudformation_include import CfnInclude
9+
import json
10+
11+
class SageMakerEndpointConfigStack(Stack):
12+
13+
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
14+
super().__init__(scope, construct_id, **kwargs)
15+
16+
# Create SageMaker EndpointConfig
17+
endpoint_config = sagemaker.CfnEndpointConfig(
18+
self, "MyEndpointConfig",
19+
production_variants=[
20+
sagemaker.CfnEndpointConfig.ProductionVariantProperty(
21+
initial_instance_count=1,
22+
instance_type="ml.t2.medium",
23+
model_name="model-name-1348706a967911ef97190653ff4a3db9",
24+
variant_name="AllTraffic",
25+
initial_variant_weight=1.0
26+
)
27+
],
28+
endpoint_config_name="test-endpoint-config-11-20"
29+
)
30+
31+
# Output the EndpointConfig name
32+
CfnOutput(self, "EndpointConfigName", value=endpoint_config.endpoint_config_name)
33+
34+
app = App()
35+
stack = SageMakerEndpointConfigStack(app, "SageMakerEndpointConfigStack")
36+
37+
# Synthesize the stack to a CloudFormation template
38+
cfn_template = app.synth().get_stack_by_name(stack.stack_name).template
39+
40+
# Save the CloudFormation template as a JSON file
41+
with open("sagemaker_endpoint_config_template.json", "w") as f:
42+
json.dump(cfn_template, f, indent=2)
43+
print("cccccc")
44+
app.synth()
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from aws_cdk import (
2+
Stack,
3+
aws_sagemaker as sagemaker,
4+
CfnOutput,
5+
App,
6+
)
7+
from constructs import Construct
8+
import json
9+
10+
class SageMakerEndpointStack(Stack):
11+
12+
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
13+
super().__init__(scope, construct_id, **kwargs)
14+
15+
# Create SageMaker EndpointConfig
16+
endpoint_config = sagemaker.CfnEndpointConfig(
17+
self, "MyEndpointConfig",
18+
production_variants=[
19+
sagemaker.CfnEndpointConfig.ProductionVariantProperty(
20+
initial_instance_count=1,
21+
instance_type="ml.t2.medium",
22+
model_name="model-name-1348706a967911ef97190653ff4a3db9",
23+
variant_name="AllTraffic",
24+
initial_variant_weight=1.0
25+
)
26+
],
27+
endpoint_config_name="test-endpoint-config-11-20-001"
28+
)
29+
30+
# Create SageMaker Endpoint
31+
endpoint = sagemaker.CfnEndpoint(
32+
self, "MyEndpoint",
33+
endpoint_config_name=endpoint_config.endpoint_config_name,
34+
endpoint_name="test-endpoint-11-20"
35+
)
36+
37+
# Ensure the Endpoint is created after the EndpointConfig
38+
endpoint.add_dependency(endpoint_config)
39+
40+
# Output the EndpointConfig name
41+
CfnOutput(self, "EndpointConfigName", value=endpoint_config.endpoint_config_name)
42+
43+
# Output the Endpoint name
44+
CfnOutput(self, "EndpointName", value=endpoint.endpoint_name)
45+
46+
def generate_cfn_template():
47+
app = App()
48+
stack = SageMakerEndpointStack(app, "SageMakerEndpointStack")
49+
50+
# Synthesize the stack to a CloudFormation template
51+
cfn_template = app.synth().get_stack_by_name(stack.stack_name).template
52+
53+
# Save the CloudFormation template as a JSON file
54+
with open("sagemaker_endpoint_template.json", "w") as f:
55+
json.dump(cfn_template, f, indent=2)
56+
57+
print("CloudFormation template saved as sagemaker_endpoint_template.json")
58+
59+
return cfn_template
60+
61+
if __name__ == "__main__":
62+
generate_cfn_template()
63+
print("CDK synthesis complete")

0 commit comments

Comments
 (0)