AWS Builder Center

Building a Cloud-Based Network Intrusion Detection System Using AWS

Learn how to design a cloud-based Network Intrusion Detection System using AWS, Python, and machine learning. This article covers data storage, model training, evaluation, deployment, and security considerations using AWS services.

Network attacks are becoming increasingly difficult to identify using only traditional rule-based security mechanisms. Modern network environments generate large volumes of traffic, making it challenging to manually analyze every connection and identify suspicious behavior.
Machine learning can help by learning patterns from network traffic and classifying connections as normal or malicious.
In this article, I will walk through how a machine-learning-based Network Intrusion Detection System (NIDS) can be designed using AWS services, Python, and Scikit-learn.
The goal is not just to train a machine learning model, but to understand how a local ML workflow can be transformed into a cloud-based architecture.

What are we building?

The system takes network traffic records as input and uses a trained machine learning model to determine whether the traffic appears to be:
  • Normal
  • Malicious
The overall workflow is:
Network Dataset → Amazon S3 → Data Preparation → Model Training → Model Evaluation → Model Deployment → Prediction
For the machine learning component, a classification model such as Random Forest can be used.

Why use AWS?

A machine learning project can be developed entirely on a local computer, but cloud services become useful when the project needs scalable storage, managed machine learning infrastructure, deployment, monitoring, and collaboration.
For this architecture, we can use:
  • Amazon S3 — store datasets and model artifacts
  • Amazon SageMaker AI — develop, train, and deploy the ML model
  • IAM — control access between AWS resources
  • Amazon CloudWatch — monitor logs and application activity
Amazon S3 can serve as the storage layer for ML data and artifacts, while SageMaker AI can manage the infrastructure required for machine learning training jobs.

Architecture Flow

The proposed architecture follows this flow:
Network Traffic Dataset
↓
Amazon S3
↓
SageMaker / Python Data Processing
↓
Feature Engineering
↓
Random Forest Model
↓
Model Evaluation
↓
SageMaker Endpoint
↓
Network Traffic Prediction
↓
Normal / Malicious
AWS's own SageMaker documentation describes a similar ML workflow in which training data can be stored in S3 and SageMaker manages the infrastructure for training workloads.

Step 1: Understanding the Dataset

For this project, we can use the UNSW-NB15 network intrusion detection dataset.
The dataset contains network traffic features that can be used to distinguish normal traffic from different types of attacks.
Each record contains features describing network activity, which can then be transformed into numerical features suitable for machine learning.
Before training the model, the data needs to be cleaned and prepared.
Typical preprocessing steps include:
  1. Loading the dataset
  2. Handling missing values
  3. Removing unnecessary columns
  4. Encoding categorical features
  5. Separating features and target labels
  6. Splitting data into training and testing sets
  7. Scaling or transforming features when required

Step 2: Uploading Data to Amazon S3

Amazon S3 provides highly durable object storage and can be used as the central storage location for datasets and machine learning artifacts.
A simple bucket structure could look like:
1
2
3
4
5
6
7
8
9
10
11
nids-ml-project/
│
├── data/
│ ├── train.csv
│ └── test.csv
│
├── models/
│ └── random_forest.pkl
│
└── predictions/
└── results.csv
The idea is to separate raw data, trained models, and prediction results.

Screenshot to add

Take a screenshot of your S3 bucket showing the folder structure.
Do not expose:
  • AWS account ID
  • access keys
  • secret keys
  • private information

Step 3: Preparing the Data

Once the data is available, we can load it using Python.
1
2
3
4
5
6
7
8
9
import pandas as pd

train_data = pd.read_csv("train.csv")
test_data = pd.read_csv("test.csv")

print(train_data.shape)
print(test_data.shape)

print(train_data.head())
Next, separate the input features from the target label.
1
2
3
4
5
X_train = train_data.drop("label", axis=1)
y_train = train_data["label"]

X_test = test_data.drop("label", axis=1)
y_test = test_data["label"]
The exact column names will depend on the version and preprocessing of the dataset being used.

Step 4: Training the Machine Learning Model

For this example, we can use the Random Forest algorithm.
Random Forest is an ensemble learning algorithm that combines multiple decision trees to produce a classification result.
1
2
3
4
5
6
7
8
from sklearn.ensemble import RandomForestClassifier

model = RandomForestClassifier(
n_estimators=100,
random_state=42
)

model.fit(X_train, y_train)
After training, predictions can be generated using:
1
predictions = model.predict(X_test)

Step 5: Evaluating the Model

Accuracy alone is not enough for an intrusion detection system.
For security-related classification problems, it is useful to examine metrics such as:
  • Accuracy
  • Precision
  • Recall
  • F1-score
  • Confusion matrix
1
2
3
4
5
6
7
8
9
10
11
from sklearn.metrics import (
accuracy_score,
precision_score,
recall_score,
f1_score
)

