Unable to Figure out OR/Equals Condition in Troposphere

660 views
Skip to first unread message

Justin Seiser

unread,
Jul 11, 2017, 12:19:08 PM7/11/17
to cloudtools-dev
All,

Im sure this is dead easy, but I am really drawing a blank between reading the troposphere docs and the cloudformation docs.  In short.  I have a template, which I am not adding support for deploying Aurora as well as the usual mysql setup.  so I have to add some conditions to check if I need to do a `t.add_resource(DBInstance` or a `t.add_resource(DBCluster(` and then add the DBInstance to it.  So what I have, is DBEngine as a input parameter.  if DBEngine is EQUAL to 'aurora' I am using that to set a condition and configure the RDS Cluster/Instance.  My trouble is, how to catch and create just a DB Instance if its NOT equal to aurora.

Paramter

SQLEngine = t.add_parameter(Parameter(
   
"SQLEngine",
   
Default="mysql",
   
Type="String",
   
ConstraintDescription="Must be True or False.",
   
Description="Mutli AZ",
   
AllowedValues=["mysql", "aurora", "mariadb"],
))

t
.add_condition("isAuroraCondition", Equals(Ref(SQLEngine), "aurora"))
t
.add_condition("isNotAuroraCondition", Not(Ref(SQLEngine), "mysql")) <-- This needs to be NOT aurora, or Both Mysql and Mariadb.
 

Then my actual code looks like this to create a Aurora Cluster or just a single non aurora Instance

# RDS
# IS Aurora

RDSCluster = t.add_resource(DBCluster(
   
"RDSCluster",
   
Condition="isAuroraCondition",
   
Engine=Ref(SQLEngine),
   
Tags=Tags(
       
Customer=Ref(Customer),
       
Environment=Ref(Environment),
       
Name=Join("-", ["rdscluster", Ref(Customer), "db", Ref(Environment)]),
   
),
   
MasterUsername="username",
   
MasterUserPassword=Ref(Password),
   
VPCSecurityGroups=[Ref("SecurityGroupRDS")],
   
DataBaseName="temp",
   
DBSubnetGroupName=Ref("RDSSubnetGroup"),
   
BackupRetentionPeriod="7",
   
DependsOn="RDSSubnetGroup",
))


RDS
= t.add_resource(DBInstance(
   
"RDS",
   
Condition="isAuroraCondition",
   
DBClusterIdentifier=Ref(RDSCluster),
   
Engine=Ref(SQLEngine),
   
MultiAZ=If("RDSMultiAZCondition", True, False),
   
AvailabilityZone=If("RDSMultiAZCondition", Ref("AWS::NoValue"), Select("0", GetAZs(Ref("AWS::Region")))),
   
PubliclyAccessible="false",
   
AllowMajorVersionUpgrade="true",
   
Tags=Tags(
       
Customer=Ref(Customer),
       
Environment=Ref(Environment),
       
Name=Join("-", ["rds", Ref(Customer), "db", Ref(Environment)]),
   
),
   
MasterUsername="username",
   
MasterUserPassword=Ref(Password),
   
AutoMinorVersionUpgrade="true",
   
VPCSecurityGroups=[Ref("SecurityGroupRDS")],
   
DBName="temp",
   
DBInstanceClass=Ref(RDSClass),
   
DBSubnetGroupName=Ref("RDSSubnetGroup"),
   
DBInstanceIdentifier=Join("-", ["rds", Ref(Customer), "db", Ref(Environment)]),
   
DependsOn="RDSSubnetGroup",
))

# Is Not Aurora

RDS
= t.add_resource(DBInstance(
   
"RDS",
   
Condition="isNotAuroraCondition",
   
Engine=Ref(SQLEngine),
   
MultiAZ=If("RDSMultiAZCondition", True, False),
   
AvailabilityZone=If("RDSMultiAZCondition", Ref("AWS::NoValue"), Select("0", GetAZs(Ref("AWS::Region")))),
   
PubliclyAccessible="false",
   
AllowMajorVersionUpgrade="true",
   
Tags=Tags(
       
Customer=Ref(Customer),
       
Environment=Ref(Environment),
       
Name=Join("-", ["rds", Ref(Customer), "db", Ref(Environment)]),
   
),
   
MasterUsername="username",
   
MasterUserPassword=Ref(Password),
   
AutoMinorVersionUpgrade="true",
   
VPCSecurityGroups=[Ref("SecurityGroupRDS")],
   
DBName="temp",
   
AllocatedStorage="100",
   
DBInstanceClass=Ref(RDSClass),
   
DBSubnetGroupName=Ref("RDSSubnetGroup"),
   
StorageType="gp2",
   
BackupRetentionPeriod="7",
   
DBInstanceIdentifier=Join("-", ["rds", Ref(Customer), "db", Ref(Environment)]),
   
DependsOn="RDSSubnetGroup",
))

# RDS Subnet Group

RDSSubnetGroup = t.add_resource(DBSubnetGroup(
   
"RDSSubnetGroup",
   
SubnetIds=[Ref("Subnet0Private1"), Ref("Subnet2Private2")],
   
DBSubnetGroupDescription="RDS Subnet Group",
   
Tags=Tags(
       
Customer=Ref(Customer),
       
Environment=Ref(Environment),
       
Name=Join("-", ["rds", Ref(Customer), "dbsubnet", Ref(Environment)]),
   
),
))

Any helps would be great.

Hugo Lopes Tavares

unread,
Jul 11, 2017, 12:23:41 PM7/11/17
to Justin Seiser, cloudtools-dev
I think you negate the first conditional, like the following:

t.add_condition("isAuroraCondition", Equals(Ref(SQLEngine), "aurora"))

t
.add_condition("isNotAuroraCondition", Not(Condition("isAuroraCondition")))



--
You received this message because you are subscribed to the Google Groups "cloudtools-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cloudtools-dev+unsubscribe@googlegroups.com.
To post to this group, send email to cloudtools-dev@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cloudtools-dev/f2e046ba-737c-4c88-8292-c020fc9328f7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Justin Seiser

unread,
Jul 11, 2017, 1:55:39 PM7/11/17
to cloudtools-dev, justin...@gmail.com

ugh, im an idiot.  Thanks.

Ill give this a try. 
To unsubscribe from this group and stop receiving emails from it, send an email to cloudtools-de...@googlegroups.com.
To post to this group, send email to cloudto...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages