pythonaws-cloudformationtropospheresceptre

Is there anyway to use try/catch in troposphere?


I'm using sceptre user data and I can't use AWS::NoValue with it. Since I don't want to change the current template much, I want to do a workaround but I see a warning in try line, which is Expected expression Pylance. How can I use try catch in this case? If I can't, is there any workaround this?

                LifecycleRule(
                    Id="Xxxx",
                    Status="Enabled",
                    ExpirationInDays=
                        try:
                            bucket['ExpirationInDays'] 
                        except KeyError: Ref("AWS::NoValue")
                ),
            ]),

Solution

  • Would this fragment work for you:

    LifecycleRule(Id="Xxxx", Status="Enabled",
    ExpirationInDays=bucket.get('ExpirationInDays', Ref('AWS::NoValue'))
    

    I'm assuming bucket is a Python dictionary. Using a dictionary's get() function like this avoids the need for a try/except construct