Amplify-cli: Ability to select runtime when creating function

Created on 22 May 2019  路  2Comments  路  Source: aws-amplify/amplify-cli

Would be nice if we could select the runtime when creating a lambda function. Specifically it would it would be nice if the hello world function was created with each of the supported languages. Ie Node, Python, Ruby, Java, Go, .NET depending on the selection (as well as updating the cloud-formation template.

This would allow for functions to be written in any of the AWS supported languages, which is useful for integrating with pre-existing code that may not exist in Node.

feature-request functions pending-review

Most helpful comment

I have long wanted this as well. Instead of trying to use a completely different app/codebase with the http directive as suggested in #1074 - I found it's actually pretty easy to modify a lambda created from amplify add function to use the python runtime and auto-build packages as part of the lambda zip that amplify uploads to AWS. For others, I figured I'd document the steps here.

1) use amplify add function to create your lambda
2) change the cloudformation template that amplify created for your lambda to use the Runtime you desire, e.g. python3.7
3) change the same cloudformation template Handler to be your entry point. e.g., handler.resolve AND create that lambda. e.g. handler.py within the src of your newly created lambda folder:

def resolve(event, context):
    return "Hello from Python"

4) In your package.json (from your newly created lambda folder) add a postinstall script to install your deps (change depending on your python setup). e.g.

"scripts": {
    "postinstall": "npm run install-py-deps",
    "install-py-deps": "pip install --target ./.requirements -r requirements.txt"
  },

5) modify your handler to include the local path for your custom resources dir. e.g.

import os
import sys

srcpath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, srcpath+"/.requirements")

6) profit. - from now on when you run amplify push your python lambda just works.

Obviously there are multiple approaches to the above, but I found this easiest so far without any additional setup needed. It seems it should be a rather simple Amplify CLI code change to support python as well, however, dealing with the multitude of python virtual environment setups, and as well as the compatibility issues that may come up, it might be easier to use SAM or code from SAM to enable python lambdas within the amplify cli. ref: https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html

Hope this helps any out there trying to use python lambdas with amplify!

All 2 comments

I have long wanted this as well. Instead of trying to use a completely different app/codebase with the http directive as suggested in #1074 - I found it's actually pretty easy to modify a lambda created from amplify add function to use the python runtime and auto-build packages as part of the lambda zip that amplify uploads to AWS. For others, I figured I'd document the steps here.

1) use amplify add function to create your lambda
2) change the cloudformation template that amplify created for your lambda to use the Runtime you desire, e.g. python3.7
3) change the same cloudformation template Handler to be your entry point. e.g., handler.resolve AND create that lambda. e.g. handler.py within the src of your newly created lambda folder:

def resolve(event, context):
    return "Hello from Python"

4) In your package.json (from your newly created lambda folder) add a postinstall script to install your deps (change depending on your python setup). e.g.

"scripts": {
    "postinstall": "npm run install-py-deps",
    "install-py-deps": "pip install --target ./.requirements -r requirements.txt"
  },

5) modify your handler to include the local path for your custom resources dir. e.g.

import os
import sys

srcpath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, srcpath+"/.requirements")

6) profit. - from now on when you run amplify push your python lambda just works.

Obviously there are multiple approaches to the above, but I found this easiest so far without any additional setup needed. It seems it should be a rather simple Amplify CLI code change to support python as well, however, dealing with the multitude of python virtual environment setups, and as well as the compatibility issues that may come up, it might be easier to use SAM or code from SAM to enable python lambdas within the amplify cli. ref: https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html

Hope this helps any out there trying to use python lambdas with amplify!

Duplicate of #156

Was this page helpful?
0 / 5 - 0 ratings