pulumi / pulumi-yaml

YAML language provider for Pulumi
Apache License 2.0
38 stars 11 forks source link

support explicit typing for variables, or figure out how to auto-cast as required #238

Open allixsenos opened 2 years ago

allixsenos commented 2 years ago

Hello!

Issue details

Some AWS constructs are very sensitive about their datatypes (number vs string).

aws:lb:TargetGroup properties.port expects a number, complains if it's a string, expects a number aws-native:ecs:TaskDefinition properties.containerDefinitions.portMappings.containerPort (and .hostPort) complain if it's a string, expect a number

but ...

aws-native:ecs:TaskDefinition properties.containerDefinitions.environment.value (and .hostPort) will complain if it's a number

I would like to feed the same variable into both portMappings and environment. I haven't found a better way than defining the same variable twice, with String appended to the name of the second one, and wrapping it in quotes :(

like so:

variables:
  AppListenPort: 8000
  AppListenPortString: '8000'
  HaproxyListenPort: 8080
  HaproxyListenPortString: '8080'

so that I can

resources:
  TaskDefinition:
    type: aws-native:ecs:TaskDefinition
    properties:
      containerDefinitions:
          - name: haproxy
            portMappings:
              - containerPort: ${HaproxyListenPort}
                hostPort: ${HaproxyListenPort}
            environment:
              - name: HAPROXY_APP_PORT
                value: ${AppListenPortString}
              - name: HAPROXY_LISTEN_PORT
                value: ${HaproxyListenPortString}

Affected area/feature

iwahbe commented 2 years ago

Thanks for opening an issue. This is definitely something we are thinking about! I think you can abuse Fn::ToJSON as a work around right now. It will cast numbers to strings.

Define your variables as numbers:

variables:
  AppListenPort: 8000
  HaproxyListenPort: 8080

and then cast them to strings by passing them to Fn::ToJSON:

resources:
  TaskDefinition:
    type: aws-native:ecs:TaskDefinition
    properties:
      containerDefinitions:
          - name: haproxy
            portMappings:
              - containerPort: ${HaproxyListenPort}
                hostPort: ${HaproxyListenPort}
            environment:
              - name: HAPROXY_APP_PORT
                value:
                  Fn::ToJSON: ${AppListenPort}
              - name: HAPROXY_LISTEN_PORT
                value: 
                  Fn::ToJSON: ${HaproxyListenPort}
ringods commented 10 months ago

@iwahbe I have a customer with a need of going from string to integer. In below snippet, the stdout from command:local:run is typed string. Now the string represents an integer and it should be passed as type integer to the Redis instance:

variables:
  redisSize:
    fn::invoke:
      function: command:local:run
      arguments:
        command: yq ".${env}.redisSize" < extra-vars.yaml
      return: stdout

resources:
  redis:
    type: gcp:redis:Instance
    properties:
      memorySizeGb: ${redisSize}
      ...

A toNumber and toBool function would be handy.

iwahbe commented 10 months ago

CC @pulumi/platform for ☝️