I'm trying to learn Pulumi but this issue has been...
# python
b
I'm trying to learn Pulumi but this issue has been driving my mad - already spent 6 hours trying to find the right way to pass the bucket name in the even pattern - please can someone look into this I've tried a variety of variations to get the bucket name but all produce the same error: The line that's giving me grief is:
pulumi.Output.all(bucket.id).apply(lambda y: f"{y}")
- also tried:
Copy code
import json
import pulumi
import pulumi_aws as aws

# Create an AWS resource (S3 Bucket)
bucket = aws.s3.Bucket('kollective-data')

# Export the name of the bucket
pulumi.export('bucket_name', bucket.id)

# Create an event rule to watch for events.
s3_rule = aws.cloudwatch.EventRule("rule",
                                   event_pattern=json.dumps({"source": ["aws.s3"],
                                                             "detail-type": ["Object Created"],
                                                             "bucket": [pulumi.Output.all(bucket.id).apply(lambda y: f"{y}")],
                                                             "object": {
                                                                 "key": [{
                                                                     "prefix": "user-data/roger/"
                                                                 }
                                                                 ]
                                                             }

                                                             })
                                   )
error message:
Copy code
(kollective-1g8s_x9V-py3.10) C:\Users\sashi\PycharmProjects\Kollective\kollective_iac>pulumi up
        event_pattern=json.dumps({"source": ["aws.s3"],
      File "C:\Users\sashi\.pyenv\pyenv-win\versions\3.10.4\lib\json\__init__.py", line 231, in dumps
        return _default_encoder.encode(obj)
      File "C:\Users\sashi\.pyenv\pyenv-win\versions\3.10.4\lib\json\encoder.py", line 199, in encode
        chunks = self.iterencode(o, _one_shot=True)
      File "C:\Users\sashi\.pyenv\pyenv-win\versions\3.10.4\lib\json\encoder.py", line 257, in iterencode
        return _iterencode(o, 0)
      File "C:\Users\sashi\.pyenv\pyenv-win\versions\3.10.4\lib\json\encoder.py", line 179, in default
        raise TypeError(f'Object of type {o.__class__.__name__} '
    TypeError: Object of type Output is not JSON serializable
r
You'll need the apply outside the
json.dumps
Maybe something like this;
Copy code
def build_event_pattern(bucket_id):
    return json.dumps({
        "source": ["aws.s3"],
        "detail-type": ["Object Created"],
        "bucket": bucket_id,
        "object": {
            "key": [{
                "prefix": "user-data/roger/"
            }]
        }
    })
    
s3_rule = aws.cloudwatch.EventRule("rule",   event_pattern=bucket.id.apply(build_event_pattern)
)
Actually you can use
pulum.Output.json_dumps
like documented here: https://www.pulumi.com/docs/intro/concepts/inputs-outputs/#converting-outputs-to-json
b
Awesome - thank you very much @red-match-15116