https://pulumi.com logo
#typescript
Title
# typescript
e

echoing-zebra-28421

06/04/2021, 3:46 PM
for example:
Copy code
const api = new awsx.apigateway.API(`airship-api-${NODE_ENV}`, {
  routes: [
    {
      path: "/",
      method: "GET",
      eventHandler: async (event: APIGatewayProxyEvent) => ({
        statusCode: HttpStatus.OK,
        body: "Hello, API Gateway V4!",
      }),
    },
    {
      path: "/api/named_users/associate",
      method: "POST",
      eventHandler: async (event: APIGatewayProxyEvent): Promise<any> => ({
        statusCode: HttpStatus.OK,
        body: JSON.stringify(await handleSetNamedUser(event)),
        isBase64Encoded: false,
      }),
    },
    {
      path: "/api/channels/tags",
      method: "POST",
      eventHandler: async (event: APIGatewayProxyEvent) => ({
        statusCode: HttpStatus.OK,
        body: JSON.stringify(await handleSetTags(event)),
        isBase64Encoded: false,
      }),
    },
  ],
});
I want to make something if
c

curved-pharmacist-41509

06/05/2021, 1:12 AM
There are a few bits to API gateway v2. You need the lambda, an integration and a route. AWSX (Pulumi Crosswalk in their docs) is a higher level API which creates a few different things under the covers for you.
Copy code
const lambda = new aws.lambda.CallbackFunction(`${name}-lambda`, {
            callback: async (event: APIGatewayProxyEvent): Promise<any> => ({
        statusCode: HttpStatus.OK,
        body: JSON.stringify(await handleSetNamedUser(event)),
        isBase64Encoded: false,
      }),
        })

const integration = new aws.apigatewayv2.Integration(
    `${name}-integration`,
    {
        apiId,
        integrationMethod: 'POST',
        integrationType: 'AWS_PROXY',
        integrationUri: lambda.invokeArn,
    },
)

new aws.apigatewayv2.Route(
    `${name}-route`,
    {
        apiId,
        routeKey: 'api/named_users/associate',
        authorizationType: authorizerId ? 'JWT' : 'NONE',
        authorizerId: authorizerId,
        target: pulumi.interpolate`integrations/${integration.id}`,
    },
)
I pull the above into a component resource called api-route which creates all of those things, plus a log group etc
🙌 1
e

echoing-zebra-28421

06/05/2021, 2:34 AM
Thanks @curved-pharmacist-41509, although I'm still not very clear on how to implement the idea you gave me
c

curved-pharmacist-41509

06/05/2021, 2:34 AM
Each route needs those 3 resources
You create the api gateway, then you attach routes to them
Routes have an integration, you are using a lambda integration
It matches the AWS docs and way you configure things through the console much closer than the crosswalk approach
e

echoing-zebra-28421

06/05/2021, 2:35 AM
Here I have an example with V2 but I still don't see very well how to implement two routes that work in the same api https://github.com/pulumi/examples/blob/master/aws-ts-apigatewayv2-http-api/index.ts
c

curved-pharmacist-41509

06/05/2021, 2:36 AM
That is using the quick start which is a single route
There are two ways to do it
e

echoing-zebra-28421

06/05/2021, 2:54 AM
Thanks @curved-pharmacist-41509, I'll let you know when I do.🙌
2 Views