Skip to content
This repository was archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
feat: add destroy lambda layer
Browse files Browse the repository at this point in the history
  • Loading branch information
arantespp committed Oct 24, 2020
1 parent c8dafe7 commit 2f19610
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 29 deletions.
21 changes: 10 additions & 11 deletions packages/cli/src/deploy/cloudFormation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,30 +487,29 @@ const destroy = async ({ stackName }: { stackName: string }) => {
}

if (!(await doesStackExist({ stackName }))) {
log.info(logPrefix, `Stack ${stackName} doesn't exist.`);
return;
}

if (!(await canDestroyStack({ stackName }))) {
log.info(
logPrefix,
`Stack ${stackName} cannot be destroyed while TerminationProtection is enabled.`,
);
return;
const message = `Stack ${stackName} cannot be destroyed while TerminationProtection is enabled.`;
throw message;
}

await emptyStackBuckets({ stackName });

await deleteStack({ stackName });
};

export const destroyCloudFormation = async () => {
export const destroyCloudFormation = async ({
stackName: defaultStackName,
}: {
stackName?: string;
} = {}) => {
try {
log.info(logPrefix, 'Starting CloudFormation DESTROY...');

const stackName = await getStackName();

log.info(logPrefix, 'CAUTION! Starting CloudFormation destroy...');
const stackName = defaultStackName || (await getStackName());
log.info(logPrefix, `stackName: ${stackName}`);

await Promise.all([destroy({ stackName })]);
} catch (err) {
log.error(logPrefix, 'Cannot destroy cloudformation stack.');
Expand Down
10 changes: 9 additions & 1 deletion packages/cli/src/deploy/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ export const deployCommand: CommandModule<
},
destroy: {
default: false,
description: 'Destroy the deployment.',
description:
'Destroy the deployment. You cannot destroy a deploy with "environment" is defined.',
type: 'boolean',
},
'lambda-externals': {
Expand Down Expand Up @@ -100,6 +101,13 @@ export const deployCommand: CommandModule<
alias: 't',
type: 'string',
},
'termination-protection': {
describe: [
'You can prevent a stack from being accidentally deleted by enabling termination protection.',
'Note that you cannot disable termination protection (passing false to this option) once it is already activated.',
].join(' '),
type: 'boolean',
},
})
.middleware(({ region, stackName }) => {
AWS.config.region = region;
Expand Down
59 changes: 42 additions & 17 deletions packages/cli/src/deploy/lambdaLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { CloudFormationTemplate } from '../utils/cloudFormationTemplate';
import { exec } from '../utils/exec';

import { getBaseStackBucketName } from './baseStack/getBaseStackBucketName';
import { deploy } from './cloudFormation';
import { uploadFileToS3 } from './s3';
import { deploy, destroyCloudFormation } from './cloudFormation';
import { emptyS3Directory, uploadFileToS3 } from './s3';
import { getStackName } from './stackName';

const logPrefix = 'lambda-layer';
Expand Down Expand Up @@ -43,7 +43,12 @@ export const getLambdaLayerTemplate = (): CloudFormationTemplate => {
// eslint-disable-next-line global-require, import/no-dynamic-require
return require(path.resolve(process.cwd(), 'package.json')) || {};
} catch (err) {
return {};
log.error(
logPrefix,
'Cannot read package.json. Error message: %j',
err.message,
);
return process.exit();
}
})();

Expand Down Expand Up @@ -97,26 +102,45 @@ export const getLambdaLayerTemplate = (): CloudFormationTemplate => {
/**
* The steps followed when deploying a Lambda Layer are:
*
* 1. Remove the package node_nodules.
* 2. Install node_modules again with the current packages on package.json.
* 3. Zip node_modules folder.
* 4. Upload the zipped file to Carlin bucket S3.
* 5. Deploy the Lambda Layer CloudFormation template referencing the
* uploaded zipper folder on Carlin bucket S3.
* 1. Remove the package node_nodules.
* 2. Install node_modules again with the current packages on package.json.
* 3. Zip node_modules folder.
* 4. Upload the zipped file to Carlin bucket S3.
* 5. Deploy the Lambda Layer CloudFormation template referencing the uploaded
* zipper folder on Carlin bucket S3.
*
* If the flag "destroy" is provided, these steps are performed:
*
* 1. Remove all uploaded zipped node_modules.
* 2. Destroy the CloudFormation deployment.
*/
export const deployLambdaLayer = async () => {
export const deployLambdaLayer = async ({
destroy,
terminationProtection = true,
}: {
destroy?: boolean;
terminationProtection: boolean;
}) => {
log.info(logPrefix, `Starting Lambda Layer deploy...`);
const stackName = await getStackName();
const zip = await getZipNodeModules();
const { bucket, key, versionId } = await uploadFileToS3({
bucket: await getBaseStackBucketName(),
const bucket = await getBaseStackBucketName();
const key = `lambda-layer/${stackName}/layer.zip`;

if (destroy) {
await destroyCloudFormation({ stackName });
await emptyS3Directory({ bucket, directory: key });
return undefined;
}

const { versionId } = await uploadFileToS3({
bucket,
contentType: 'application/zip' as any,
key: `lambda-layer/${stackName}/layer.zip`,
file: zip,
key,
file: await getZipNodeModules(),
});
const template = getLambdaLayerTemplate();

return deploy({
template,
template: getLambdaLayerTemplate(),
params: {
Parameters: [
{ ParameterKey: 'S3Bucket', ParameterValue: bucket },
Expand All @@ -125,5 +149,6 @@ export const deployLambdaLayer = async () => {
],
StackName: stackName,
},
terminationProtection,
});
};

0 comments on commit 2f19610

Please sign in to comment.