DEV Community

kyooryoo
kyooryoo

Posted on

My first Lambda function in AWS Bedrock Agent

Amazon Bedrock > Agents > Agent Builder > Action Group > Action Group Invocation > Select Lambda function > View

import json
from datetime import datetime
from zoneinfo import ZoneInfo

def lambda_handler(event, context):
    now_cst = datetime.now(ZoneInfo("Asia/Shanghai")).strftime("%Y-%m-%d %H:%M:%S %Z %z")

    return {
        "messageVersion": "1.0",
        "response": {
            "actionGroup": event.get("actionGroup"),
            "function": event.get("function"),
            "apiPath": event.get("apiPath"),
            "httpMethod": event.get("httpMethod"),
            "httpStatusCode": 200,
            "responseBody": {
                "application/json": {
                    "body": json.dumps({
                        "current_time_cst": now_cst,
                        "note": "Time is returned in China Standard Time (CST)"
                    })
                }
            }
        }
    }
Enter fullscreen mode Exit fullscreen mode

Deploy > Test > Create Test if needed:

Status: Succeeded
Test Event Name: tester

Response:
{
  "messageVersion": "1.0",
  "response": {
    "actionGroup": null,
    "function": null,
    "apiPath": null,
    "httpMethod": null,
    "httpStatusCode": 200,
    "responseBody": {
      "application/json": {
        "body": "{\"current_time_cst\": \"2026-04-17 10:19:59 CST +0800\", \"note\": \"Time is returned in China Standard Time (CST)\"}"
      }
    }
  }
}

The area below shows the last 4 KB of the execution log.

Function Logs:
START RequestId: 0f8c3021-9df9-40ec-9ee5-e90e2795cdb5 Version: $LATEST
END RequestId: 0f8c3021-9df9-40ec-9ee5-e90e2795cdb5
REPORT RequestId: 0f8c3021-9df9-40ec-9ee5-e90e2795cdb5  Duration: 28.70 ms  Billed Duration: 29 ms  Memory Size: 128 MB Max Memory Used: 38 MB

Request ID: 0f8c3021-9df9-40ec-9ee5-e90e2795cdb5
Enter fullscreen mode Exit fullscreen mode

Action Group Type: Define with API schemas > Action Group Schema: Define via in-line schema editor:

{
    "openapi": "3.0.0",
    "info": {
        "title": "Current CST Time API",
        "version": "1.0.0",
        "description": "API for getting the current China Standard Time."
    },
    "paths": {
        "/get-time": {
            "post": {
                "summary": "Return current CST time",
                "description": "Return the current China Standard Time.",
                "operationId": "get_current_time",
                "requestBody": {
                    "required": false,
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "properties": {}
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "Successful response containing the current CST time",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object",
                                    "properties": {
                                        "current_time": {
                                            "type": "string",
                                            "description": "The current time in CST."
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Save and Exit > Save > Prepare > Enter your message: tell me the time

The current time is 2026-04-17 10:20:41 CST (China Standard Time).
Enter fullscreen mode Exit fullscreen mode

Top comments (0)