如何使用terraform创建aws sagemaker项目?



这是文档中显示的地形:

resource "aws_sagemaker_project" "example" {
project_name = "example"
service_catalog_provisioning_details {
product_id = aws_servicecatalog_product.example.id
}
}

我创建了一个id为"prod-xxxxxxxxxxxxx"的服务目录产品。当我将服务目录产品id替换到上面的模板中时,获取以下内容:

resource "aws_sagemaker_project" "example" {
project_name = "example"
service_catalog_provisioning_details {
product_id = aws_servicecatalog_product.prod-xxxxxxxxxxxxx
}
}

我运行terraform plan,但是出现以下错误:

A managed resource "aws_servicecatalog_product" "prod-xxxxxxxxxxxxx" has not been declared in the root module.

我需要做什么来修复这个错误?

由于文档缺乏一点清晰度,为了使此工作像示例中一样,您将首先必须在Terraform中创建服务目录产品,例如:

resource "aws_servicecatalog_product" "example" {
name  = "example"
owner = [aws_security_group.example.id] # <---- This would need to be created first
type  = aws_subnet.main.id # <---- This would need to be created first
provisioning_artifact_parameters {
template_url = "https://s3.amazonaws.com/cf-templates-ozkq9d3hgiq2-us-east-1/temp1.json"
}
tags = {
foo = "bar"
}
}

您可以在SageMaker项目中引用它,方法与示例中相同:

resource "aws_sagemaker_project" "example" {
project_name = "example"
service_catalog_provisioning_details {
product_id = aws_servicecatalog_product.example.id
}
}

创建的每个资源都有一组属性,其他资源、数据源或输出可以根据需要访问这些属性。为了理解它是如何工作的,我强烈建议阅读关于引用值的文档[1]。由于已经创建了Service Catalog产品,因此唯一需要做的就是为产品ID提供字符串值:

resource "aws_sagemaker_project" "example" {
project_name = "example"
service_catalog_provisioning_details {
product_id = "prod-xxxxxxxxxxxxx"
}
}

当我不能理解一个参数的期望值时(例如,在这种情况下,product_id),我通常阅读文档并寻找像[2]这样的例子。注意:这个例子是CloudFormation,但它可以帮助你理解期望的值类型(例如,字符串,数字,bool)。

您还可以将创建的Service Catalog产品导入到Terraform中,以便您可以使用IaC[3]管理它。在尝试terraform import之前,您应该了解它的所有含义[4]。


[1] https://www.terraform.io/language/expressions/references

[2] https://docs.amazonaws.cn/en_us/AWSCloudFormation/latest/UserGuide/aws-resource-sagemaker-project.html aws-resource-sagemaker-project——例子SageMaker_Project_Example

[3] https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/servicecatalog_product进口

[4] https://www.terraform.io/cli/commands/import