{"slug": "how-to-deploy-your-ml-model-to-aws-step-by-step-guide", "title": "How to Deploy Your ML Model to AWS (Step-by-Step Guide)", "summary": "A developer provides a step-by-step guide to deploying machine learning models to AWS using SageMaker, covering model packaging, S3 upload, endpoint creation, and inference testing. The guide includes code examples for saving models, creating inference scripts, and invoking endpoints, along with troubleshooting tips and cost considerations.", "body_md": "I've trained more ML models than I've deployed. There's something comforting about the local loop—`model.fit()`\n\n, `model.evaluate()`\n\n, hitting 94% accuracy, then staring at the screen wondering, \"Okay, how do I make this actually useful?\"\n\nIf you're stuck there right now, this guide will help.\n\nNote: I wrote this based on AWS documentation and standard SageMaker patterns. If you try it, drop a comment about what worked (or broke).\n\n`model.pkl`\n\n(or `.joblib`\n\n)`requirements.txt`\n\nwith your dependencies`aws configure`\n\n)\n\n``` python\nimport joblib\njoblib.dump(model, 'model.pkl')\n```\n\nCreate a `requirements.txt`\n\nfile:\n\n```\nsklearn==1.2.0\npandas==1.5.0\nnumpy==1.23.0`\n```\n\nKeep both files in the same folder.\n\n``` python\nimport boto3\n\ns3 = boto3.client('s3')\n\nbucket_name = 'my-unique-ml-bucket-12345'  # Make this unique\ns3.create_bucket(Bucket=bucket_name, CreateBucketConfiguration={\n    'LocationConstraint': 'us-east-1'\n})\n\ns3.upload_file('model.pkl', bucket_name, 'models/model.pkl')\ns3.upload_file('requirements.txt', bucket_name, 'models/requirements.txt')\n\nmodel_s3_path = f's3://{bucket_name}/models/model.pkl'\n```\n\nSave this as `inference.py`\n\n:\n\n``` python\nimport json\nimport joblib\nimport numpy as np\nimport os\n\nmodel = None\n\ndef model_fn(model_dir):\n    return joblib.load(os.path.join(model_dir, 'model.pkl'))\n\ndef input_fn(input_data, content_type):\n    if content_type == 'application/json':\n        data = json.loads(input_data)\n        return np.array(data['features'])\n    raise ValueError(f\"Unsupported content type: {content_type}\")\n\ndef predict_fn(input_data, model):\n    return model.predict(input_data)\n\ndef output_fn(prediction, content_type):\n    return json.dumps({'predictions': prediction.tolist()})\n```\n\nThese four functions are what SageMaker calls when someone hits your endpoint.\n\nRun this in a Python script:\n\n``` python\nfrom sagemaker.sklearn.model import SKLearnModel\nfrom sagemaker import get_execution_role\n\nsklearn_model = SKLearnModel(\n    model_data=model_s3_path,\n    role=get_execution_role(),\n    instance_type='ml.m5.large',\n    entry_point='inference.py',\n    py_version='py3'\n)\n\nsklearn_model.deploy(\n    initial_instance_count=1,\n    instance_type='ml.m5.large',\n    endpoint_name='my-model-endpoint'\n)\n```\n\nThis takes 5–10 minutes. You'll see `Creating`\n\n→ `In Service`\n\n.\n\n``` python\nimport boto3\nimport json\n\nruntime = boto3.client('sagemaker-runtime')\n\nresponse = runtime.invoke_endpoint(\n    EndpointName='my-model-endpoint',\n    ContentType='application/json',\n    Body=json.dumps({'features': [[5.1, 3.5, 1.4, 0.2]]})\n)\n\nresult = json.loads(response['Body'].read().decode())\nprint(result)\n```\n\nIf you see `{'predictions': [...]}`\n\n, it worked.\n\nEndpoints cost money even when idle:\n\n```\naws sagemaker delete-endpoint --endpoint-name my-model-endpoint\naws sagemaker delete-endpoint-config --endpoint-config-name my-model-endpoint\n```\n\nError |\nFix |\n|---|---|\n`NoCredentialsError` |\nRun `aws configure` again |\n`InvalidRoleException` |\nIAM role needs S3 + SageMaker permissions |\n`ModelError` |\nCheck `inference.py` for missing imports |\nEndpoint stuck on `Creating`\n|\nWait 5–10 more minutes |\n\nYour IAM role needs:\n\n`s3:GetObject`\n\n, `s3:PutObject`\n\n`sagemaker:CreateModel`\n\n, `sagemaker:CreateEndpoint`\n\nResource |\nCost |\n|---|---|\n`ml.m5.large` |\n~$0.20/hour (~$6/month if 24/7) |\n| S3 storage | ~$0.02/GB/month |\n\nDelete when not using. I've seen $50 surprises from idle endpoints.\n\nIf you're following this, check:\n\n`pip show boto3 sagemaker`\n\n`os`\n\n, `joblib`\n\n, `numpy`\n\nare installedIf something breaks, comment below with the error. I'll update this guide.\n\nDeploying ML feels intimidating until you do it once. SageMaker handles most of the complexity. You just upload your model to S3, point SageMaker at it, and deploy.\n\nI've trained models that sat on my laptop for months because I didn't know how to deploy them. Now I tell people: \"Just run this script, it's not that hard.\"\n\n*If you're building something with this, drop a comment. I love seeing what people deploy.*", "url": "https://wpnews.pro/news/how-to-deploy-your-ml-model-to-aws-step-by-step-guide", "canonical_source": "https://dev.to/shresthapandey/how-to-deploy-your-ml-model-to-aws-step-by-step-guide-af9", "published_at": "2026-06-22 07:21:40+00:00", "updated_at": "2026-06-22 07:39:44.631048+00:00", "lang": "en", "topics": ["machine-learning", "mlops", "developer-tools"], "entities": ["AWS", "SageMaker", "S3", "boto3", "scikit-learn", "joblib", "pandas", "numpy"], "alternates": {"html": "https://wpnews.pro/news/how-to-deploy-your-ml-model-to-aws-step-by-step-guide", "markdown": "https://wpnews.pro/news/how-to-deploy-your-ml-model-to-aws-step-by-step-guide.md", "text": "https://wpnews.pro/news/how-to-deploy-your-ml-model-to-aws-step-by-step-guide.txt", "jsonld": "https://wpnews.pro/news/how-to-deploy-your-ml-model-to-aws-step-by-step-guide.jsonld"}}