You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Terraform为每个SageMaker用户创建动态aws_sagemaker_user_profile模块

批量生成SageMaker User Profile的Terraform实现方法

步骤1:定义批量用户变量

在variables.tf中添加一个列表类型的变量,用于接收所有需要创建的用户名:

variable "sagemaker_user_profiles" {
  type        = list(string)
  description = "需要创建的SageMaker用户配置文件名称列表"
}

步骤2:使用for_each动态生成资源

修改原有的aws_sagemaker_user_profile资源块,通过for_each遍历变量列表,自动为每个用户生成对应的资源:

resource "aws_sagemaker_user_profile" "user_profile" {
  for_each          = toset(var.sagemaker_user_profiles)
  domain_id         = aws_sagemaker_domain.sagemaker_domain.id
  user_profile_name = each.value
}

步骤3:在tfvars中配置用户名列表

在terraform.tfvars(或自定义的tfvars文件)中填入需要创建的用户名:

sagemaker_user_profiles = ["alice", "bob", "charlie"]

关键说明

  • toset()函数把列表转换为集合,避免重复用户名(若存在重复,Terraform会直接报错提示)
  • 每个生成的资源会以用户名为唯一标识,比如aws_sagemaker_user_profile.user_profile["alice"],便于后续单独引用或管理
  • 执行terraform apply时,Terraform会自动为列表中的每个用户创建对应的SageMaker User Profile资源

内容的提问来源于stack exchange,提问作者Carlos Gonzalez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 11:32:06