Entry non-public repos utilizing the @distant decorator for Amazon SageMaker coaching workloads

As an increasing number of clients need to put machine studying (ML) workloads in manufacturing, there’s a massive push in organizations to shorten the event lifecycle of ML code. Many organizations desire writing their ML code in a production-ready type within the type of Python strategies and lessons versus an exploratory type (writing code with out utilizing strategies or lessons) as a result of this helps them ship production-ready code quicker.

With Amazon SageMaker, you need to use the @remote decorator to run a SageMaker coaching job just by annotating your Python code with an @distant decorator. The SageMaker Python SDK will robotically translate your present workspace atmosphere and any related knowledge processing code and datasets right into a SageMaker coaching job that runs on the SageMaker coaching platform.

Operating a Python operate regionally usually requires a number of dependencies, which can not include the native Python runtime atmosphere. You may set up them through bundle and dependency administration instruments like pip or conda.

Nonetheless, organizations working in regulated industries like banking, insurance coverage, and healthcare function in environments which have strict knowledge privateness and networking controls in place. These controls usually mandate having no web entry out there to any of their environments. The rationale for such restriction is to have full management over egress and ingress visitors to allow them to cut back the possibilities of unscrupulous actors sending or receiving non-verified info by their community. It’s usually additionally mandated to have such community isolation as a part of the auditory and industrial compliance guidelines. In the case of ML, this restricts knowledge scientists from downloading any bundle from public repositories like PyPI, Anaconda, or Conda-Forge.

To offer knowledge scientists entry to the instruments of their alternative whereas additionally respecting the restrictions of the atmosphere, organizations usually arrange their very own non-public bundle repository hosted in their very own atmosphere. You may arrange non-public bundle repositories on AWS in a number of methods:

On this publish, we give attention to the primary possibility: utilizing CodeArtifact.

Resolution overview

The next structure diagram exhibits the answer structure.


The high-level steps to implement the answer are as follows

  • Arrange a digital non-public cloud (VPC) with no web entry utilizing an AWS CloudFormation template.
  • Use a second CloudFormation template to arrange CodeArtifact as a personal PyPI repository and supply connectivity to the VPC, and arrange an Amazon SageMaker Studio atmosphere to make use of the non-public PyPI repository.
  • Practice a classification mannequin based mostly on the MNIST dataset utilizing an @distant decorator from the open-source SageMaker Python SDK. All of the dependencies shall be downloaded from the non-public PyPI repository.

Be aware that utilizing SageMaker Studio on this publish is elective. You may select to work in any built-in growth atmosphere (IDE) of your alternative. You simply have to arrange your AWS Command Line Interface (AWS CLI) credentials appropriately. For extra info, check with Configure the AWS CLI.


You want an AWS account with an AWS Identity and Access Management (IAM) role with permissions to handle sources created as a part of the answer. For particulars, check with Creating an AWS account.

Arrange a VPC with no web connection

Create a new CloudFormation stack utilizing the vpc.yaml template. This template creates the next sources:

  • A VPC with two non-public subnets throughout two Availability Zones with no web connectivity
  • A Gateway VPC endpoint for accessing Amazon S3
  • Interface VPC endpoints for SageMaker, CodeArtifact, and some different companies to permit the sources within the VPC to hook up with AWS companies through AWS PrivateLink

Present a stack identify, akin to No-Web, and full the stack creation course of.


Anticipate the stack creation course of to finish.

Arrange a personal repository and SageMaker Studio utilizing the VPC

The subsequent step is to deploy one other CloudFormation stack utilizing the sagemaker_studio_codeartifact.yaml template. This template creates the next sources:

Present a stack identify and maintain the default values or regulate the parameters for the CodeArtifact area identify, non-public repository identify, person profile identify for SageMaker Studio, and identify for the upstream public PyPI repository. You additionally we have to present the VPC stack identify created within the earlier step.


When the stack creation is full, the SageMaker area must be seen on the SageMaker console.


To confirm there isn’t any web connection out there in SageMaker Studio, launch SageMaker Studio. Select File, New, and Terminal to launch a terminal and attempt to curl any web useful resource. It ought to fail to attach, as proven within the following screenshot.


Practice a picture classifier utilizing an @distant decorator with the non-public PyPI repository

On this part, we use the @distant decorator to run a PyTorch coaching job that produces a MNIST picture classification mannequin. To realize this, we arrange a configuration file, develop the coaching script, and run the coaching code.

Arrange a configuration file

We arrange a config.yaml file and supply the configurations wanted to do the next:

  • Run a SageMaker training job within the no-internet VPC created earlier
  • Obtain the required packages by connecting to the non-public PyPI repository created earlier

