我正在尝试创建一些资源并需要强制执行某种创建顺序。例如,创建一个aws.s3.Bucket用于存储日志,然后才能将其用作aws.cloudfront.Distribution.
使用Pulumi时如何控制资源创建顺序?
通常,Pulumi 会自动处理资源创建的顺序。在 TypeScript 中,这甚至由语言的类型系统通过pulumi.Input<T>和pulumi.Output<T>类型强制执行。但了解这些类型的细节实际上并不是必需的。
Pulumi 引擎将解析资源的所有“参数”或“输入”。因此,如果您在配置另一个资源时使用一个资源作为参数,则将首先创建依赖资源。即它以您希望的方式工作。
但是,在某些情况下,您需要明确地将一种资源标记为依赖于另一种资源。当 Pulumi 程序之外存在某种耦合时,就会发生这种情况。
要指定显式依赖项,您可以为pulumi.ResourceOptions资源提供一个实例,并设置其dependsOn属性。Pulumi 引擎会dependsOn在处理资源之前解析数组中的所有资源。
这是一个简单的例子,展示了 Pulumi 确定排序的这两种方式。AWS S3 存储桶是一种包含文件的资源,称为对象。必须先创建存储桶,然后才能在其中创建任何对象。
// Create a bucket named "example-bucket", available at s3://example-bucket.
let bucket = new aws.s3.Bucket("bucket",
{
bucket: "example-bucket",
});
let file1 = new aws.s3.BucketObject("file1", {
// The bucket field of BucketObjectArgs is an instance of
// aws.s3.Bucket. Pulumi will know to create the "bucket"
// resource before this BucketObject resource.
bucket: bucket,
});
let file2 = new aws.s3.BucketObject("file2",
{
// The bucket field of BucketObjectArgs is a string. So
// Pulumi does not know to block creating the file2 resource
// until the S3 bucket exists.
bucket: "example-bucket",
} as aws.s3.BucketArgs,
{
// By putting "bucket" in the "dependsOn" array here,
// the Pulumi engine will create the bucket resource before
// this file2 resource.
dependsOn: [ bucket ],
} as pulumi.ResourceOptions);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2254 次 |
| 最近记录: |