Deploy with Terraform

View as Markdown

Prefer infrastructure-as-code? This Terraform module provisions everything a Sarvam endpoint needs — the IAM execution role, the model (from your Marketplace package ARN), the endpoint configuration, and the endpoint — in a single terraform apply. It works for Saaras v3, Bulbul v3, and Sarvam Vision; just point model_package_arn at the right package (all three run on ml.g6e.xlarge by default).

Prerequisites

  • Terraform 1.5+ and AWS credentials configured.
  • An active Marketplace subscription and the model package ARN for your region.

Variables

variables.tf
variable "region" { type = string default = "ap-south-1" }
variable "endpoint_name" { type = string default = "sarvam-endpoint" }
variable "model_package_arn" { type = string } # from your Marketplace subscription
variable "instance_type" { type = string default = "ml.g6e.xlarge" }
variable "instance_count" { type = number default = 1 }

Main configuration

main.tf
provider "aws" {
region = var.region
}
# Execution role SageMaker assumes to run the endpoint
resource "aws_iam_role" "exec" {
name = "${var.endpoint_name}-exec"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Principal = { Service = "sagemaker.amazonaws.com" }
Action = "sts:AssumeRole"
}]
})
}
resource "aws_iam_role_policy_attachment" "sagemaker" {
role = aws_iam_role.exec.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSageMakerFullAccess"
}
# Model from the Marketplace package
resource "aws_sagemaker_model" "this" {
name = var.endpoint_name
execution_role_arn = aws_iam_role.exec.arn
enable_network_isolation = true
primary_container {
model_package_name = var.model_package_arn
}
}
resource "aws_sagemaker_endpoint_configuration" "this" {
name = var.endpoint_name
production_variants {
variant_name = "AllTraffic"
model_name = aws_sagemaker_model.this.name
instance_type = var.instance_type
initial_instance_count = var.instance_count
}
}
resource "aws_sagemaker_endpoint" "this" {
name = var.endpoint_name
endpoint_config_name = aws_sagemaker_endpoint_configuration.this.name
}
output "endpoint_name" {
value = aws_sagemaker_endpoint.this.name
}

Deploy

terraform init
terraform plan -var="model_package_arn=arn:aws:sagemaker:ap-south-1:<vendor>:model-package/<id>"
terraform apply -var="model_package_arn=arn:aws:sagemaker:ap-south-1:<vendor>:model-package/<id>"

Once apply finishes and the endpoint reaches InService, invoke it exactly as in the Deploy Saaras v3 or Deploy Vision guides.

For async endpoints, add an async_inference_config block (with an S3 output path) to the endpoint configuration and attach S3 permissions to the execution role. Add an aws_appautoscaling_target / aws_appautoscaling_policy pair to autoscale — including scale-to-zero for async.

Tear down

terraform destroy -var="model_package_arn=arn:aws:sagemaker:ap-south-1:<vendor>:model-package/<id>"