Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
ELB Automatic resource naming
Browse files Browse the repository at this point in the history
This change removes settings LoadBalancerName manually and
uses the AWS auto naming instead.

* Do not set LoadBalancerName manually, use AWS auto naming
* Add elb dns names as outputs
* Pass troposphere template to elb function so that resources
and outputs can be set up internal to the function
* Update tests for elb method requiring template and resultant
need to seperate out resource componenents for comparison
* PEP8 fixes for test.py and test_iam.py

(Closes #136)
  • Loading branch information
Niall Creech authored and ashb committed Aug 7, 2015
1 parent 1ba47c8 commit bf4ea2c
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 42 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,12 @@
This is used by bootstrap-salt to remove a file that it places and manages
in an S3 bucket so that the stack can be cleanly deleted.

* Let Cloudformation name the ELBs automatically to make creating multiple
stacks easier.

The 'name' parameter in each load balancer config is now only used to
generate the DNS entries.

## Version 0.5.6
* Automaticly generate the RDS identifier

Expand Down
55 changes: 38 additions & 17 deletions bootstrap_cfn/config.py
Expand Up @@ -67,10 +67,7 @@ def process(self):
map(template.add_resource, ec2)

if 'elb' in self.data:
elbs, sgs = self.elb()
map(template.add_resource, elbs)
map(template.add_resource, sgs)
template = self._attach_elbs(template)
self.elb(template)

if 'rds' in self.data:
self.rds(template)
Expand Down Expand Up @@ -257,7 +254,7 @@ def s3(self, template):
Args:
template:
The cloudformation template file
The troposphere.Template object
"""
# As there are no required fields, although we may not have any
# subkeys we still need to be able to have a parent key 's3:' to
Expand Down Expand Up @@ -311,12 +308,12 @@ def ssl(self):
def rds(self, template):
"""
Create an RDS resource configuration from the config file data
and add it to the troposphere template. Outputs for the RDS name,
and add it to the troposphere.Template. Outputs for the RDS name,
host and port are created.
Args:
template:
The cloudformation template file
The troposphere.Template object
"""
# REQUIRED FIELDS MAPPING
required_fields = {
Expand Down Expand Up @@ -400,12 +397,23 @@ def rds(self, template):
Value=GetAtt(rds_instance, "Endpoint.Port")
))

def elb(self):
def elb(self, template):
"""
Create an ELB resource configuration from the config file data
and add them to the troposphere template. Outputs for each ELB's
DNSName are created.
Args:
template:
The cloudformation template file
"""
# REQUIRED FIELDS AND MAPPING
# Note, 'name' field is used internally to help label
# logical ids, and as part of the DNS record name.
required_fields = {
'listeners': 'Listeners',
'scheme': 'Scheme',
'name': 'LoadBalancerName',
'name': None,
'hosted_zone': 'HostedZoneName'
}

Expand All @@ -425,7 +433,6 @@ def elb(self):
Subnets=[Ref("SubnetA"), Ref("SubnetB"), Ref("SubnetC")],
Listeners=elb['listeners'],
Scheme=elb['scheme'],
LoadBalancerName=self._get_elb_canonical_name(elb['name']),
ConnectionDrainingPolicy=ConnectionDrainingPolicy(
Enabled=True,
Timeout=120,
Expand Down Expand Up @@ -502,7 +509,8 @@ def elb(self):
Ref("AWS::Region"),
":",
Ref("AWS::AccountId"),
':loadbalancer/%s' % load_balancer.LoadBalancerName
':loadbalancer/',
Ref(load_balancer)
])
],
"Effect": "Allow"}
Expand Down Expand Up @@ -544,7 +552,20 @@ def elb(self):
)
load_balancer.SecurityGroups = [Ref(sg)]
elb_sgs.append(sg)
return elb_list, elb_sgs

# Add outputs
output_name = "ELB" + safe_name
logging.debug("config:elb:Adding output to ELB '%s'" % (output_name))
template.add_output(Output(
output_name,
Description="ELB DNSName",
Value=GetAtt(load_balancer, "DNSName")
))

# Update template with ELB resources
map(template.add_resource, elb_list)
map(template.add_resource, elb_sgs)
template = self._attach_elbs(template)

def _convert_ref_dict_to_objects(self, o):
"""
Expand Down Expand Up @@ -725,10 +746,10 @@ def _attach_elbs(self, template):
return template
asgs = self._find_resources(template,
'AWS::AutoScaling::AutoScalingGroup')
elbs = self._find_resources(template,
'AWS::ElasticLoadBalancing::LoadBalancer')

asgs[0].LoadBalancerNames = [x.LoadBalancerName for x in elbs]
template.resources[asgs[0].title] = asgs[0]
if len(asgs) > 0:
elbs = self._find_resources(template,
'AWS::ElasticLoadBalancing::LoadBalancer')
asgs[0].LoadBalancerNames = [Ref(x) for x in elbs]
template.resources[asgs[0].title] = asgs[0]

return template

0 comments on commit bf4ea2c

Please sign in to comment.