Blog

AWS Lambda inlined

10 Nov, 2018
Xebia Background Header Wave

The easiest way to package and deploy a Lambda is inlined with CloudFormation. Inlined means that the source code is part
of the CloudFormation template. Inlining code has a lot of advantages but also some disadvantages. This article discusses the pros and cons
of inlining Lambda code.

Advantages

Inlining lambda code has the following advantages:

  • Simple packaging strategy,
  • Simple deployment strategy,
  • Single packaging and deployment artifact,
  • Code is part of the infrastructure (CloudFormation) definition,
  • Code is part of the infrastructure commit,
  • Lambda code versioning comes for free,
  • Inlined code has the AWS SDK available
    There are a lot of advantages for inlining Lambda code, but there are also some disadvantages.

Disadvantages

Inlining lambda code has the following disadvantages:

  • No custom libraries,
  • Only available for Python or NodeJS,
  • Max 4000 characters per inlined Lambda
    The biggest disadvantage of an inlined lambda is that is does not support custom libraries. This means that third party
    libraries cannot be packaged or called, except the AWS SDK.

Replace spaces with tabs

When inlining Lambda code, there is a 4000 character limit. A space also counts as a character. When using Python as
a programming language, it helps to replace spaces with tabs for indentation. Because a character is a valuable resource
when inlining a Lambda, this saves characters for code.

An example inlined Lambda

An inlined lambda is part of the CloudFormation template. When inlined, the Handler property must be set to the
value: index.handler. The inlined code is part of the Code.ZipFile property as shown below.

NodeJS:

  NodeFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: NodeFunction
      content: Node Function Handler
      Handler: index.handler
      Runtime: nodejs6.10
      Role: !GetAtt LambdaBasicExecutionRole.Arn
      Code:
        ZipFile: |
          exports.handler = function(event, context, callback) {
          console.log(event);
              const response = {
                  statusCode: 200,
                  body: JSON.stringify('Hello Node')
              };
              callback(null, response);
          };

Python:

  PythonFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: PythonFunction
      content: Python Function Handler
      Handler: index.handler
      Runtime: python3.6
      Role: !GetAtt LambdaBasicExecutionRole.Arn
      Code:
        ZipFile: |
          import json
          def handler(event, context):
            response = {
                'statusCode': 200,
                'body': json.dumps('Hello Python')
            }
            return response

aws-cfn-update

The utility aws-cfn-update by binx.io makes it easy to
inline lambda code into a CloudFormation template. The example project show that it is possible to have the lambda source
code in a separate folder and file. The separation makes developing lambda easy and allows for a dev-test cycle.

Example

The example repository shows how to create and build and deploy
an inlined Lambda. It also shows how to separate lambda code from the CloudFormation template and how to merge
the code with the CloudFormation template. The example shows a Python and a NodeJS example lambda that are handlers
for API Gateway.

Using the example

I assume you have a mac with python, pipenv, sceptre installed. To run the example type make deploy.
To get the url of the api gateway, type make info. To remove the example type make delete.

Conclusion

CloudFormation supports inlined Lambda code. The advantage of inlining lambda code are simple packaging and deployment strategy,
a single packaging and deployment artifact, code is infrastructure-as-code, code is part of the infrastructure commit, easy
lambda versioning and the availability of the AWS SDK. The disdvantages of linining lambda code are no custom library support,
4000 character limit and only available for Python and NodeJS.

Questions?

Get in touch with us to learn more about the subject and related solutions

Explore related posts