I need to create a simple Lambda programmatically from another Lambda.
This is possible with CloudFormation:
MyLambda:
Type: AWS::Lambda::Function
Properties:
FunctionName: my-lambda
Handler: index.handler
Runtime: nodejs8.10
Role: !GetAtt MyRole.Arn
Code:
ZipFile: >
exports.handler = event => console.log('EVENT', event)
I want to create a Lambda in the same manner programmatically.
When I pack the Lambda code into a zip file and upload the zip with the Lambda code, everything works fine:
const lambda = new Lambda({apiVersion: '2015-03-31'});
...
await lambda.createFunction({
FunctionName: 'my-lambda',
Handler: 'index.handler',
Runtime: 'nodejs8.10',
Role: role.Role.Arn,
Code: {
ZipFile: fs.readFileSync('my-lambda.zip')
}
}).promise();
But it is a lot of boilerplate code to write Lambda code into a file and zip it afterwards.
If I try to set the Lambda code inline:
...
Code: {
ZipFile: "exports.handler = event => console.log('EVENT', event)"
}
I get an expected error:
Failed: Could not unzip uploaded file. Please check your file, then try to upload again.
Is there a way how to create an inline Lambda function from another Lambda dynamically, similar to the CloudFormation "hack" mentioned on the top?
EDIT: Focus of the question on dynamical creation of code without need to zip it first.