1

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.

10
  • 2
    you can create a zip file dynamically by saving it to /tmp since you do have write permissions there. Commented Mar 18, 2019 at 14:31
  • 1
    @ThalesMinussi Thanks a lot, I think it will work! But the focus of the question was really on the "inline" creation of code, it meas without need to create a zip file. I edited the question a bit to make it clear. Commented Mar 18, 2019 at 15:05
  • 1
    I understand. Ye, that's a very good question tbh! Let's see if someone gets back to you :) Commented Mar 18, 2019 at 15:06
  • 1
    Take a look at the CDK docs.aws.amazon.com/CDK/latest/userguide/what-is.html Commented Mar 18, 2019 at 16:05
  • 1
    What's the use case? Why not simply use something like Terraform? Having a Lambda create another Lambda seems like bad practice to me...you run the risk of an error triggering a retry and you ending up with hundreds of Lambdas Commented Mar 18, 2019 at 18:47

2 Answers 2

2

I think aws-cdk is a pretty good option. It generates cloudformation from javascript or typescript and keeps the lines of coding down to a minimum.

In your master lambda project

npm i @aws-cdk/aws-lambda --save-exact

You will then need to create a directory in /tmp and run cdk init from a node shell using node_cmd

Then you'd have your lambda export the cdk Lambda template something like below to /tmp/output.js (transforming the inline part which I'm assuming is something you want)

import lambda = require('@aws-cdk/aws-lambda');    
const fn = new lambda.Function(this, 'MyFunction', {
    runtime: lambda.Runtime.NodeJS810,
    handler: 'index.handler',
    code: lambda.Code.inline('exports.handler = function(event, ctx, cb) { return cb(null, "hello ttulka"); }')
});

You will then need to run cdk --app 'node /tmp/output.js' synth from a node shell using node_cmd

Sign up to request clarification or add additional context in comments.

Comments

0

TLDR Code is uploaded to Lambda as a Deployment Package--which is a zip file. So you can write the new function code dynamically, but you'd still need to create a Deployment Package zip dynamically as well before passing it to lambda.createFunction.

Additional Info: From the Lambda API Docs, the Code element must be a FunctionCode object. The options with a FunctionCode object are to either specify a local Deployment Package zip file, or specify the location on s3 for your Deployment Package zip file.

FunctionCode Object Reference: https://docs.aws.amazon.com/lambda/latest/dg/API_FunctionCode.html

syntax:

"Code": { 
      "S3Bucket": "string",
      "S3Key": "string",
      "S3ObjectVersion": "string",
      "ZipFile": blob
   }

Source: https://docs.aws.amazon.com/lambda/latest/dg/API_CreateFunction.html

Some Lambda Deployment Package limits to keep in mind (values at time of writing):

  1. If you're uploading a zip file directly as a blob, it needs to be less than 50 MB.
  2. If larger than 50 MB, you'll need to upload to s3 first, and specify the location.
  3. The overall unzipped size still needs to be less than 250 MB.

Lambda Limits Reference: https://docs.aws.amazon.com/lambda/latest/dg/limits.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.