top of page

Create a Serverless JSON Page Using Lambda

AWS Lambda is a serverless compute service, and it doesn't directly serve JSON pages like a traditional web server. However, you can create an AWS Lambda function to handle HTTP requests using an API Gateway, and the Lambda function can generate JSON content as a response.


Below is a simplified example of how you can create a basic AWS Lambda function that generates JSON content in response to an HTTP request:


1. Create Lambda Function:

  • Go to the AWS Lambda console.

  • Click on "Create function" and choose "Author from scratch."

  • Give your function a name, choose a runtime (e.g., python 3.7), and create the function.

Lambda

2. Write Lambda Function Code:


In the Lambda function code editor, you can write Python code to generate JSON. Here's a basic example:

import json

def lambda_handler(event, context):
    # TODO implement
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from BuildDevOps!')
    }

This example returns a simple JSON page with the message "Hello from BuildDevOps!"

Lambda

3. Configure API Gateway:

  • Go to the API Gateway console.

  • Create a new API.

  • Set a Security to Open (Not Recommended In Prod Environment)

  • Set the integration type to Lambda Function and choose your Lambda function.

Lambda

4. Access JSON API via URL:

Once deployed, you will get an Invoke URL. Accessing this URL will trigger your Lambda function, and the HTML content will be returned.

Lambda

When access from browser:

Lambda

bottom of page