https://pulumi.com logo
#getting-started
Title
# getting-started
t

thankful-ambulance-76337

02/08/2024, 10:39 PM
I’m trying to convert an existing CF template to pulumi. I’m having trouble with AWS Lambdas and am using pulumi in typescript in my project i have a lambdas directory which has a few lambdas - some in typescript and some in python. The typescript lambdas are their own projects (they have node_modules and package.json for example). When I try to set up a lambda like this, i get an error:
api error RequestEntityTooLargeException: Request must be smaller than 70167211 bytes for the CreateFunction operation
Copy code
const myLambda = new aws.lambda.Function(
  "myLambda",
  {
    code: new pulumi.asset.AssetArchive({
      ".": new pulumi.asset.FileArchive("./lambdas/typescript_proj1"),
    }),
    handler: "src/index.handler",
    runtime: "nodejs18.x",
    role: lambdaRole.arn,
    timeout: 120,
  }
)
My thought is this actually has something to do with typescript. Especially since CF allows me to pass in a BuildMethod of esbuild but there’s no equivelent in pulumi that i can find
w

wide-nest-220

02/09/2024, 3:20 PM
I don't know much about Pulumi but are you adding the devDependencies to your dependencies in the package.json as this is just a lambda limit.
m

magnificent-soccer-44287

02/11/2024, 2:00 PM
@thankful-ambulance-76337 I believe nodejs18+ uses the AWS SDK v3 by default, so assuming the following folder structure (attached)
Copy code
index.ts
lambda/s3-retrieval.js
lambda/package.json
you'd use:
Copy code
code: new pulumi.asset.FileArchive("./lambda"),
    runtime: aws.lambda.Runtime.NodeJS20dX,
    handler: 's3-retrieval.handler',
    tracingConfig: {
        mode: "Active",
    },
    memorySize: 256,
    timeout: 10,
    role: lambdaRole.arn,
    environment: {
        variables: {
            BUCKET_NAME: assetsBucket.bucket,
        },
    },
A few things: a) There is no need to use AssetArchive - FileArchive zips the entire folder it points to. b) Within that folder, the handler is [fileName].[methodName] So above: /lambda/s3-retrieval.js : handler is what gets invoked. If the entire folders' size (including node_modules) is too large, you'd see that message. You can reduce your node_modules size (assuming this is indeed the issue and you haven't ran into a legitimate bug) by using AWS SDKv3 - specific imports, as such:
Copy code
import { GetObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { gzipSync } from "zlib";
the SDK v3 imports and usage are very different from the SDK v2. This has caused me a lot of grief before.
In package.json (this is more of a Lambda than a Pulumi thing), you usually either want:
Copy code
{
  "private": "true",
  "type": "module"
}
or an mjs extension in the file containing the handler.