AWS Builder Center
Automating LLM Fine-Tuning with Step Functions

Automating LLM Fine-Tuning with Step Functions

How I used CodePipeline, CodeBuild, Step Functions, and SageMaker to automate LLM fine-tuning.

Cloud Engineer | AWS | Generative AI & Agentic AI

Automating LLM Fine-Tuning with Step Functions

Introduction

I recently completed the AWS SimuLearn: Automate Fine-Tuning of an LLM hands-on lab on AWS Skill Builder.
In my previous AWS labs, I explored how to fine-tune a large language model using LoRA and deploy it to an Amazon SageMaker endpoint.
However, that process involved manually executing notebook cells, preparing model artifacts, and deploying the model.
This time, I explored a different challenge:
How can we automate the fine-tuning and deployment workflow instead of performing every step manually?
This lab introduced me to AWS CodePipeline, AWS CodeBuild, AWS Step Functions, AWS Lambda, and Amazon SageMaker AI working together to build an automated machine learning workflow.
Here are my key takeaways.

1. Understanding the Business Scenario

The scenario involved a technology company struggling to maintain the accuracy and relevance of its large language model.
The company relied on a manual process for retraining models and applying updates.
This created several challenges:
  • Repetitive manual operations.
  • Delays when introducing new training data.
  • Time-consuming model deployment.
  • Difficulty maintaining a consistent update process.
The goal was to automate the workflow using AWS services.
Instead of manually coordinating every operation, the solution uses a state machine to manage the steps required for fine-tuning and deployment.

2. Exploring the Solution Architecture

The solution combines CI/CD automation with machine learning workflow orchestration.
AWS Service
Responsibility
Amazon S3
Stores source code, datasets, and model artifacts
AWS CodePipeline
Orchestrates the source and build stages
AWS CodeBuild
Runs the Python deployment script
AWS Step Functions
Coordinates the model fine-tuning and deployment workflow
AWS Lambda
Creates and checks the SageMaker notebook environment
Amazon SageMaker AI
Provides the training environment and model deployment
AWS IAM
Controls access between the services
The architecture can be divided into two main stages.
Stage 1 — Provisioning the workflow
Amazon S3 → CodePipeline → CodeBuild → Step Functions State Machine
Stage 2 — Executing the workflow
Step Functions → AWS Lambda → SageMaker Notebook → Model Artifacts → SageMaker Model → Endpoint Configuration → SageMaker Endpoint
An important distinction is that deploying a state machine does not automatically start its execution.
The state machine defines the workflow, while an execution represents an actual run.

3. Understanding AWS CodePipeline and CodeBuild

One of the most valuable parts of this lab was understanding how CodePipeline and CodeBuild work together.
The source code was stored in an Amazon S3 bucket as a ZIP archive.
CodePipeline retrieved the source artifact and passed it to CodeBuild.
CodeBuild then prepared the build environment, extracted the source, and executed the commands defined in  buildspec.yml .
For example, the build configuration included:
 python state_machine_manager.py 
This Python script used AWS SDKs to define and create a Step Functions state machine.
I also learned about the environment variable:
 $CODEBUILD_SRC_DIR 
This variable points to the directory containing the primary source code prepared by CodeBuild.
An important lesson was that CodeBuild performs several operations automatically, including environment provisioning, source preparation, and environment variable initialization.
Understanding these operations made the CI/CD workflow much easier to debug.

4. Creating a State Machine Programmatically

The next important concept was AWS Step Functions.
Instead of creating the workflow manually in the AWS Console, the lab used the Step Functions Data Science SDK to define it through Python.
The script defined tasks such as:
  • Creating a SageMaker notebook environment.
  • Waiting for the environment to become ready.
  • Checking its status.
  • Creating a SageMaker Model.
  • Creating an endpoint configuration.
  • Creating a SageMaker endpoint.
The workflow was assembled using  steps.Chain() .
The key operation was:
 basic_workflow.create() 
This created the state machine resource in AWS Step Functions.
I learned three concepts that initially seemed confusing:
State Machine: The definition of the workflow, including tasks, conditions, and transitions.
State: An individual step within the workflow, such as a Task, Wait, or Choice state.
Execution: A single run of a state machine.
A state machine has its own ARN, while each execution also receives a unique execution ARN.
This distinction makes it possible to track individual workflow runs independently.

5. Automating Training Environment Setup with AWS Lambda

Another interesting part of the lab involved AWS Lambda.
The state machine invoked a preconfigured Lambda function named:
 create_notebook_function 
