This message was deleted.
# python
s
This message was deleted.
m
Hi there — have a look at this example. It looks similar to your code, so might help: https://github.com/pulumi/examples/blob/258d3bad0a00020704743e37911c51be63c06bb4/aws-py-serverless-raw/__main__.py#L73-L89
This, for example, seems to work for me:
Copy code
some_dict = {
  "THING": "one",
  "OTHER_THING": 2,
}

fn = aws.lambda_.Function("fn",
    runtime="python3.9",
    handler="handler.handler",
    role=role.arn,
    code=pulumi.FileArchive("./function"),
    environment=aws.lambda_.FunctionEnvironmentArgs(
        variables=some_dict,
    ))
When deployed, I see the environment variables:
o
Hi, @miniature-musician-31262 Thank you for your reply, I can understand this code why not warring. https://github.com/pulumi/examples/blob/258d3bad0a00020704743e37911c51be63c06bb4/aws-py-serverless-raw/__main__.py#L73-L89. But for your code.
Copy code
some_dict = {
  "THING": "one",
  "OTHER_THING": 2,
}

fn = aws.lambda_.Function("fn",
    runtime="python3.9",
    handler="handler.handler",
    role=role.arn,
    code=pulumi.FileArchive("./function"),
    environment=aws.lambda_.FunctionEnvironmentArgs(
        variables=some_dict,
    ))
I can not understand, It looks like my codes, but it works! Just eluded pylance’s inspection?
so, There is no way to convert str to pulumi.Input[str] ?
m
In general you shouldn’t have to manually cast anything from a string to an input of string, as plain strings are also valid inputs. Where are your
s3_zip_log_bucket_name
and
db_password
values coming from, though? Are they plain strings, or outputs of some other resource?
f
The warning is easy to fix: make sure the values are strings
Copy code
...
                "RDS_PORT": "3306",
                "TTL": "172800",
...
these will be env vars anyway, so they will be strings.
However, it's weird that if you do this
Copy code
variables = {
      "DESTN_BUCKET": "blah",                
      "RDS_HOSTNAME": "<http://xxxx.ap-east-1.rds.amazonaws.com|xxxx.ap-east-1.rds.amazonaws.com>",
      "RDS_USERNAME": "admin",
      "RDS_PASSWORD": "blah",
      "RDS_PORT": "3306",
      "TTL": 172800,
}
then it works. It seems, the variables type is inferred as
dict[str, Unknown]
Copy code
variables = {                                                                
    "DES (variable) variables: dict[str, Unknown]
but if you define the type properly, then it becomes an error:
Copy code
variables: dict[str, str] = { 
      "DESTN_BUCKET": "blah",
      "RDS_HOSTNAME": "<http://xxxx.ap-east-1.rds.amazonaws.com|xxxx.ap-east-1.rds.amazonaws.com>",
      "RDS_USERNAME": "admin",
      "RDS_PASSWORD": "blah",
      "RDS_PORT": "3306",
E     "TTL": 172800,     ■ Expression of type "dict[str, str | int]" cannot be assigned to declared type "dict[str, str]"    "Literal[172800]" is incompatible with "str"
  }
Add quotes to TTL and it works 🙂
I think of an
Input[str]
as either
str
or
Output[str]
In fact, in the SDK it's defined as a Union
Copy code
Input = Union[T, Awaitable[T], "Output[T]"]
o
@miniature-musician-31262 Thanks for reminding me! The
s3_zip_log_bucket_name
and
db_password
values are string too, I find the key is
Copy code
"TTL": 172800
When I remove this line, It is fine.
@fierce-ability-58936 Thank you for your reply, you are right! I just realized how stupid my question is.
👍 1
f
There are no stupid questions 🙂
☝️ 1
716 Views