print("Accuracy:", accuracy_score(y_test, predictions))
print("Precision:", precision_score(y_test, predictions))
print("Recall:", recall_score(y_test, predictions))
print("F1 Score:", f1_score(y_test, predictions))

Why is recall important?

In intrusion detection, missing a malicious connection can be costly.
Therefore, recall can be an important metric because it measures how many of the actual positive cases were detected by the model.
However, optimizing only one metric can create trade-offs, so the complete evaluation should consider multiple metrics.

Visual 3 — Confusion Matrix

Create a confusion matrix showing:
1
2
3
4
5
6
Predicted
Normal Attack

Actual Normal TN FP

Actual Attack FN TP
A heatmap or clean matrix visualization would make this section much easier to understand.

Step 6: Moving the Model to AWS

After developing and testing the model locally, the next step is to move the machine learning workflow into AWS.
Amazon SageMaker AI can be used for managed machine learning training and deployment.
Instead of manually managing all the underlying compute infrastructure, SageMaker training jobs handle the infrastructure required for ML training workloads.
The workflow becomes:
1
2
3
4
5
6
7
8
9
10
11
Local Development
↓
Upload Dataset
↓
Amazon S3
↓
SageMaker Training
↓
Model Artifact
↓
SageMaker Endpoint

Visual 4 — Local to Cloud Transition

Create a simple Before vs After diagram:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
BEFORE

Laptop
├── Dataset
├── Python
├── ML Model
└── Predictions

AFTER

AWS Cloud
├── S3
│ ├── Dataset
│ └── Model
│
└── SageMaker
├── Training
└── Inference Endpoint
This is a particularly useful visual because beginners can immediately understand why cloud deployment is different from local experimentation.

Step 7: Model Deployment

Once the model has been trained, it can be deployed for inference.
Conceptually:
1
2
3
4
5
6
7
8
9
10
11
Incoming Network Traffic
↓
API / Client
↓
SageMaker Endpoint
↓
ML Classification
↓
┌───────────────────┐
│ Normal / Malicious │
└───────────────────┘
For a production implementation, additional components could be introduced for API access, authentication, logging, monitoring, and security.
AWS provides reference architectures where machine-learning endpoints are invoked through application/API layers, demonstrating how ML inference can be integrated into a larger application.

Step 8: Monitoring and Security

Deploying a model is not the end of the workflow.
A production NIDS should also consider:
  • Model performance
  • Prediction latency
  • Logs
  • Failed requests
  • Data drift
  • Model drift
  • Access control
  • Network isolation
IAM should be used to provide only the permissions required by each AWS resource.
For more security-sensitive deployments, SageMaker can also be integrated with VPC networking and private endpoints. AWS documentation describes using VPC endpoints for services such as S3, SageMaker, CloudWatch, and related ML components.

Visual 5 — Security Layer

Create a diagram showing:
1
2
3
4
5
6
7
8
9
10
11
12
13
AWS Cloud
│
┌────────────┴────────────┐
│ │
IAM VPC / Network
│ │
└────────────┬────────────┘
│
SageMaker
│
ML Endpoint
│
Predictions

Step 9: What I Learned

Building an ML-based intrusion detection system helped me understand that machine learning is only one part of a real-world solution.
The complete workflow involves:
Data → Processing → Model → Evaluation → Deployment → Monitoring → Security
One of the biggest lessons is that moving from a notebook-based ML model to a cloud-based application requires thinking about infrastructure, access control, scalability, monitoring, and reliability in addition to model accuracy.
AWS services such as Amazon S3 and SageMaker AI can help connect these different stages into a managed cloud workflow.

Possible Improvements

There are several directions in which this system could be extended.

1. Real-time detection

Instead of processing static datasets, the system could receive network events continuously and generate predictions in near real time.

2. Streaming architecture

A streaming service could be introduced between the network traffic source and the ML inference layer.

3. Better models

Other algorithms could be compared, including:
  • XGBoost
  • Support Vector Machines
  • Neural Networks
  • Gradient Boosting

4. Explainable predictions

An explainability layer could help security analysts understand why traffic was classified as suspicious.

5. Monitoring dashboard

A dashboard could visualize:
  • Number of requests
  • Normal traffic
  • Detected attacks
  • Attack categories
  • Model performance
  • Detection trends

Conclusion

A Network Intrusion Detection System is a good example of how machine learning and cloud computing can work together.
The machine learning model provides the intelligence needed to classify network traffic, while AWS provides services that can support data storage, model training, deployment, access control, and monitoring.
The most important takeaway is that building a practical ML solution is more than training a model. The real challenge is connecting the model to a reliable, secure, and scalable system.
For students and developers beginning their AWS journey, starting with an existing machine learning project can be a practical way to learn cloud concepts without having to build an entirely new application from scratch.
I would be interested to hear from other builders:
How would you extend this architecture for real-time network intrusion detection?
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article