Skip to content

Cloud Formation Lambda Extractor (cfn_lambda_extractor) extracts inline Python code from Lambda resources in a Cloudformation template, allowing the code to be tested.

License

bw-intuit/cfn_lambda_extractor

 
 

Repository files navigation

Cloud Formation Lambda Extractor

AWS Cloudformation extracts inline Lambda function code from a Cloudformation template.

For example, see the below TestFunction1 Lambda function:

TestFunction1:
  Type: AWS::Lambda::Function
  Properties:
    Code:
      ZipFile: !Sub
        - |
          def handler(event, context):
              val = ${ValueToSub1}
              print("This is a test with value '{}'.".format(val))
        - ValueToSub1: "test1234"
    Handler: index.handler
    Role: !GetAtt MyRole.Arn
    Runtime: python3.6

Inline code is great for incorporating code directly within your Cloudformation templates, however the code is difficult to test.

To support extracting and testing code included in your Cloudformation template, cfn_lambda_extractor will parse your AWS Cloudformation templates, remove inline code, replace variables populated by ${variable} styble sub statements, and write the modified template to a directory for testing.

See example directory for a full working example of extracting, replacing variables and finally testing inline Lambda code.

Installation

To install directly from GitHub.

pip3 install git+https://github.com/intuit/cfn_lambda_extractor

Usage

To extract all the functions in template.yaml into /tmp:

$ cfn_lambda_extractor -c template.yaml -o /tmp
INFO:root:Loading input from file 'template.yaml'.
INFO:root:Loaded 2 function(s).
INFO:root:Writing function '0' to '/tmp/tmp_test_lambda_function0.py'.
INFO:root:Writing function '1' to '/tmp/tmp_test_lambda_function1.py'.
INFO:root:Completed processing cfn template.

This will result in a file being created for each function.

For example:

$ ls /tmp/*.py
tmp_test_lambda_function0.py
tmp_test_lambda_function1.py

Variable substituation for Cloudformation substituation of variables in the form of ${VARIABLE} is supported. You can include a list of variables to replace as a command line argument.

$ cfn_lambda_extractor -c template.yaml -o /tmp -s Key1=test1234,Key2=test4321
INFO:root:Loading input from file 'cfn_lambda_extractor/testdata/test_template.yaml'.
INFO:root:Loaded 2 function(s).
INFO:root:Replacing cfn value 'Key1' with 'test1234'.
INFO:root:Replacing cfn value 'Key2' with 'test4321'.
INFO:root:Writing function '0' to '/tmp/tmp_test_lambda_function0.py'.
INFO:root:Writing function '1' to '/tmp/tmp_test_lambda_function1.py'.
INFO:root:Completed processing cfn template.

Development

Test

make test

Constraints and Known Limitations

Known Issues and Limitations

  • Only supports YAML.
  • Only supports Python functions.
  • Only supports replacement of ${VAR} style cfn substituation (not Fn::Sub style or other variables).
  • No other psuedo parameters or replacements.

Copyright (c) 2019 Intuit

About

Cloud Formation Lambda Extractor (cfn_lambda_extractor) extracts inline Python code from Lambda resources in a Cloudformation template, allowing the code to be tested.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 97.1%
  • Shell 2.0%
  • Makefile 0.9%