Terraform创建AWS SageMaker项目报资源未声明错误如何修复
问题修复方案
报错核心原因是Terraform资源引用语法使用错误。aws_servicecatalog_product.<资源本地名>的写法仅用于引用当前Terraform配置文件中已经声明、由当前栈管理的Service Catalog产品资源,不能直接把手动创建的产品ID拼在这个引用格式里。
根据你的实际场景,选择对应操作即可修复:
场景1:需要用当前Terraform栈管理这个Service Catalog产品
你需要先在配置中声明aws_servicecatalog_product资源,再在SageMaker项目配置中引用该资源的ID属性,示例配置如下:
# 声明Service Catalog产品资源 resource "aws_servicecatalog_product" "example" { name = "sagemaker-project-template" owner = "cloud-admin" type = "CLOUD_FORMATION_TEMPLATE" # 按实际需求补充产品配置,比如预置制品参数、标签等 } # 声明SageMaker项目资源 resource "aws_sagemaker_project" "example" { project_name = "example" service_catalog_provisioning_details { product_id = aws_servicecatalog_product.example.id } }
场景2:Service Catalog产品已存在,不需要当前Terraform栈管理
也就是你已经提前在AWS侧创建好ID为prod-xxxxxxxxxxxxx的产品的情况,有两种合法写法:
- 最简写法:直接把产品ID作为字符串传入
product_id字段
resource "aws_sagemaker_project" "example" { project_name = "example" service_catalog_provisioning_details { product_id = "prod-xxxxxxxxxxxxx" } }
- 规范写法:通过数据源查询已存在的产品后再引用,适合需要关联引用产品其他属性、做前置校验的场景
# 数据源查询已存在的Service Catalog产品 data "aws_servicecatalog_product" "existing" { id = "prod-xxxxxxxxxxxxx" } resource "aws_sagemaker_project" "example" { project_name = "example" service_catalog_provisioning_details { product_id = data.aws_servicecatalog_product.existing.id } }
配置修改完成后,重新执行terraform plan即可消除该报错。
内容的提问来源于stack exchange,提问作者awscoder
相关产品推荐
相关产品推荐

