Pulumi DigitalOcean:液滴的不同名称

Pulumi DigitalOcean: different name for droplet

我正在使用 Pulumi 在 DigitalOcean 中创建一个 droplet。我有以下代码:

name = "server"

droplet = digitalocean.Droplet(
     name,
     image=_image,
     region=_region,
     size=_size,
)

服务器在 DigitalOcean 上成功创建,但 DigitalOcean 控制台中的名称类似于 server-0bbc405(每次执行时,它都是不同的名称)。

为什么不是我提供的名字?我怎样才能做到这一点?

这是 auto-naming 的结果,在 Pulumi 文档中有解释:

https://www.pulumi.com/docs/intro/concepts/resources/names/#autonaming

附加到资源名称末尾的额外字符允许您对多个堆栈使用相同的“逻辑”名称(您的 "server")而不会发生冲突(因为云提供商通常需要资源同类以ba命名唯一)。 Auto-naming 起初看起来有点奇怪,但它在实践中非常有用,一旦你开始使用多个堆栈,你几乎肯定会喜欢它。

也就是说,您通常可以通过在资源参数列表中提供 name 来覆盖此名称:

...
name = "server"

droplet = digitalocean.Droplet(
    name,
    name="my-name-override",     # <-- Override auto-naming
    image="ubuntu-18-04-x64",
    region="nyc2",
    size="s-1vcpu-1gb")

.. 将产生以下结果:

+ pulumi:pulumi:Stack: (create)
    ...
    + digitalocean:index/droplet:Droplet: (create)
        ...
        name            : "my-name-override"  # <-- As opposed to "server-0bbc405"
        ...

.. 但同样,出于文档中指定的原因,通常最好使用 auto-naming。此处引用:

  • It ensures that two stacks for the same project can be deployed without their resources colliding. The suffix helps you to create multiple instances of your project more easily, whether because you want, for example, many development or testing stacks, or to scale to new regions.
  • It allows Pulumi to do zero-downtime resource updates. Due to the way some cloud providers work, certain updates require replacing resources rather than updating them in place. By default, Pulumi creates replacements first, then updates the existing references to them, and finally deletes the old resources.

希望对您有所帮助!