Skip to content

Python synthesizer generates empty if blocks for conditional resources #1139

@sai-ray

Description

@sai-ray

When a conditional resource has no metadata, update_policy, deletion_policy, or dependencies, the Python synthesizer generates an empty if block which is a syntax error.

It should either emit pass or skip the if block entirely when there are no attributes to emit.

Can be reproduced by any CloudFormation template with a conditional resource that has no Metadata, UpdatePolicy, DeletionPolicy, or DependsOn.

For example:

Input

AWSTemplateFormatVersion: "2010-09-09"
Conditions:
  CreateResource:
    !Equals [!Ref "AWS::Region", "us-east-1"]
Resources:
  ConditionalBucket:
    Type: AWS::S3::Bucket
    Condition: CreateResource
    Properties:
      BucketName: test-bucket'

Output

from aws_cdk import Stack
import aws_cdk as cdk
import aws_cdk.aws_s3 as s3
from constructs import Construct

class NoctStack(Stack):
  def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
    super().__init__(scope, construct_id, **kwargs)

    # Conditions
    create_resource = self.region == 'us-east-1'

    # Resources
    conditionalBucket = s3.CfnBucket(self, 'ConditionalBucket',
          bucket_name = 'test-bucket',
        ) if create_resource else None
    if (conditionalBucket is not None):

The file ends with if (conditionalBucket is not None):. Nothing after the if statement. Python requires at least pass inside an if block, otherwise it's a syntax error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions