Creating Lambdas to inspect their execution environment
Creation of three Lambdas sharing the same code
The code generated by this example is available here.
Lets create a new Myrmex project.
myrmex new lambda-profiling @myrmex/iam @myrmex/lambda
cd lambda-profiling
We create a simple execution role for our Lambdas.
myrmex create-role LambdaInspection --model LambdaBasicExecutionRole
We create a node module called inspection
. It will inspect its own execution environment.
myrmex create-node-module inspection
The inspection
module is created in lambda-profiling/lambda/modules/inspection
and only contain a package.json
file for now.
Lets create an index.js
file that exposes data from the os
native module and the process
global object in our module.
// lambda-profiling/lambda/modules/inspection/index.js
'use strict';
const os = require('os');
module.exports = () => {
return {
host: {
name: os.hostname(),
architecture: process.arch,
memory: os.totalmem(),
cpus: os.cpus()
},
os: {
platform: process.platform,
release: os.release(),
uptime: os.uptime()
},
user: {
uid: process.getuid(),
gid: process.getgid(),
groups: process.getgroups
},
process: {
command: process.argv,
execArgs: process.execArgv,
pid: process.pid,
uptime: process.uptime(),
memoryUsage: process.memoryUsage()
},
exec: {
env: process.env,
currentDirectory: process.cwd(),
// mainModule: process.mainModule,
config: process.config
}
};
};
Now we create three Lambdas that will use the inspection
module as a dependency. Their are configured with different memory values: 128MB, 512MB and 1536MB.
myrmex create-lambda config-128 --runtime nodejs6.10 --timeout 30 --memory 128 --dependencies inspection --role LambdaInspection
myrmex create-lambda config-512 --runtime nodejs6.10 --timeout 30 --memory 512 --dependencies inspection --role LambdaInspection
myrmex create-lambda config-1536 --runtime nodejs6.10 --timeout 30 --memory 1536 --dependencies inspection --role LambdaInspection
We alter the handler of the three Lambdas to call the inspection
module, log its output (it will be visible in cloudwatch) and return it.
// lambda-profiling/lambda/lambdas/config-128/index.js
// lambda-profiling/lambda/lambdas/config-512/index.js
// lambda-profiling/lambda/lambdas/config-1536/index.js
'use strict';
const inspection = require('inspection');
module.exports.handler = function(event, context, cb) {
const inspect = inspection();
console.log(JSON.stringify(inspect));
cb(null, inspect);
};
We can execute the lambdas locally to test them.
# Install dependencies of the Lambdas
myrmex install-lambdas-locally config-128 config-512 config-1536
# Execute Lambdas locally
myrmex test-lambda-locally config-128
myrmex test-lambda-locally config-512
myrmex test-lambda-locally config-1536
Testing the Lambdas in AWS
Lets deploy in AWS!
myrmex deploy-lambdas config-128 config-1536 config-512 --region us-east-1 --environment DEV --alias v0
Now we can execute the Lambdas in AWS using Myrmex, but you can also use the AWS console if you prefer.
myrmex test-lambda config-128 --region us-east-1 --environment DEV --alias v0
myrmex test-lambda config-512 --region us-east-1 --environment DEV --alias v0
myrmex test-lambda config-1536 --region us-east-1 --environment DEV --alias v0
Check the result of the functions to have insights about the execution environment of Lambdas. Observe the variations when calling several times the same function.
TODO: explain results and add a module to profile the use of require() with/without warm up of a Lambda.