This is a Custom Resource example for CDK development with Python.
In this project, we will deploy SageMaker JumpStart model into SageMaker Endpoint.
The cdk.json
file tells the CDK Toolkit how to execute your app.
This project is set up like a standard Python project. The initialization
process also creates a virtualenv within this project, stored under the .venv
directory. To create the virtualenv it assumes that there is a python3
(or python
for Windows) executable in your path with access to the venv
package. If for any reason the automatic creation of the virtualenv fails,
you can create the virtualenv manually.
To manually create a virtualenv on MacOS and Linux:
$ python3 -m venv .venv
After the init process completes and the virtualenv is created, you can use the following step to activate your virtualenv.
(.venv) $ source .venv/bin/activate
If you are a Windows platform, you would activate the virtualenv like this:
% .venv\Scripts\activate.bat
Once the virtualenv is activated, you can install the required dependencies.
(.venv) $ pip install -r requirements.txt
Before deployment, you should uplad zipped code files to s3 like this example:
⚠️ Important: Replacelambda-layer-resources
with your s3 bucket name for lambda layer zipped code. :warning: To create a bucket outside of theus-east-1
region,aws s3api create-bucket
command requires the appropriate LocationConstraint to be specified in order to create the bucket in the desired region. For more information, see these examples.
⚠️ Make sure you have Docker installed.
(.venv) $ aws s3api create-bucket --bucket lambda-layer-resources --region us-east-1 (.venv) $ cat <requirements-lambda_layer.txt > sagemaker==2.188 > cfnresponse==1.1.2 > urllib3==1.26.16 > EOF (.venv) $ docker run -v "$PWD":/var/task "public.ecr.aws/sam/build-python3.10" /bin/sh -c "pip install -r requirements-lambda_layer.txt -t python/lib/python3.10/site-packages/; exit" (.venv) $ zip -r sagemaker-python-sdk-lib.zip python > /dev/null (.venv) $ aws s3 cp sagemaker-python-sdk-lib.zip s3://lambda-layer-resources/pylambda-layer/
For more information about how to create a package for Amazon Lambda Layer, see here.
Before to synthesize the CloudFormation template for this code, you should update cdk.context.json
file.
In particular, you need to fill the s3 location of the previously created lambda lay codes.
For example,
{ "lambda_layer_lib_s3_path": "s3://lambda-layer-resources/pylambda-layer/sagemaker-python-sdk-lib.zip", "sagemaker_jumpstart_model_info": { "model_id": "meta-textgeneration-llama-2-7b-f", "endpoint_name": "meta-textgen-llama-2-7b-f" } }
Now you are ready to synthesize the CloudFormation template for this code.
(.venv) $ export CDK_DEFAULT_ACCOUNT=$(aws sts get-caller-identity --query Account --output text)
(.venv) $ export CDK_DEFAULT_REGION=$(curl -s 169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region)
(.venv) $ cdk synth --all
Use cdk deploy
command to create the stack shown above.
(.venv) $ cdk deploy --require-approval never --all
Or, we can provision each CDK stack one at a time like this:
(.venv) $ cdk list
SageMakerPySDKLambdaLayerStack
SageMakerEndpointIAMRoleStack
SMJumpStartModelDeployLambdaStack
SMJumpStartModelEndpointStack
(.venv) $ cdk deploy --require-approval never SageMakerPySDKLambdaLayerStack
(.venv) $ cdk deploy --require-approval never SageMakerEndpointIAMRoleStack
(.venv) $ cdk deploy --require-approval never SMJumpStartModelDeployLambdaStack
(.venv) $ cdk deploy --require-approval never SMJumpStartModelEndpointStack
⚠️ Deploying SageMaker JumpStart Model (i.e., Step 4) requires about5~15
minutes. Therefore, wait for about a few minutes after launching theSMJumpStartModelEndpointStack
to use SageMaker Endpoint.
ℹ️ To add additional dependencies, for example other CDK libraries, just add them to your
setup.py
file and rerun thepip install -r requirements.txt
command.
Once all CDK stacks have been successfully created, we can use SageMaker Endpoint.
For example, we can test SageMaker Endpoint that meta-textgeneration-llama-2-7b-f
is deployed by running the following code in SageMaker Studio.
import base64
import json
import boto3
region = boto3.session.Session().region_name
def query_endpoint(payload, endpoint_name=None, region_name='us-east-1'):
client = boto3.client("sagemaker-runtime", region_name=region_name)
response = client.invoke_endpoint(
EndpointName=endpoint_name,
ContentType="application/json",
Body=json.dumps(payload),
CustomAttributes="accept_eula=true", # eula: End User Licence Agreement
)
response = response["Body"].read().decode("utf8")
response = json.loads(response)
return response
#TODO: Should create a right payload based on your ML model
payload = {
"inputs": [
[
{"role": "system", "content": "Always answer with Haiku"},
{"role": "user", "content": "I am going to Paris, what should I see?"}
]
],
"parameters": {"max_new_tokens": 256, "top_p": 0.9, "temperature": 0.6}
}
TEXT2TEXT_ENDPOINT_NAME = "meta-textgen-llama-2-7b-f" #TODO: Replace your endpoint name
result = query_endpoint(payload, TEXT2TEXT_ENDPOINT_NAME)[0]
print(result)
Delete the CloudFormation stack by running the below command.
(.venv) $ cdk destroy --force --all
cdk ls
list all stacks in the appcdk synth
emits the synthesized CloudFormation templatecdk deploy
deploy this stack to your default AWS account/regioncdk diff
compare deployed stack with current statecdk docs
open CDK documentation
Enjoy!
- SageMaker Python SDK - Deploy a Pre-Trained Model Directly to a SageMaker Endpoint
- Use proprietary foundation models from Amazon SageMaker JumpStart in Amazon SageMaker Studio (2023-06-27)
- AWS CDK TypeScript Example - Custom Resource
- How to create a Lambda layer using a simulated Lambda environment with Docker
$ cat <<EOF>requirements-lambda_layer.txt > sagemaker==2.188 > cfnresponse==1.1.2 > urllib3==1.26.16 > EOF $ docker run -v "$PWD":/var/task "public.ecr.aws/sam/build-python3.10" /bin/sh -c "pip install -r requirements-lambda_layer.txt -t python/lib/python3.10/site-packages/; exit" $ zip -r sagemaker-python-sdk-lib.zip python > /dev/null $ aws s3 mb s3://my-bucket-for-lambda-layer-packages $ aws s3 cp sagemaker-python-sdk-lib.zip s3://my-bucket-for-lambda-layer-packages/pylambda-layer/
- (AWS re:Post) Stack deletion stuck as DELETE_IN_PROGRESS
- (Video) How do I delete an AWS Lambda-backed custom resource that’s stuck deleting in AWS CloudFormation?
- (Stack Overflow)"cannot import name 'DEFAULT_CIPHERS' from 'urllib3.util.ssl_'" on AWS Lambda using a layer
- Error message:
cannot import name 'DEFAULT_CIPHERS' from 'urllib3.util.ssl_' (/opt/python/lib/python3.10/site-packages/urllib3/util/ssl_.py
- Solution: You’ll need to explicitly pin to
urllib3<2
in your project to ensureurllib3 2.0
isn’t brought into your environment.urllib3<2
- Error message: