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

gentle-airline-14942

06/30/2023, 2:38 AM
I have a container-based lambda, and want to associate it to an API gateway in AWS. The article here assumes a standalone (non-container)-based lambda. Would anyone know how to do this? Now that I think about it, maybe it's as simple as assigning the reference from
new aws.lambda.Function()
to
eventHandler
https://www.pulumi.com/docs/clouds/aws/guides/api-gateway/#lambda-request-handling
Doesn't work
Copy code
index.ts(12,7): error TS2322: Type 'Function' is not assignable to type 'Input<Function>'.
e

elegant-gigabyte-8733

06/30/2023, 4:38 AM
Something like this?
Copy code
import * as aws from "@pulumi/aws";
import * as pulumi from "@pulumi/pulumi";

// Create an AWS resource (S3 Bucket)
const bucket = new aws.s3.Bucket("my-bucket");

// Create the ECR repository to hold our Docker image
const repository = new aws.ecr.Repository("myRepository");

// Create the Docker image
const image = aws.ecr.getRegistry().then(registry => {
    const imageName = `${registry.registryId}.<http://dkr.ecr.region.amazonaws.com/${repository.name}`;|dkr.ecr.region.amazonaws.com/${repository.name}`;>
    return new docker.Image("myImage", {
        imageName: imageName,
        build: "./appDir",  // directory containing Dockerfile
    });
});

// Assume that we have a role for the Lambda to assume
const lambdaRole = aws.iam.getRole({ name: "lambdaRole" });

// Create a Lambda function, using the Docker image
const lambdaFunction = new aws.lambda.Function("mylambda", {
    packageType: "Image",
    imageUri: image.imageName,
    role: lambdaRole.arn
});

// Create a REST API that routes to our Lambda function
const api = new aws.apigatewayv2.Api("myapi", {
    protocolType: "HTTP",
    target: pulumi.interpolate`${lambdaFunction.arn}:$LATEST`,
});

// Export the ARN of the Lambda Function and the URL of the API Gateway
export const lambdaArn = lambdaFunction.arn;
export const url = api.apiEndpoint;
g

gentle-airline-14942

06/30/2023, 5:19 PM
unfortunately no, wouldn't that be the same as using
aws.lambda.FunctionUrl()
?
unless i can attach several of those defs to an API server that can serve multiple routes
I'd like to use an API gateway server vs an individual FunctionUrl because it's easier to export and manintain a single API resource vs multiple FunctionUrl references
this works, but I'm not sure how i'd be able to re-use the API gateway for other lambdas - a version change in the lambda would result in a completely new API gateway
Copy code
const lambda = new aws.lambda.Function('my-lambda', {
    name: ...
	publish: true,
	packageType: 'Image',
	imageUri: ...
})

// Create a REST API that routes to our Lambda function
const api = new apigatewayv2.Api('ai-lambda-gateway', {
  protocolType: 'HTTP',
  routeKey: 'POST /pdf/scrape',
  target: pulumi.interpolate`${lambda.arn}:${lambda.version}`,
  corsConfiguration: {
    allowCredentials: false,
    allowHeaders: ['*'],
    allowMethods: ['POST'],
    allowOrigins: ['*'],
  },
});

new lambda.Permission('pdf-scraper-perm', {
  action: 'lambda:InvokeFunction',
  function: lambda,
  principal: '<http://apigateway.amazonaws.com|apigateway.amazonaws.com>',
  sourceArn: pulumi.interpolate`${api.executionArn}/*/*/pdf/scrape`,
  qualifier: lambda.version,
});
e

elegant-gigabyte-8733

06/30/2023, 8:56 PM
Holy smokes that is not straight forward
g

gentle-airline-14942

06/30/2023, 8:57 PM
I take it it's not a common thing people do in general which is why there's no resources on how to do it
e

elegant-gigabyte-8733

06/30/2023, 8:57 PM
I save this to my knowledge repo because it’s something I will likely do