def check_change_set_status(cf, job_id, stack, change_set): status = get_change_set_status(cf, stack, change_set) if status == 'CREATE_COMPLETE': put_job_success(job_id, 'Change set created') elif status == 'CREATE_IN_PROGRESS': continue_job_later(job_id, 'Change set still in progress') else: put_job_failure(job_id, 'Change set failed')
def check_stack_status(cf, job_id, stack): status = get_stack_status(cf, stack) if status in ['UPDATE_COMPLETE', 'CREATE_COMPLETE']: put_job_success(job_id, 'Stack completed') return True elif status in ['UPDATE_IN_PROGRESS', 'UPDATE_ROLLBACK_IN_PROGRESS', 'UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS', 'CREATE_IN_PROGRESS', 'ROLLBACK_IN_PROGRESS', 'DELETE_IN_PROGRESS', 'UPDATE_COMPLETE_CLEANUP_IN_PROGRESS']: continue_job_later(job_id, 'Stack still in progress: {}'.format(status)) elif status in ['REVIEW_IN_PROGRESS']: put_job_failure(job_id, 'Stack in REVIEW_IN_PROGRESS state') else: put_job_failure(job_id, 'Stack failed: {}'.format(status)) return False
def start_stack_create_or_update(cf, job_id, stack_name, template_url, config: PipelineStackConfig, update=False, role_arn=None): if update: status = get_stack_status(cf, stack_name) if status not in ['CREATE_COMPLETE', 'ROLLBACK_COMPLETE', 'UPDATE_COMPLETE', 'UPDATE_ROLLBACK_COMPLETE']: put_job_failure(job_id, 'Stack cannot be updated when status is: ' + status) return if update_stack(cf, stack_name, template_url, config, role_arn): continue_job_later(job_id, 'Stack update started') else: continue_job_later(job_id, 'There were no stack updates') else: create_stack(cf, stack_name, template_url, config, role_arn) continue_job_later(job_id, 'Stack create started')
def handler(event, ctx): """ The Lambda Function Handler :param event: lambda event :param ctx: lambda context :return: """ logger.info(event) job_id = None try: job_id = event['CodePipeline.job']['id'] job_data = event['CodePipeline.job']['data'] if len(job_data.get('outputArtifacts', [])) > 1: raise ValueError("Maximum number of output Artifacts is 1") params = PipelineUserParameters(job_data, ctx) in_artifacts = load_pipeline_artifacts( job_data.get('inputArtifacts', []), params.Region) if params.ActionMode == 'CREATE_UPDATE': create_update_stack_handler(job_id, job_data, params, in_artifacts) elif params.ActionMode == 'DELETE_ONLY': delete_stack_handler(job_id, job_data, params) elif params.ActionMode == 'REPLACE_ON_FAILURE': replace_stack_handler(job_id) elif params.ActionMode == 'CHANGE_SET_REPLACE': create_replace_change_set_handler(job_id, job_data, params, in_artifacts) elif params.ActionMode == 'CHANGE_SET_EXECUTE': execute_change_set_handler(job_id, job_data, params) else: raise ValueError("Unknown operation mode requested: {}".format( params.ActionMode)) except Exception as e: logger.error('Function failed due to exception. {}'.format(e)) traceback.print_exc() put_job_failure(job_id, 'Function exception: ' + str(e)) logger.debug('Function complete.') return "Complete."
def handler(event, ctx): """ The Lambda Function Handler :param event: lambda event :param ctx: lambda context :return: """ logger.info(event) job_id = None try: job_id = event['CodePipeline.job']['id'] job_data = event['CodePipeline.job']['data'] if len(job_data.get('outputArtifacts', [])) > 1: raise ValueError("Maximum number of output Artifacts is 1") params = PipelineUserParameters(job_data, ctx) in_artifacts = load_pipeline_artifacts(job_data.get('inputArtifacts', []), params.Region) if params.ActionMode == 'CREATE_UPDATE': create_update_stack_handler(job_id, job_data, params, in_artifacts) elif params.ActionMode == 'DELETE_ONLY': delete_stack_handler(job_id, job_data, params) elif params.ActionMode == 'REPLACE_ON_FAILURE': replace_stack_handler(job_id) elif params.ActionMode == 'CHANGE_SET_REPLACE': create_replace_change_set_handler(job_id, job_data, params, in_artifacts) elif params.ActionMode == 'CHANGE_SET_EXECUTE': execute_change_set_handler(job_id, job_data, params) else: raise ValueError("Unknown operation mode requested: {}".format(params.ActionMode)) except Exception as e: logger.error('Function failed due to exception. {}'.format(e)) traceback.print_exc() put_job_failure(job_id, 'Function exception: ' + str(e)) logger.debug('Function complete.') return "Complete."
def replace_stack_handler(job_id): # This operation is not implemented but can be replaced with 2 other operations - delete stack + create stack put_job_failure(job_id, 'not implemented')