# Apollo 4, CORS, and aws-lambda handler

**URL:** <https://community.apollographql.com/t/apollo-4-cors-and-aws-lambda-handler/6832>\
**Category:** Client SDKs\
**Tags:** server, web, tooling\
**Created:** [October 10, 2023, 11:34pm UTC](https://community.apollographql.com/t/apollo-4-cors-and-aws-lambda-handler/6832 "2023-10-10T23:34:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Lowery](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/jeff_lowery/32/4660_2.png) [@Jeff\_Lowery](https://community.apollographql.com/u/Jeff_Lowery)\
**Post date:** [October 10, 2023, 11:34pm UTC](https://community.apollographql.com/t/apollo-4-cors-and-aws-lambda-handler/6832/1 "2023-10-10T23:34:34Z")

</div>

I’m trying to set up a lambda function server separate from the web client, which means having to deal with CORS. Scenario:

1. server running on localhost:8881
2. client running on localhost:8888

Relevant server code:

```auto
import { ApolloServer } from "@apollo/server";
import {
    startServerAndCreateLambdaHandler,
    handlers,
} from "@as-integrations/aws-lambda";

const server = new ApolloServer({
    typeDefs,
    resolvers,
    introspection: !production,
    playground: !production,
    debug: !production,
});

const graphqlHandler = startServerAndCreateLambdaHandler(
    server,
    requestHandler,
    {
        middleware: [corsMiddleware],
    }
);

```

The corsMiddleware is set up according to a solution provided [here](https://github.com/apollo-server-integrations/apollo-server-integration-aws-lambda/issues/85), but I have not had success in getting it to work.

```auto
const allowedOrigins = ["http://localhost:8888"];
// const requestHandler = handlers.createAPIGatewayProxyEventV2RequestHandler();
const requestHandler = handlers.createAPIGatewayProxyEventRequestHandler();

const corsMiddleware = async (event) => {
    const origin = event.headers.origin;

    console.dir(event, { depth: 5 });

    if (origin && allowedOrigins.includes(origin)) {
        return (result) => {
            result.headers = {
                ...result.headers,
                "Access-Control-Allow-Origin": origin,
                Vary: "Origin",
            };

            console.dir(result, { depth: 5 });
            return Promise.resolve();
        };
    }

    return () => Promise.resolve();
};

```

CORS support is pretty important if you want to set up a GraphQL server, and I’m surprised the Apollo Docs don’t address it with a working example.

---

<div class="post-metadata">

**Author:** ![Justin\_Gonzalez](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/justin_gonzalez/32/5359_2.png) [@Justin\_Gonzalez](https://community.apollographql.com/u/Justin_Gonzalez)\
**Post date:** [February 6, 2025, 8:48pm UTC](https://community.apollographql.com/t/apollo-4-cors-and-aws-lambda-handler/6832/2 "2025-02-06T20:48:12Z")

</div>