The function used Boto3 to create a SageMaker Notebook Instance and its Lifecycle Configuration.
The Lifecycle Configuration contained initialization scripts for preparing the notebook environment.
These scripts handled operations such as:
  • Downloading training scripts from Amazon S3.
  • Extracting the source files.
  • Creating a Python environment.
  • Installing machine learning dependencies.
  • Preparing the environment for the training notebook.
This demonstrated how Lambda can automate AWS infrastructure operations.
The actual model fine-tuning code runs in the notebook environment, not inside the Lambda function.

6. Fine-Tuning the LLM and Saving Model Artifacts

The lab used the pretrained EleutherAI Pythia 2.8B model and an Amazon SageMaker FAQ dataset.
The notebook performed data preparation, tokenization, and parameter-efficient fine-tuning using LoRA.
The important training operation was:
 trainer.train() 
LoRA allows a pretrained model to be adapted by training a relatively small number of additional parameters instead of updating all its original weights.
After training, the notebook saved the adapter weights and prepared the inference files.
The resulting deployment package was uploaded to Amazon S3:
 output/lora_model.tar.gz 
One important lesson was understanding the separation between training and deployment.
Training produces model artifacts, while deployment uses those artifacts to serve inference requests.

7. Orchestrating Deployment with Step Functions

After the notebook preparation and training process, the state machine coordinates the deployment stages.
The workflow includes:
Create Model
Creates a SageMaker Model resource using the configured inference image, execution role, and model artifacts.
Create Endpoint Configuration
Defines the inference infrastructure, including the instance type and instance count.
Create Endpoint
Deploys the model using the configured inference environment.
In this lab, the endpoint configuration used a GPU instance type:
 ml.g4dn.2xlarge 
The resulting endpoint can serve inference requests once it reaches the appropriate ready state.
The workflow also used Wait, Choice, and Retry mechanisms.
For example, the state machine waits 150 seconds between status checks while monitoring the notebook workflow.
Instead of keeping a Lambda function running continuously during the waiting period, Step Functions manages the wait and the transitions.
A practical lesson is that status monitoring must be carefully designed. A notebook instance being ready does not necessarily prove that model training has completed successfully. Production workflows need explicit training completion checks and appropriate failure handling.

8. Understanding the DIY Challenge

The DIY section introduced an additional challenge: creating a new CI/CD pipeline to provision another state machine.
The objectives included:
  • Configuring a new CodePipeline named  diy-genai-pipeline .
  • Using the provided S3 source package.
  • Creating a CodeBuild project.
  • Provisioning a state machine named  DIY_FineTuningLLM .
  • Reviewing the required workflow definition and its configuration.
This helped reinforce the distinction between deploying a workflow and executing it.
It also demonstrated that a workflow can be managed as code rather than relying entirely on manual Console operations.

9. Key Lessons Learned

This lab helped me connect several AWS concepts that I had previously studied separately.
1. CodePipeline and CodeBuild have different responsibilities.
CodePipeline orchestrates CI/CD stages, while CodeBuild executes build commands.
2. Step Functions is a workflow orchestrator.
It coordinates operations across AWS services without performing the machine learning computation itself.
3. A state machine and an execution are different.
The state machine defines what should happen, while an execution represents one actual run.
4. Lambda can automate infrastructure operations.
Lambda functions can create and manage AWS resources using Boto3.
5. SageMaker handles the machine learning workload.
The notebook runs the fine-tuning code, and SageMaker hosting resources provide model inference.
6. Automation needs observability and error handling.
Wait states, Choice states, retries, execution history, and service logs are important for troubleshooting automated workflows.
7. CI/CD automation and ML workflow orchestration complement each other.
CodePipeline can deploy workflow definitions, while Step Functions manages the processes those workflows describe.

10. Final Thoughts

This lab helped me move from manually fine-tuning an LLM toward understanding how to orchestrate model training and deployment through AWS automation services.
What made this experience especially valuable was learning what happens behind the scenes.
I gained a better understanding of how CodeBuild prepares source code, how Python creates a Step Functions state machine, how Lambda configures a SageMaker notebook, and how the workflow progresses toward model deployment.
My biggest takeaway is:
MLOps is not just about training better models. It is also about making machine learning workflows repeatable, observable, and easier to operate.
For future exploration, I would like to build a more production-oriented workflow with automated model evaluation, approval conditions, versioning, monitoring, and deployment rollback.
This was another valuable hands-on experience in my AWS learning journey!
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