The file appears to be like like the next code:

SchemaVersion: '1.0'
        Dependencies: '../config/necessities.txt'
        InstanceType: 'ml.m5.xlarge'
            - 'aws codeartifact login --tool pip --domain <domain-name> --domain-owner <AWS account quantity> --repository <non-public repository identify> --endpoint-url <VPC-endpoint-url-prefixed with https://>
        RoleArn: '<execution position ARN for operating coaching job>'
        S3RootUri: '<s3 bucket to retailer the job output>'
            - '<safety group id utilized by SageMaker Studio>'
            - '<VPC subnet id 1>'
            - '<VPC subnet id 2>'

The Dependencies discipline comprises the trail to necessities.txt, which comprises all of the dependencies wanted. Be aware that every one the dependencies shall be downloaded from the non-public repository. The necessities.txt file comprises the next code:


The PreExecutionCommands part comprises the command to hook up with the non-public PyPI repository. To get the CodeArtifact VPC endpoint URL, use the next code:

response = ec2.describe_vpc_endpoints(
            'Name': 'service-name',
            'Values': [

code_artifact_api_vpc_endpoint = response['VpcEndpoints'][0]['DnsEntries'][0]['DnsName']

endpoint_url = f'https://{code_artifact_api_vpc_endpoint}'

Typically, we get two VPC endpoints for CodeArtifact, and we are able to use any of them within the connection instructions. For extra particulars, check with Use CodeArtifact from a VPC.

Moreover, configurations like execution position, output location, and VPC configurations are supplied within the config file. These configurations are wanted to run the SageMaker coaching job. To know extra about all of the configurations supported, check with Configuration file.

It’s not obligatory to make use of the config.yaml file with the intention to work with the @distant decorator. That is only a cleaner approach to provide all configurations to the @distant decorator. All of the configs is also provided instantly within the decorator arguments, however that reduces readability and maintainability of adjustments in the long term. Additionally, the config file might be created by an admin and shared with all of the customers in an atmosphere.

Develop the coaching script

Subsequent, we put together the coaching code in easy Python information. We have now divided the code into three information:

  • – Incorporates the code to obtain the MNIST dataset
  • – Incorporates the code for the neural community structure for the mannequin
  • – Incorporates the code for coaching the mannequin through the use of and

In, we have to beautify the principle coaching operate as follows:

def perform_train(train_data,
                  batch_size: int = 64,
                  test_batch_size: int = 1000,
                  epochs: int = 3,
                  lr: float = 1.0,
                  gamma: float = 0.7,
                  no_cuda: bool = True,
                  no_mps: bool = True,
                  dry_run: bool = False,
                  seed: int = 1,
                  log_interval: int = 10,
    # pytorch native coaching code........

Now we’re able to run the coaching code.

Run the coaching code with an @distant decorator

We are able to run the code from a terminal or from any executable immediate. On this publish, we use a SageMaker Studio pocket book cell to exhibit this:

Operating the previous command triggers the coaching job. Within the logs, we are able to see that it’s downloading the packages from the non-public PyPI repository.


This concludes the implementation of an @distant decorator working with a personal repository in an atmosphere with no web entry.

Clear up

To wash up the sources, comply with the directions in


On this publish, we realized methods to successfully use the @distant decorator’s capabilities whereas nonetheless working in restrictive environments with none web entry. We additionally realized how can we combine CodeArtifact non-public repository capabilities with the assistance of configuration file assist in SageMaker. This resolution makes iterative growth a lot less complicated and quicker. One other added benefit is which you can nonetheless proceed to put in writing the coaching code in a extra pure, object-oriented method and nonetheless use SageMaker capabilities to run coaching jobs on a distant cluster with minimal adjustments in your code. All of the code proven as a part of this publish is accessible within the GitHub repository.

As a subsequent step, we encourage you to take a look at the @remote decorator functionality and Python SDK API and use it in your alternative of atmosphere and IDE. Extra examples can be found within the amazon-sagemaker-examples repository to get you began rapidly. You can even take a look at the publish Run your local machine learning code as Amazon SageMaker Training jobs with minimal code changes for extra particulars.

In regards to the writer

Vikesh Pandey is a Machine Studying Specialist Options Architect at AWS, serving to clients from monetary industries design and construct options on generative AI and ML. Outdoors of labor, Vikesh enjoys making an attempt out completely different cuisines and taking part in out of doors sports activities.

Gradient Boosting from Concept to Observe (Half 1) | by Dr. Roi Yehoshua | Jul, 2023

An open-source gymnasium for machine studying assisted pc structure design – Google Analysis Weblog