Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python template hello world function does not work #5914

Closed
chazarabriseis opened this issue Nov 19, 2020 · 2 comments · Fixed by #6477
Closed

python template hello world function does not work #5914

chazarabriseis opened this issue Nov 19, 2020 · 2 comments · Fixed by #6477
Assignees
Labels
bug Something isn't working functions Issues tied to the functions category

Comments

@chazarabriseis
Copy link

Describe the bug
Using the amplify client adding a path with a python lambda function to a REST API, the hello world template does not work out of the box.

To Reproduce
Using the amplify cli:

  1. amplify update api
    ? Please select from one of the below mentioned services: REST
    ? Please select the REST API you would want to update ReSoBuAPI
    ? What would you like to do Add another path
    ? Provide a path (e.g., /book/{isbn}): /test
    ? Choose a Lambda source Create a new Lambda function
    ? Provide a friendly name for your resource to be used as a label for this category in the project: testalambda
    ? Provide the AWS Lambda function name: testalambda
    ? Choose the runtime that you want to use: Python
    Only one template found - using Hello World by default.
    ? Do you want to access other resources in this project from your Lambda function? No
    ? Do you want to invoke this function on a recurring schedule? No
    ? Do you want to configure Lambda layers for this function? No
    ? Do you want to edit the local lambda function now? No
    Successfully added resource testalambda locally.
    Next steps:
    Check out sample function code generated in /amplify/backend/function/testalambda/src
    "amplify function build" builds all of your functions currently in the project
    "amplify mock function " runs your function locally
    "amplify push" builds all of your local backend resources and provisions them in the cloud
    "amplify publish" builds all of your local backend and front-end resources (if you added hosting category) and provisions them in the cloud
    Succesfully added the Lambda function locally
    ? Restrict API access No
    ? Do you want to add another path? No
    Successfully updated resource

amplify push

Resulted in the following error (python version on path is python 3.8)
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
(node:43186) UnhandledPromiseRejectionWarning: Error: Could not find a pipenv site-packages directory at /Users/chazarabriseis/.local/share/virtualenvs/testalambda-XXR/lib/python3.9/site-packages
got an error

fixed this by changing the folder of python3.8 to python3.9

testing the API via my frontend resulted in:
Access to XMLHttpRequest at 'https://xxx.execute-api.eu-central-1.amazonaws.com/dev/test' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
xhr.js:172 POST https://z3c0n1vg14.execute-api.eu-central-1.amazonaws.com/dev/check-for-chats net::ERR_FAILED
createError.js:16 Uncaught (in promise) Error: Network Error
at createError (createError.js:16)
at XMLHttpRequest.handleError (xhr.js:81)

fixed this by changing the lambda function code to:

def handler(event, context):
    headers = {
        "Access-Control-Allow-Credentials": True,
        "Access-Control-Allow-Headers": "Content-Type",
        "Access-Control-Allow-Methods": "OPTIONS,POST,GET",
        "Access-Control-Allow-Origin": "*",
    }

    try:
        _event_body = event["body"]
        event_body = json.loads(_event_body)

    except KeyError:
        print('problem')
        return {"headers": headers,
                "statusCode": 500,
                "body": json.dumps({"errorType": "KeyError",
                                    "message": "One of the required function parameters not present (body)",
                                    "statusCode": 500})
                }

    response = 'python lambda says hi'

    return {"headers": headers,
            "statusCode": 200,
            "body": json.dumps({"statusCode": 200, "response": response})
            }

Expected behaviour
When using out of the box templates I expect things to work so I can then go straight from there and edit the working template to my usecase.

Desktop (please complete the following information):

  • OS: macOS high sierra
  • Browser chrome
  • Version [e.g. 22]

Additional context
It would be great if you could change the hello world python template so it works out of the box by returning a dictionary with a status Code, headers and a body containing a json string. Or documentation that walks newbies through these steps.

@kahdojay kahdojay transferred this issue from aws-amplify/amplify-hosting Nov 19, 2020
@kahdojay
Copy link

Transferring the issue to the amplify-cli repo

@github-actions
Copy link

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working functions Issues tied to the functions category
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants