Skip to content

Commit

Permalink
Merge pull request #30 from remind101/postgres_internal_alias
Browse files Browse the repository at this point in the history
Postgres internal alias
  • Loading branch information
phobologic committed May 13, 2015
2 parents 7ad4605 + 9f0c9d6 commit e95f73f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 13 deletions.
10 changes: 10 additions & 0 deletions conf/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ stacks:
# Enough subnets for 4 AZs
PublicSubnets: 10.128.0.0/24,10.128.1.0/24,10.128.2.0/24,10.128.3.0/24
PrivateSubnets: 10.128.8.0/22,10.128.12.0/22,10.128.16.0/22,10.128.20.0/22
# Uncomment if you want an internal hosted zone for the VPC
# If provided, it will be added to the dns search path of the DHCP
# Options
#InternalDomain: internal
- name: bastion
class_path: stacker.blueprints.bastion.Bastion
parameters:
Expand All @@ -63,6 +67,12 @@ stacks:
MasterUser: dbuser
MasterUserPassword: ExamplePassword!
DBName: db1
# If the following are uncommented and you set an InternalDomain above
# in the VPC a CNAME alias of InternalHostname will be setup pointing at
# the database.
#InternalZoneId: vpc::InternalZoneId
#InternalZoneName: vpc::InternalZoneName
#InternalHostname: mydb
- name: myWeb
class_path: stacker.blueprints.asg.AutoscalingGroup
parameters:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

install_requires = [
'aws_helper>=0.2.0',
'troposphere>=0.7.1',
'troposphere>=1.0.0',
'boto>=2.25.0',
'PyYAML>=3.11',
]
Expand Down
56 changes: 49 additions & 7 deletions stacker/blueprints/postgres.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from troposphere import Ref, ec2, Output, GetAtt, Join
from troposphere import (
Ref, ec2, Output, GetAtt, Not, Equals, Condition, And, Join
)
from troposphere.rds import DBInstance, DBSubnetGroup
from troposphere.route53 import RecordSetType

from .base import Blueprint

Expand Down Expand Up @@ -34,8 +37,36 @@ class PostgresRDS(Blueprint):
'DBName': {
'type': 'String',
'description': 'Initial db to create in database.'},
"InternalZoneId": {
"type": "String",
"default": "",
"description": "Internal zone Id, if you have one."},
"InternalZoneName": {
"type": "String",
"default": "",
"description": "Internal zone name, if you have one."},
"InternalHostname": {
"type": "String",
"default": "",
"description": "Internal domain name, if you have one."},
}

def create_conditions(self):
self.template.add_condition(
"HasInternalZone",
Not(Equals(Ref("InternalZoneId"), "")))
self.template.add_condition(
"HasInternalZoneName",
Not(Equals(Ref("InternalZoneName"), "")))
self.template.add_condition(
"HasInternalHostname",
Not(Equals(Ref("InternalHostname"), "")))
self.template.add_condition(
"CreateInternalHostname",
And(Condition("HasInternalZone"),
Condition("HasInternalZoneName"),
Condition("HasInternalHostname")))

def create_subnet_group(self):
t = self.template
t.add_resource(
Expand Down Expand Up @@ -74,16 +105,27 @@ def create_rds(self):
MultiAZ=True,
PreferredBackupWindow=Ref('PreferredBackupWindow'),
VPCSecurityGroups=[Ref(RDS_SG_NAME % self.name), ]))

endpoint = GetAtt(db_name, 'Endpoint.Address')
user = Ref("MasterUser")
passwd = Ref("MasterUserPassword")
dbname = Ref("DBName")

# Setup CNAME to db
t.add_resource(
RecordSetType(
'%sDnsRecord' % db_name,
# Appends a '.' to the end of the domain
HostedZoneId=Ref("InternalZoneId"),
Comment='RDS DB CNAME Record',
Name=Join(".", [Ref("InternalHostname"),
Ref("InternalZoneName")]),
Type='CNAME',
TTL='120',
ResourceRecords=[endpoint],
Condition="CreateInternalHostname"))
t.add_output(Output('DBAddress', Value=endpoint))
db_url = Join("", ["postgres://", user, ":", passwd, "@", endpoint,
"/", dbname])
t.add_output(Output('DBURL', Value=db_url))
t.add_output(Output('DBCname', Value=Ref("%sDnsRecord" % db_name)))

def create_template(self):
self.create_conditions()
self.create_subnet_group()
self.create_security_group()
self.create_rds()
12 changes: 8 additions & 4 deletions stacker/blueprints/vpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,15 @@ def create_internal_zone(self):
t.add_resource(
HostedZone(
"EmpireInternalZone",
Name="empire",
VPCs=HostedZoneVPCs(
VPCId=Ref("VpcId"),
VPCRegion=Ref("AWS::Region")),
Name=Ref("InternalDomain"),
VPCs=[HostedZoneVPCs(
VPCId=VPC_ID,
VPCRegion=Ref("AWS::Region"))],
Condition="CreateInternalDomain"))
t.add_output(Output("InternalZoneId",
Value=Ref("EmpireInternalZone")))
t.add_output(Output("InternalZoneName",
Value=Ref("InternalDomain")))

def create_default_security_group(self):
t = self.template
Expand Down
3 changes: 2 additions & 1 deletion stacker/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ def build_stack_tags(self, stack_context, template_url):
self.get_stack_full_name(s) for s in stack_context.requires]
logger.debug("Stack %s required stacks: %s",
stack_context.name, requires)
tags = {'template_url': template_url}
tags = {'template_url': template_url,
'stacker_namespace': self.namespace}
if requires:
tags['required_stacks'] = ':'.join(requires)

Expand Down

0 comments on commit e95f73f

Please sign in to comment.