Code Monkey home page Code Monkey logo

Comments (2)

apparentlymart avatar apparentlymart commented on June 30, 2024

Hi @dbogen,

This is an unfortunate quirk of Terraform v0.11 and earlier: all provider results are either strings, lists of strings, or maps of strings. The bug here is that format should see that you used %d and automatically try to convert the string result to an integer, as all other language features do, but instead (because in v0.11 format is based on Go's formatter) it leaves it as a string but produces invalid output.

This is fixed in both regards in the master branch of Terraform, in preparation for the forthcoming v0.12.0 release:

  • Providers now return full type information for their attributes, allowing Terraform Core to see that the result of random_integer is a number rather than a string.
  • The format function is no longer based on Go's own fmt package and instead is a custom implementation that understands how to do all of the standard type conversions used in the Terraform language, so that %d can be used with strings containing decimal digits just as is the case for arithmetic operators on strings, etc.

The random provider won't need to change at all to benefit from this when running in v0.12.0 mode, except to support the new v0.12.0 plugin protocol. The latest version of this provider is already compatible, so no further changes are required here for it to work. The example you tried here should therefore work as expected in v0.12.0-beta1 and in the forthcoming v0.12.0 release, without any further changes.

To work around this in Terraform v0.11, I'd suggest performing some no-op arithmetic on the result to force the language to convert to integer. For example:

  bar_int = "bar-${format("%05d", floor(random_integer.foo.result))}"

Although that will no longer be necessary in Terraform v0.12.0, that additional floor function won't do any harm there, and so this workaround shouldn't cause any compatibility difficulties during upgrade, allowing you to remove it at your leisure after upgrading.

from terraform-provider-random.

kmoe avatar kmoe commented on June 30, 2024

This works as expected in Terraform 0.12 and above.

from terraform-provider-random.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.