Skip to content

Commit

Permalink
fix: lambda layer change handling (#7537)
Browse files Browse the repository at this point in the history
* fix: layer CFN generation for multiple layers

* refactor: change latestVersionLogicalId handling

* fix: update function fails when a previously assigned layer does not exists
  • Loading branch information
Attila Hajdrik authored Jun 17, 2021
1 parent 93e4a8c commit 9b7a6b7
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export async function updateLayerWalkthrough(

// select layer version
if (layerHasDeployed) {
const layerCloudState = LayerCloudState.getInstance();
const layerCloudState = LayerCloudState.getInstance(parameters.layerName);
const layerVersions = await layerCloudState.getLayerVersionsFromCloud(context, parameters.layerName);
const latestVersionText = 'Future layer versions';
const layerVersionChoices = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { updateLayerArtifacts } from '../utils/storeResources';
const removeLayerQuestion = 'Choose the Layer versions you want to remove.';

export async function removeWalkthrough(context: $TSContext, layerName: string): Promise<string | undefined> {
const layerCloudState = LayerCloudState.getInstance();
const layerCloudState = LayerCloudState.getInstance(layerName);
const layerVersionList = await layerCloudState.getLayerVersionsFromCloud(context, layerName);

// if the layer hasn't been pushed return and remove it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const askLayerSelection = async (
layerSelections = layerSelections.filter(selection => selection !== provideExistingARNsPrompt);

for (const layerName of layerSelections) {
const layerCloudState = LayerCloudState.getInstance();
const layerCloudState = LayerCloudState.getInstance(layerName);
const layerVersions = await layerCloudState.getLayerVersionsFromCloud(context, layerName);
const layerVersionChoices = layerVersions.map(mapVersionNumberToChoice);

Expand All @@ -83,10 +83,16 @@ export const askLayerSelection = async (
const previousLayerSelection = _.first(filterProjectLayers(previousSelections).filter(prev => prev.resourceName === layerName));

let defaultLayerSelection: string;

if (previousLayerSelection === undefined || previousLayerSelection.isLatestVersionSelected) {
defaultLayerSelection = defaultLayerVersionPrompt;
} else {
defaultLayerSelection = mapVersionNumberToChoice(_.first(layerVersions.filter(v => v.Version === previousLayerSelection.version)));
const previouslySelectedLayerVersion = _.first(layerVersions.filter(v => v.Version === previousLayerSelection.version));

// Fallback to defaultLayerVersionPrompt as it is possible that a function is associated with a non-existent layer version
defaultLayerSelection = previouslySelectedLayerVersion
? mapVersionNumberToChoice(previouslySelectedLayerVersion)
: defaultLayerVersionPrompt;
}

const versionSelection = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export function generateLayerCfnObj(isNewVersion: boolean, parameters: LayerPara
if (isNewVersion) {
const [shortId] = uuid().split('-');
logicalName = `${LayerCfnLogicalNamePrefix.LambdaLayerVersion}${shortId}`;
const layerCloudState = LayerCloudState.getInstance();
layerCloudState.latestVersionLogicalId = logicalName; // Store in singleton so it can be used in zipfile name
const layerCloudState = LayerCloudState.getInstance(parameters.layerName);
layerCloudState.latestVersionLogicalId = logicalName; // Store in the given layer's layerCloudState instance so it can be used in zipfile name
versionList.unshift({ LogicalName: logicalName, legacyLayer: false });
} else {
logicalName = _.first(versionList).LogicalName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import { LegacyPermissionEnum } from './layerMigrationUtils';
import { LayerVersionMetadata, PermissionEnum } from './layerParams';

export class LayerCloudState {
private static instance: LayerCloudState;
private static instances: Record<string, LayerCloudState> = {};
private layerVersionsMetadata: LayerVersionMetadata[];
public latestVersionLogicalId: string;

private constructor() {}

static getInstance(): LayerCloudState {
if (!LayerCloudState.instance) {
LayerCloudState.instance = new LayerCloudState();
static getInstance(layerName: string): LayerCloudState {
if (!LayerCloudState.instances[layerName]) {
LayerCloudState.instances[layerName] = new LayerCloudState();
}
return LayerCloudState.instance;
return LayerCloudState.instances[layerName];
}

private async loadLayerDataFromCloud(context: $TSContext, layerName: string): Promise<LayerVersionMetadata[]> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ export const packageLayer: Packager = async (context, resource) => {
await zipPackage(packageResult.zipEntries, destination);
}

const layerCloudState = LayerCloudState.getInstance();
const layerCloudState = LayerCloudState.getInstance(resource.resourceName);
if (!layerCloudState.latestVersionLogicalId) {
// "Should" never be reachable, but sanity check just in case
throw new Error('LogicalId missing for new layer version.');
throw new Error(`LogicalId missing for new layer version: ${resource.resourceName}.`);
}

const zipFilename = createLayerZipFilename(resource.resourceName, layerCloudState.latestVersionLogicalId);
Expand Down Expand Up @@ -112,6 +112,9 @@ export async function checkContentChanges(context: $TSContext, layerResources: A
for (const layer of changedLayerResources) {
let { parameters } = layer;
if (!accepted) {
context.print.info('');
context.print.info(`Change options layer: ${layer.resourceName}`);
context.print.info('');
parameters = await lambdaLayerNewVersionWalkthrough(parameters, timestampString);
} else {
parameters.description = `Updated layer version ${timestampString}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,17 @@ function ensureLayerRuntimeFolder(layerDirPath: string, runtime: LayerRuntime) {
}

function createLayerCfnFile(parameters: LayerParameters, layerDirPath: string) {
JSONUtilities.writeJson(path.join(layerDirPath, getCfnTemplateFileName(parameters.layerName)), generateLayerCfnObj(true, parameters));
const layerCfnObj = generateLayerCfnObj(true, parameters);
const layerCfnFilePath = path.join(layerDirPath, getCfnTemplateFileName(parameters.layerName));

JSONUtilities.writeJson(layerCfnFilePath, layerCfnObj);
}

async function updateLayerCfnFile(context: $TSContext, parameters: LayerParameters, layerDirPath: string): Promise<$TSObject> {
let layerVersionList: LayerVersionMetadata[] = [];

if (loadPreviousLayerHash(parameters.layerName)) {
const layerCloudState = LayerCloudState.getInstance();
const layerCloudState = LayerCloudState.getInstance(parameters.layerName);

layerVersionList = await layerCloudState.getLayerVersionsFromCloud(context, parameters.layerName);
}
Expand Down

0 comments on commit 9b7a6b7

Please sign in to comment.