Load S3 Data into AWS SageMaker Notebook
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Amazon S3 (Simple Storage Service) is a widely used object storage service in cloud computing, providing scalable and secure data storage. AWS SageMaker, a comprehensive machine learning platform, often requires loading data from S3 for preprocessing, training, and deployment. This article provides a detailed, step-by-step guide on how to efficiently load data from S3 into a SageMaker notebook.
Prerequisites
Before we delve into the process, make sure you have the following:
- An active AWS account.
- IAM role with sufficient permissions to access SageMaker and S3.
- SageMaker notebook instance set up.
- Data stored in an S3 bucket.
Step-by-Step Guide
Step 1: Configure IAM Role
Ensure that the SageMaker notebook has an IAM role attached with policies to access the S3 bucket. AWS provides managed policies like AmazonS3ReadOnlyAccess which could be attached. To create a custom policy, ensure it grants at least the following permissions:
Step 2: Setting Up the SageMaker Notebook
- Launch the notebook instance: If it's not already running, launch the SageMaker notebook instance.
- Attach the IAM Role: Ensure the instance uses the IAM role configured in Step 1.
Step 3: Loading Data into SageMaker
The data can be loaded by using boto3, AWS's SDK for Python, or via SageMaker's library. Here’s how to achieve this using both methods:
Using Boto3:
- Install Boto3:
- Import and Configure Boto3:
- Load Data into Pandas DataFrame:
Using SageMaker's Built-in Functions:
- Install SageMaker:
- Load Data:SageMaker provides built-in functions for simplified data loading.
Step 4: Preprocessing and Utilizing Data
Once the data is loaded into a Pandas DataFrame, you can preprocess it using standard Pandas operations. This usually involves cleaning, normalizing, and transforming the dataset to prepare it for model training.
Step 5: Cleaning Up
Upon completing your tasks:
- Unload unnecessary data from memory.
- Stop the SageMaker instance if not in use to avoid incurring additional costs.
Summary
Here's a concise table outlining the key steps to load data from S3 into SageMaker:
| Step | Description | Command/Code |
| 1 | Configure IAM Role | AWS IAM Console |
| 2 | Set Up Notebook Instance | AWS SageMaker Console |
| 3 | Load Data using boto3 | s3.download_file(...) |
| Load Data using SageMaker | session.download_data(...) | |
| 4 | Preprocess Data | pd.read_csv(...) |
| 5 | Clean Up Resources | AWS Console Actions |
Additional Topics
Handling Large Datasets
For large datasets, consider using SageMaker-specific features like Pipe Mode or distributed training to optimize performance and memory usage.
Automation and Scheduling
To automate data loading and preprocessing, integrate AWS Lambda functions or AWS Step Functions in your workflow.
Security Considerations
Use encryption for sensitive data and configure S3 bucket policies and VPC settings to ensure data security and compliance.
Conclusion
Loading data from S3 to SageMaker is a critical step in the machine learning pipeline. With AWS's robust tools, this process can be streamlined to enhance efficiency and focus on building and deploying powerful models. By following these steps and best practices, you can harness S3 and SageMaker's full potential in your data science endeavors.
Related reading
- Load saved checkpoint and predict not producing same results as in training
- Load Tensorflow js model from local file system in javascript
- load weights require h5py
- Loaded runtime CuDNN library 8.0.5 but source was compiled with 8.1.0
- Load S3 Data into AWS SageMaker Notebook
- Local cloud stack for Azure similar to LocalStack for AWS?
- Loading a pyspark ML model in a non-Spark environment
- Loading a trained Keras model and continue training

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.