def create(event, context): """ Runs on Stack Creation. As there is no real 'resource', and it will never be replaced, PhysicalResourceId is set to a hash of StackId and LogicalId. """ s = '%s-%s' % (event.get('StackId'), event.get('LogicalResourceId')) physical_resource_id = md5(s.encode('UTF-8')).hexdigest() if event.get('ResourceType') == 'Custom::ExpungeVPC': ec2 = ExpungeDefaultVPC(event, logger) logger.info("Deleting VPCs and dependencies - CR Router") response = ec2.expunge_default_vpc() logger.info("Response from ExpungeVPC CR Handler") logger.info(response) return physical_resource_id, response elif event.get('ResourceType') == 'Custom::EC2KeyPair': ec2 = KeyPair(event, logger) logger.info("Creating EC2 Key Pair - CR Router") response = ec2.create_key_pair() logger.info("Response from KeyPair CR Handler") logger.info(response) return physical_resource_id, response elif event['ResourceType'] == 'Custom::SSMParameters': logger.info(event) ssm = GetParameters(event, logger) logger.info("Running SSM Parameter Values Handler Event - CR Router") response = ssm.get_parameter_values() logger.info("Response from SSM Parameter Values Handler") logger.info(response) return physical_resource_id, response elif event['ResourceType'] == 'Custom::VPCCalculator': logger.info(event) vpc = VPCCalculator(event, logger) logger.info("Running VPC Calculator - CR Router") response = vpc.calculate_vpc_parameters() logger.info("Response from Calculate VPC Parameters Handler") logger.info(response) return physical_resource_id, response elif event['ResourceType'] == 'Custom::VPCPeering': logger.info(event) vpc = VPCPeering(event, logger) if event.get('ResourceProperties', {}).get('RouteTableIDs') is not None: logger.info("Running VPC Peer Routing - CR Router") response = vpc.create_vpc_peering_routing() else: logger.info("Running VPC Peer Connection - CR Router") response = vpc.create_vpc_peering_connection() logger.info("Response from VPC Peering Handler") logger.info(response) return physical_resource_id, response else: logger.error('No valid ResourceType found! Resource type \"' + event['ResourceType'] + '\" received', exc_info=True) raise Exception('No valid ResourceType found! Resource type \"' + event['ResourceType'] + '\" received')
def update(event, context): """ Runs on Stack Update """ physical_resource_id = event['PhysicalResourceId'] if event.get('ResourceType') == 'Custom::ExpungeVPC': ec2 = ExpungeDefaultVPC(event, logger) logger.info("Deleting VPCs and dependencies - CR Router") response = ec2.expunge_default_vpc() logger.info("Response from ExpungeVPC CR Handler") logger.info(response) return physical_resource_id, response elif event['ResourceType'] == 'Custom::VPCCalculator': logger.info(event) vpc = VPCCalculator(event, logger) logger.info("Running VPC Calculator - CR Router") response = vpc.calculate_vpc_parameters() logger.info("Response from Calculate VPC Parameters Handler") logger.info(response) return physical_resource_id, response elif event['ResourceType'] == 'Custom::SSMParameters': logger.info(event) ssm = GetParameters(event, logger) logger.info("Running SSM Parameter Values Handler Event - CR Router") response = ssm.get_parameter_values() logger.info("Response from SSM Parameter Values Handler") logger.info(response) return physical_resource_id, response elif event['ResourceType'] == 'Custom::VPCPeering': logger.info(event) vpc = VPCPeering(event, logger) if event.get('ResourceProperties', {}).get('RouteTableIDs') is not None: logger.info("Running VPC Peer Routing - CR Router") response = vpc.update_vpc_peering_routing() else: logger.info("Running VPC Peer Connection - CR Router") response = vpc.update_vpc_peering_connection() logger.info("Response from VPC Peering Handler") logger.info(response) return physical_resource_id, response else: logger.error('No valid ResourceType found! Resource type \"' + event['ResourceType'] + '\" received', exc_info=True) raise Exception('No valid ResourceType found! Resource type \"' + event['ResourceType'] + '\" received')