•   7 months ago

Need Assistance: CORS / S3 Connection Issue with My AWS Setup

Hey everyone,
I am currently working on hosting my project web app on S3 (static site) and connecting it to my Lambda function through API Gateway.
The setup works fine locally, but once I hosted it on S3 and linked it to the API Gateway endpoint, I keep getting this error in the browser console:
"Error connecting to server"
After checking, I realized it’s a CORS issue between the S3 static site and the API Gateway.
I already:
Enabled CORS in API Gateway
Allowed POST and OPTIONS methods,
but it’s still not connecting.
Has anyone faced a similar issue or can guide me on the exact CORS setup that worked for your S3 + API Gateway + Lambda integration?

  • 2 comments

  • Manager   •   7 months ago

    Hi there,

    The AWS Team says: "
    I hope this message finds you well. If you're looking to return content from an AWS Lambda function, you can use the following method:

    ```python

    import json

    def lambda_handler(event, context):

    # Assuming rhtml is your response content

    rhtml = {"message": "Hello from Lambda!"}

    _rret = {

    "statusCode": 200,

    "isBase64Encoded": False,

    "body": json.dumps(rhtml),

    "headers": {

    "Content-Type": "text/html",

    "Access-Control-Allow-Origin": "*"

    }

    }

    return _rret"

    Good luck!

  •   •   7 months ago

    Thank you. Let me try it out.

Comments are closed.