- Installed and configured
AWS CLIandÌBM CLI. - Node.js version >= 10.2.0
npm i m2faas -gEnclose your code block with annotations:
//cfun ...
<code_block>
//cfunendm2faas example/
cd out && npm install
node your_code.js
//cfun require(./foo.js as foo,lodash as _)
const fooBefore = foo.fun(12);
const value = _.chunk(['a', 'b', 'c', 'd'], 2);
// cfunend//cfun install(lodash)
const value = _.chunk(['a', 'b', 'c', 'd'], 2);
// cfunendlet value1 = 5;
//cfun assign(value1,value2)
value1 = 12;
const value2 = 34;
// cfunend
const sum = value1 + value2;let value1 = 2;
//cfun vars(value1)
value1 = 12;
// cfunenddeploy allows to specify an ordered list of deployments. All of the specified configurations will be deployed.
This annotation also specifies the fault tolerance of the execution. If the cloud function call to the first function fails, the next cloud function in the list will be invoked.
The deployment configuration is represented as a json-array:
[
{
"name": "m2FaaSExampleAWS",
"provider": "aws",
"region": "us-east-1",
"memorySize": 128,
"runtime": "nodejs14.x",
"timeout": 3,
"role": "arn:aws:iam::xxxxxxxxxxx:role/service-role/xxxxxxx"
},
{
"name": "m2FaaSExampleIBM",
"provider": "ibm",
"region": "eu-gb",
"memorySize": 128,
"runtime": "nodejs:12",
"timeout": 60
}
]region, memorySize, runtime, timeout represents optional values. If no specified, default values will be used.
//cfun deploy([{"name": "m2FaaSExampleAWS", "provider": "aws", "region": "us-east-1", "memorySize": 128, "runtime": "nodejs14.x", "timeout": 3, "role": "arn:aws:iam::170392512081:role/service-role/getFlight-role-n1g2o34s"},{"name": "m2FaaSExampleIBM", "provider": "ibm", "region": "eu-gb", "memorySize": 128, "runtime": "nodejs:12", "timeout": 60 }])
...
// cfunendThe example monolithic project consists of three files:
- package.json contains the dependencies of the monolithic application.
- foo.js contains a a simple nodeJs function.
- example.js is the starting point of the application:
const foo = require('./foo')
const _ = require('lodash')
async function main(args) {
let a = 2;
// cfun require(./foo.js as foo,lodash as _) assign(value,a,foo1) vars(a) install(lodash) deploy([{"name": "m2FaaSExampleAWS", "provider": "aws", "region": "us-east-1", "memorySize": 128, "runtime": "nodejs14.x", "timeout": 3, "role": "arn:aws:iam::170392512081:role/service-role/getFlight-role-n1g2o34s"},{"name": "m2FaaSExampleIBM", "provider": "ibm", "region": "eu-gb", "memorySize": 128, "runtime": "nodejs:12", "timeout": 60 }])
await new Promise(resolve => setTimeout(resolve, 5000));
const foo1 = foo.fun(a);
a = 43;
const value = _.chunk(['a', 'b', 'c', 'd'], 2);
// cfunend
return { a: a, value: value, foo1: foo1, foo2: foo.fun(a) }
}
main().then(response => { console.log(response) });The annotated code has the following specialities:
- The code block, which should be FaaSified, has two external dependencies: ./foo.js represents a local code dependency, while lodash represents an external package dependency.
- After the FaaSification the variables value, a and foo1 need to be available.
- The variable a is not declared in the code block and therefore an input to the cloud function.
The tool generates a foo.js file to solve the local dependency and a package.json for the external dependency.
The generated lambda function:
const foo = require("./foo.js")
const _ = require("lodash")
exports.handler = async (event) => {
let a = event.a
// cfun require(./foo.js as foo,lodash as _) assign(value,a,foo1) vars(a) install(lodash) deploy([{"name": "m2FaaSExampleAWS", "provider": "aws", "region": "us-east-1", "memorySize": 128, "runtime": "nodejs14.x", "timeout": 3, "role": "arn:aws:iam::170392512081:role/service-role/getFlight-role-n1g2o34s"},{"name": "m2FaaSExampleIBM", "provider": "ibm", "region": "eu-gb", "memorySize": 128, "runtime": "nodejs:12", "timeout": 60 }])
await new Promise((resolve) => setTimeout(resolve, 5000))
const foo1 = foo.fun(a)
a = 43
const value = _.chunk(["a", "b", "c", "d"], 2)
return (response = {
statusCode: 200,
body: { value: value, a: a, foo1: foo1 },
})
}Starting point of the IBM function:
const foo = require("./foo.js")
const _ = require("lodash")
async function main(event) {
let a = event.a
// cfun require(./foo.js as foo,lodash as _) assign(value,a,foo1) vars(a) install(lodash) deploy([{"name": "m2FaaSExampleAWS", "provider": "aws", "region": "us-east-1", "memorySize": 128, "runtime": "nodejs14.x", "timeout": 3, "role": "arn:aws:iam::170392512081:role/service-role/getFlight-role-n1g2o34s"},{"name": "m2FaaSExampleIBM", "provider": "ibm", "region": "eu-gb", "memorySize": 128, "runtime": "nodejs:12", "timeout": 60 }])
await new Promise((resolve) => setTimeout(resolve, 5000))
const foo1 = foo.fun(a)
a = 43
const value = _.chunk(["a", "b", "c", "d"], 2)
return { value: value, a: a, foo1: foo1 }
}
exports.main = mainThe code block is replaced by a cloud function call:
const foo = require('./foo')
const _ = require('lodash')
async function main(args) {
let a = 2;
// cfun require(./foo.js as foo,lodash as _) assign(value,a,foo1) vars(a) install(lodash) deploy([{"name": "m2FaaSExampleAWS", "provider": "aws", "region": "us-east-1", "memorySize": 128, "runtime": "nodejs14.x", "timeout": 3, "role": "arn:aws:iam::170392512081:role/service-role/getFlight-role-n1g2o34s"},{"name": "m2FaaSExampleIBM", "provider": "ibm", "region": "eu-gb", "memorySize": 128, "runtime": "nodejs:12", "timeout": 60 }])
/* await new Promise(resolve => setTimeout(resolve, 5000));
const foo1 = foo.fun(a);
a = 43;
const value = _.chunk(['a', 'b', 'c', 'd'], 2); */
// cfunend
try {
m2FaaSExampleIBMResult = await require('./m2faaSInvoker').invoke({ a: a, }, [{"name":"m2FaaSExampleAWS","provider":"aws","region":"us-east-1"},{"name":"m2FaaSExampleIBM","provider":"ibm","region":"eu-gb"}]);
} catch (e) {
m2FaaSExampleIBMResult = await async function() { // cfun require(./foo.js as foo,lodash as _) assign(value,a,foo1) vars(a) install(lodash) deploy([{"name": "m2FaaSExampleAWS", "provider": "aws", "region": "us-east-1", "memorySize": 128, "runtime": "nodejs14.x", "timeout": 3, "role": "arn:aws:iam::170392512081:role/service-role/getFlight-role-n1g2o34s"},{"name": "m2FaaSExampleIBM", "provider": "ibm", "region": "eu-gb", "memorySize": 128, "runtime": "nodejs:12", "timeout": 60 }])
await new Promise(resolve => setTimeout(resolve, 5000));
const foo1 = foo.fun(a);
a = 43;
const value = _.chunk(['a', 'b', 'c', 'd'], 2);
return { value: value, a: a, foo1: foo1, }
}();
}
value = m2FaaSExampleIBMResult.value
a = m2FaaSExampleIBMResult.a
foo1 = m2FaaSExampleIBMResult.foo1
return { a: a, value: value, foo1: foo1, foo2: foo.fun(a) }
}
main().then(response => { console.log(response) });