Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use provider.request instead of custom ec2 instance #8

Merged
merged 3 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 32 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"use strict";

const d = require("d")
, lazy = require("d/lazy")
, BbPromise = require("bluebird")
, Ec2 = require("aws-sdk/clients/ec2")
, { inspect } = require("util");
, lazy = require("d/lazy")
, BbPromise = require("bluebird")
, { inspect } = require("util");
onhate marked this conversation as resolved.
Show resolved Hide resolved

const noopPromise = Promise.resolve();

class ServerlessPluginVpcEniCleanup {
constructor(serverless) {
this.serverless = serverless;
this.provider = this.serverless.getProvider(this.serverless.service.provider.name);
this.hooks = {
"before:remove:remove": this.cleanup.bind(this),
"after:remove:remove": () => this.isDisabled = true
Expand All @@ -22,14 +22,13 @@ class ServerlessPluginVpcEniCleanup {
// as we want to have polling done in parallel in the background
BbPromise.all(
this.functionNames.map(functionName =>
this.ec2
.describeNetworkInterfaces({
this.provider
.request("EC2", "describeNetworkInterfaces", {
Filters: [
{ Name: "requester-id", Values: [`*:${ functionName }`] },
{ Name: "description", Values: ["AWS Lambda VPC ENI*"] }
]
})
.promise()
.then(
result =>
Promise.all(
Expand All @@ -47,34 +46,39 @@ class ServerlessPluginVpcEniCleanup {
const interfaceId = networkInterface.NetworkInterfaceId;
let detachmentPromise = noopPromise;
if (networkInterface.Attachment) {
detachmentPromise = this.ec2
.detachNetworkInterface({ AttachmentId: networkInterface.Attachment.AttachmentId })
.promise();
detachmentPromise = this.provider
.request(
"EC2",
"detachNetworkInterface",
{ AttachmentId: networkInterface.Attachment.AttachmentId }
);
}
return detachmentPromise.then(
() =>
this.ec2.deleteNetworkInterface({ NetworkInterfaceId: interfaceId }).promise().then(
() =>
this.serverless.cli.log(
"VPC ENI Cleanup: " +
this.provider
.request("EC2", "deleteNetworkInterface", { NetworkInterfaceId: interfaceId })
.then(
() =>
this.serverless.cli.log(
"VPC ENI Cleanup: " +
`Deleted ${ interfaceId } ` +
`ENI of ${ functionName } ` +
"VPC function"
),
error => {
if (error.code === "InvalidParameterValue") {
// Network interface is currently in use
// Skip on this error, as it may happen
// few times for given interface
return;
}
if (error.code === "InvalidNetworkInterfaceID.NotFound") {
// Interface was already deleted
return;
),
error => {
if (error.code === "InvalidParameterValue") {
// Network interface is currently in use
// Skip on this error, as it may happen
// few times for given interface
return;
}
if (error.code === "InvalidNetworkInterfaceID.NotFound") {
// Interface was already deleted
return;
}
this.handleError(error);
}
this.handleError(error);
}
),
),
this.handleError.bind(this)
);
}
Expand All @@ -92,9 +96,6 @@ Object.defineProperties(
cleanupInterval: d(5000) // Attempt ENI cleanup every 5 seconds
},
lazy({
ec2: d(function () {
return new Ec2({ region: this.serverless.service.provider.region });
}),
functionNames: d(function () {
return Object.keys(this.serverless.service.functions).map(
functionName => this.serverless.service.functions[functionName].name
Expand Down
26 changes: 17 additions & 9 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
"use strict";

const noop = require("es5-ext/function/noop")
, proxyquire = require("proxyquire")
, sinon = require("sinon")
, test = require("tape")
, Plugin = proxyquire("../", { "aws-sdk/clients/ec2": require("./__ec2-mock") });
, Ec2 = require("./__ec2-mock")
, Plugin = require("../");

test("Serverless Plugin VPC ENI Cleanup", t => {
const serverlessMock = {
const serverlessMock = ec2 => ({
cli: { log: noop },
service: { functions: { foo: { name: "foo" } }, provider: { region: "eu-west-1" } }
};
service: { functions: { foo: { name: "foo" } }, provider: { region: "eu-west-1" } },
getProvider() {
return {
request(service, method, params) {
return ec2[method](params).promise();
}
};
}
});

t.test("Success run", t => {
const plugin = new Plugin(serverlessMock);
const ec2 = new Ec2();
const plugin = new Plugin(serverlessMock(ec2));
plugin.cleanupInterval = 0;
plugin.handleError = error => { throw error; };
const { ec2 } = plugin;

sinon.spy(ec2, "describeNetworkInterfaces");
sinon.spy(ec2, "detachNetworkInterface");
Expand All @@ -40,9 +47,10 @@ test("Serverless Plugin VPC ENI Cleanup", t => {
});

t.test("Error run", t => {
const plugin = new Plugin(serverlessMock);
const ec2 = new Ec2();
const plugin = new Plugin(serverlessMock(ec2));
plugin.cleanupInterval = 0;
plugin.ec2.errors = [new Error("Unknown error")];
ec2.errors = [new Error("Unknown error")];

sinon.spy(plugin, "handleError");

Expand Down