← Back

Set Up Robots.txt Categories for Your AWS Website

Overview

Use a simple Lambda edge function to append Robots.txt Categories to your existing robots.txt file. If you don't have the capability or need a custom solution, you can alternatively use the REST API to generate and append the robot.txt rules periodically using some other method like a cron job. Please contact us if you need help getting set up.

Step 1: Create a Lambda Edge Function

const DARK_VISITORS_ACCESS_TOKEN = "YOUR_ACCESS_TOKEN" // TODO: Swap in your access token
const ROBOTS_TXT_DISALLOW_PATH = "/"
const ROBOTS_TXT_AGENT_TYPES = [
    // TODO: Add blocked agent types
]

export const handler = async (event) => {
    const thisResponse = event.Records[0].cf.response
    const thatResponse = await fetchRobotsTXT()
    const thisRobotsTXT = thisResponse.body || ""
    const thatRobotsTXT = thatResponse.ok ? await thatResponse.text() : ""

    const robotsTXT = [
        thisRobotsTXT.trim(),
        "# BEGIN Dark Visitors Managed Content",
        thatRobotsTXT.trim(),
        "# END Dark Visitors Managed Content"
    ].join("\n\n")

    return {
        ...response,
        status: "200",
        statusDescription: "OK",
        headers: {
            ...response.headers,
            "content-type": [{ value: "text/plain" }]
        },
        body: robotsTXT
    }
}

async function fetchRobotsTXT() {
    return fetch("https://api.darkvisitors.com/robots-txts", {
        method: "POST",
        headers: {
            "Authorization": `Bearer ${DARK_VISITORS_ACCESS_TOKEN}`,
            "Content-Type": "application/json"
        },
        body: JSON.stringify({
            agent_types: ROBOTS_TXT_AGENT_TYPES,
            disallow: ROBOTS_TXT_DISALLOW_PATH
        })
    })
}

Step 2: Create a Distribution Behavior

Step 3: Test Your Integration

If your website is correctly connected, you should see the new rules in your website's robots.txt.