Created
June 2, 2019 20:31
-
-
Save niradler/947b40fa80b420603dad0976d5df51d4 to your computer and use it in GitHub Desktop.
How to Free AWS Lambda Code Storage (CodeStorageExceeded)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const execa = require("execa"); | |
const fs = require("fs"); | |
const awsCliGetCmd = ({ FunctionName = "", Version = "" }) => ({ | |
"list-functions": `aws lambda list-functions`, | |
"list-versions-by-function": `aws lambda list-versions-by-function --function-name ${FunctionName}`, | |
"delete-function": `aws lambda delete-function --function-name ${FunctionName} --qualifier ${Version}` | |
}); | |
const run = async cmd => { | |
const { stdout } = await execa.shell(cmd); | |
return stdout; | |
}; | |
(async () => { | |
try { | |
let res = await run(awsCliGetCmd({})["list-functions"]); | |
const { Functions } = JSON.parse(res); | |
const FunctionNames = Functions.map(f => f.FunctionName).filter( | |
name => !name.includes("prod") | |
); | |
console.log( | |
`found ${Functions.length} functions, and ${ | |
FunctionNames.length | |
} match to clean old versions.` | |
); | |
for (let i = 0; i < FunctionNames.length; i++) { | |
const FunctionName = FunctionNames[i]; | |
console.log(`list versions for: ${FunctionName}`); | |
res = await run( | |
awsCliGetCmd({ FunctionName })["list-versions-by-function"] | |
); | |
const { Versions } = JSON.parse(res); | |
let oldOrUnuseVersions = []; | |
if (Versions) { | |
oldOrUnuseVersions = Versions.filter(f => f.Version != "$LATEST").map( | |
f => f.Version | |
); | |
} | |
console.log( | |
`found ${Versions.length} versions, and ${ | |
oldOrUnuseVersions.length | |
} match to delete.` | |
); | |
for (let j = 0; j < oldOrUnuseVersions.length; j++) { | |
const Version = oldOrUnuseVersions[j]; | |
const delRes = await run( | |
awsCliGetCmd({ FunctionName, Version })["delete-function"] | |
); | |
} | |
console.log( | |
`delete ${ | |
oldOrUnuseVersions.length | |
} versions, for function: ${FunctionName}.` | |
); | |
} | |
} catch (error) { | |
console.log({ error }); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment