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
1variable "region" { type = string default = "ap-south-1" }
2variable "endpoint_name" { type = string default = "sarvam-endpoint" }
3variable "model_package_arn" { type = string } # from your Marketplace subscription
4variable "instance_type" { type = string default = "ml.g6e.xlarge" }
5variable "instance_count" { type = number default = 1 }

Main configuration

main.tf
1provider "aws" {
2 region = var.region
3}
4
5# Execution role SageMaker assumes to run the endpoint
6resource "aws_iam_role" "exec" {
7 name = "${var.endpoint_name}-exec"
8 assume_role_policy = jsonencode({
9 Version = "2012-10-17"
10 Statement = [{
11 Effect = "Allow"
12 Principal = { Service = "sagemaker.amazonaws.com" }
13 Action = "sts:AssumeRole"
14 }]
15 })
16}
17
18resource "aws_iam_role_policy_attachment" "sagemaker" {
19 role = aws_iam_role.exec.name
20 policy_arn = "arn:aws:iam::aws:policy/AmazonSageMakerFullAccess"
21}
22
23# Model from the Marketplace package
24resource "aws_sagemaker_model" "this" {
25 name = var.endpoint_name
26 execution_role_arn = aws_iam_role.exec.arn
27 enable_network_isolation = true
28
29 primary_container {
30 model_package_name = var.model_package_arn
31 }
32}
33
34resource "aws_sagemaker_endpoint_configuration" "this" {
35 name = var.endpoint_name
36
37 production_variants {
38 variant_name = "AllTraffic"
39 model_name = aws_sagemaker_model.this.name
40 instance_type = var.instance_type
41 initial_instance_count = var.instance_count
42 }
43}
44
45resource "aws_sagemaker_endpoint" "this" {
46 name = var.endpoint_name
47 endpoint_config_name = aws_sagemaker_endpoint_configuration.this.name
48}
49
50output "endpoint_name" {
51 value = aws_sagemaker_endpoint.this.name
52}

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>"