You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从指定端点拉取JSON文件存储至AWS S3的最优方案咨询

Is AWS Lambda + API Gateway a Viable Solution?

Absolutely—this is actually a perfect use case for serverless architecture. Lambda lets you run code on demand without managing servers, which aligns perfectly with your weekly update schedule, and API Gateway can handle both scheduled triggers (via EventBridge) and event-driven webhooks if your mock endpoint ever supports push notifications.

Step 1: Pull JSON from Mock Endpoint & Upload to S3

Here’s a simplified Python Lambda function that handles this workflow:

import requests
import boto3

def lambda_handler(event, context):
    # Pull JSON from mock endpoint
    mock_endpoint = "https://your-mockable-endpoint.com/data"
    try:
        response = requests.get(mock_endpoint)
        response.raise_for_status()  # Raise error for HTTP 4xx/5xx
        json_data = response.json()
    except Exception as e:
        print(f"Failed to fetch JSON: {str(e)}")
        raise e  # Let Lambda handle retries or trigger alerts

    # Upload to S3
    s3 = boto3.client('s3')
    bucket_name = "your-s3-bucket-name"
    s3_key = f"weekly-updates/{context.request_id}.json"  # Unique key per run
    try:
        s3.put_object(
            Bucket=bucket_name,
            Key=s3_key,
            Body=response.text,  # Use raw text to preserve formatting
            ContentType="application/json"
        )
        print(f"Successfully uploaded to S3: s3://{bucket_name}/{s3_key}")
    except Exception as e:
        print(f"Failed to upload to S3: {str(e)}")
        raise e

    return {"statusCode": 200, "body": "JSON fetched and uploaded successfully"}

Key notes:

  • Use requests.get to fetch the mock JSON (Lambda includes requests by default in newer runtimes)
  • Generate a unique S3 key (using context.request_id or a timestamp) to avoid overwriting files
  • Add error handling to catch network issues or S3 upload failures
Step 2: Triggering the Workflow (Scheduled or Event-Driven)

You have two great options here:

Scheduled Trigger (Weekly Updates)

Use Amazon EventBridge (formerly CloudWatch Events) to run your Lambda on a fixed schedule:

  1. Go to the EventBridge console, create a new rule
  2. Choose "Schedule" as the rule type
  3. Set a cron expression for weekly runs (e.g., 0 12 ? * SUN * to run every Sunday at noon UTC)
  4. Select your Lambda function as the target

Event-Driven Trigger (Push-Based Updates)

If your mock endpoint can send a webhook when data is updated (instead of polling), use API Gateway to trigger Lambda:

  1. Create a REST API in API Gateway
  2. Add a POST method and integrate it with your Lambda function
  3. Deploy the API and share the endpoint URL with your mock service
  4. When the mock endpoint sends a POST to this URL, API Gateway triggers Lambda to pull the latest JSON
Step 3: Sending JSON to AWS SQS

Modify your Lambda function to send the JSON data (or a reference to it) to SQS after fetching:

# Add this after fetching json_data (or after S3 upload)
sqs = boto3.client('sqs')
sqs_queue_url = "https://sqs.your-region.amazonaws.com/your-account-id/your-queue-name"

try:
    # Option 1: Send raw JSON (if <256KB)
    sqs.send_message(
        QueueUrl=sqs_queue_url,
        MessageBody=response.text
    )
    # Option 2: Send S3 object reference (for large JSON >256KB)
    # sqs.send_message(
    #     QueueUrl=sqs_queue_url,
    #     MessageBody=json.dumps({"s3_key": s3_key, "bucket": bucket_name})
    # )
    print("Successfully sent message to SQS")
except Exception as e:
    print(f"Failed to send to SQS: {str(e)}")
    raise e

Important:

  • SQS has a 256KB message size limit. If your JSON is larger, send an S3 object reference instead of the raw data
  • Ensure your Lambda’s IAM role has permissions for sqs:SendMessage
Best Practices to Consider
  • IAM Permissions: Create a minimal IAM role for Lambda that only allows the actions it needs (s3:PutObject, sqs:SendMessage, logs:CreateLogGroup, logs:CreateLogStream, logs:PutLogEvents)
  • Error Handling: Set up a Dead-Letter Queue (DLQ) for Lambda and SQS to catch failed runs/messages
  • Logging: Use CloudWatch Logs to monitor Lambda execution and debug issues
  • Testing: Use Lambda’s test events to simulate fetching from your mock endpoint before deploying the trigger
  • Versioning: Enable S3 bucket versioning to keep historical copies of your weekly JSON files

内容的提问来源于stack exchange,提问作者panza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:28:21