lib/stackops/functions.js
function mergeAliases(stackName, newTemplate, currentTemplate, aliasStackTemplates, currentAliasStackTemplate, removedResources) {

	const allAliasTemplates = _.concat(aliasStackTemplates, currentAliasStackTemplate);

	// Get all referenced function logical resource ids
	const aliasedFunctions =
		_.flatMap(
			allAliasTemplates,
			template => _.compact(_.map(
				template.Resources,
				(resource, name) => {
					if (resource.Type === 'AWS::Lambda::Alias') {
						return {
							name: _.replace(name, /Alias$/, 'LambdaFunction'),
							version: _.replace(_.get(resource, 'Properties.FunctionVersion.Fn::ImportValue'), `${stackName}-`, '')
						};
					}
					return null;
				}
			))
		);

	// Get currently deployed function definitions and versions and retain them in the stack update
	const usedFunctionElements = {
		Resources: _.map(aliasedFunctions, aliasedFunction => _.assign(
			{},
			_.pick(currentTemplate.Resources, [ aliasedFunction.name, aliasedFunction.version ])
		)),
		Outputs: _.map(aliasedFunctions, aliasedFunction => _.assign(
			{},
			_.pick(currentTemplate.Outputs, [ `${aliasedFunction.name}Arn`, aliasedFunction.version ])
		))
	};

	_.forEach(usedFunctionElements.Resources, resources => _.defaults(newTemplate.Resources, resources));
	_.forEach(usedFunctionElements.Outputs, outputs => _.defaults(newTemplate.Outputs, outputs));

	// Set references to obsoleted resources in fct env to "REMOVED" in case
	// the alias that is removed was the last deployment of the stage.
	// This will change the function definition, but that does not matter
	// as is is neither aliased nor versioned
	_.forEach(_.filter(newTemplate.Resources,  [ 'Type', 'AWS::Lambda::Function' ]), func => {
		const refs = utils.findReferences(func, removedResources);
		_.forEach(refs, ref => _.set(func, ref, "REMOVED"));
	});

}
function mergeAliases(stackName, newTemplate, currentTemplate, aliasStackTemplates, currentAliasStackTemplate, removedResources) {

	const allAliasTemplates = _.concat(aliasStackTemplates, currentAliasStackTemplate);

	// Get all referenced function logical resource ids
	const aliasedFunctions =
		_.flatMap(
			allAliasTemplates,
			template => _.compact(_.map(
				template.Resources,
				(resource, name) => {
					if (resource.Type === 'AWS::Lambda::Alias') {
						return {
							name: _.replace(name, /Alias$/, 'LambdaFunction'),
							version: _.replace(_.get(resource, 'Properties.FunctionVersion.Fn::ImportValue'), `${stackName}-`, '')
						};
					}
					return null;
				}
			))
		);

	// Get currently deployed function definitions and versions and retain them in the stack update
	const usedFunctionElements = {
		Resources: _.map(aliasedFunctions, aliasedFunction => _.assign(
			{},
			_.pick(currentTemplate.Resources, [ aliasedFunction.name, aliasedFunction.version ])
		)),
		Outputs: _.map(aliasedFunctions, aliasedFunction => _.assign(
			{},
			_.pick(currentTemplate.Outputs, [ `${aliasedFunction.name}Arn`, aliasedFunction.version ])
		))
	};

	_.forEach(usedFunctionElements.Resources, resources => _.defaults(newTemplate.Resources, resources));
	_.forEach(usedFunctionElements.Outputs, outputs => _.defaults(newTemplate.Outputs, outputs));

	// Set references to obsoleted resources in fct env to "REMOVED" in case
	// the alias that is removed was the last deployment of the stage.
	// This will change the function definition, but that does not matter
	// as is is neither aliased nor versioned
	_.forEach(_.filter(newTemplate.Resources,  [ 'Type', 'AWS::Lambda::Function' ]), func => {
		const refs = utils.findReferences(func, removedResources);
		_.forEach(refs, ref => _.set(func, ref, "REMOVED"));
	});

}
function mergeAliases ( stackName , newTemplate , currentTemplate , aliasStackTemplates , currentAliasStackTemplate , removedResources ) { const allAliasTemplates = _ . concat ( aliasStackTemplates , currentAliasStackTemplate ) ; // Get all referenced function logical resource ids const aliasedFunctions = _ . flatMap ( allAliasTemplates , template => _ . compact ( _ . map ( template . Resources , ( resource , name ) => { if ( resource . Type === 'AWS::Lambda::Alias' ) { return { name : _ . replace ( name , / Alias$ / , 'LambdaFunction' ) , version : _ . replace ( _ . get ( resource , 'Properties.FunctionVersion.Fn::ImportValue' ) , ` ${ stackName } ` , '' ) } ; } return null ; } ) ) ) ; // Get currently deployed function definitions and versions and retain them in the stack update const usedFunctionElements = { Resources : _ . map ( aliasedFunctions , aliasedFunction => _ . assign ( { } , _ . pick ( currentTemplate . Resources , [ aliasedFunction . name , aliasedFunction . version ] ) ) ) , Outputs : _ . map ( aliasedFunctions , aliasedFunction => _ . assign ( { } , _ . pick ( currentTemplate . Outputs , [ ` ${ aliasedFunction . name } ` , aliasedFunction . version ] ) ) ) } ; _ . forEach ( usedFunctionElements . Resources , resources => _ . defaults ( newTemplate . Resources , resources ) ) ; _ . forEach ( usedFunctionElements . Outputs , outputs => _ . defaults ( newTemplate . Outputs , outputs ) ) ; // Set references to obsoleted resources in fct env to "REMOVED" in case // the alias that is removed was the last deployment of the stage. // This will change the function definition, but that does not matter // as is is neither aliased nor versioned _ . forEach ( _ . filter ( newTemplate . Resources , [ 'Type' , 'AWS::Lambda::Function' ] ) , func => { const refs = utils . findReferences ( func , removedResources ) ; _ . forEach ( refs , ref => _ . set ( func , ref , "REMOVED" ) ) ; } ) ; }
Merge template definitions that are still in use into the new template
@param stackName {String} Main stack name
@param newTemplate {Object} New main stack template
@param currentTemplate {Object} Currently deployed main stack template
@param aliasStackTemplates {Array<Object>} Currently deployed and references aliases
Merge template definitions that are still in use into the new template