def __init__(self, resource, values, export=True, duplicate_attr=False): """ Initialize the output class. :param resource: The object to export attributes for. :param bool export: """ self.object_repr = None self.duplicate_attr = duplicate_attr self.validate_input(resource, values, export, duplicate_attr) self.values = values self.outputs = [] for value in self.values: if not isinstance(value, tuple): raise TypeError( "All values should be a tuple of (str, value). Got", type(value)) validate(value) attr_name = (value[0] if not isinstance(value[0], Parameter) else value[0].title) output_ext = value[1] attr_value = value[2] stack_string = ( f"{self.stack_string_base}{self.object_repr}{self.delim}{attr_name}" ) root_string = ( f"{self.root_string_base}{self.object_repr}{self.delim}{attr_name}" ) output_name = f"{self.object_repr}{output_ext}" output = Output(output_name, Value=attr_value) if export: output.Export = Export( If(USE_STACK_NAME_CON_T, Sub(stack_string), Sub(root_string))) self.outputs.append(output) if self.duplicate_attr: output = Output(attr_name, Value=attr_value) self.outputs.append(output)
#Subnet 1b subnet_1b = ec2.Subnet( "MySubnet1b", AvailabilityZone="us-east-1b", CidrBlock="10.0.1.0/24", # ADJUST THIS VALUE VpcId=GetAtt(subnet_1a, "VpcId"), Tags=Tags( Name="learncf-1b", Comment="CloudFormation+Troposphere test")) t.add_resource(subnet_1b) #Subnet 1 Output out_subnet_1a = Output("outMySubnet1a") out_subnet_1a.Value = Ref(subnet_1a) out_subnet_1a.Export = Export(Sub( "${AWS::StackName}-" + subnet_1a.title)) # Similar output for the second subnet out_subnet_1b = Output("outMySubnet1b") out_subnet_1b.Value = Ref(subnet_1b) out_subnet_1b.Export = Export(Sub( "${AWS::StackName}-" + subnet_1b.title)) # Add outputs to template t.add_output(out_subnet_1a) t.add_output(out_subnet_1b) # Finally, write the template to a file with open('learncf-subnet.json', 'w') as f: f.write(t.to_json())