https://pulumi.com logo
#aws
Title
# aws
a

aloof-sugar-9157

01/12/2024, 2:41 PM
Hey Guys I could use some insight or advice, I have a pulumi script that builds a lambda and is activated by a github action. This action is triggered when someone makes changes to the lambda's code base in the repo and is merged to main. About 20% to 20% of the time I get this error.
Copy code
error: 1 error occurred:
    	* creating Lambda Function (prod-customer-service-lambda-75e83a8): operation error Lambda: CreateFunction, https response error StatusCode: 400, RequestID: 4345617f-2790-46cc-a88e-1ed70eada3c2, InvalidParameterValueException: Uploaded file must be a non-empty zip
So I switched from using the command module to build then zip to the runOutput and AssetArchive option. I found that by adding some console.log I get better results. But still see the above error. Here is the conso.log showing the archive from the same update that produced the error above.
Copy code
}
    lambdaAssetResult Archive: {
      'dist/index.js': FileAsset {
        __pulumiAsset: true,
        path: Promise {
          '../../domains/lambdaName/microservices/lambdaName/dist/index.js'
        }
      }
    }
Here is the snippet of the lambda creation script creating the AssetArchive
Copy code
const lambdaAssetResult = command.local.runOutput({
  command: `npm run build`,
  assetPaths: ["dist/**.js"],
  dir: "../../domains/lambdaName/microservices/lambdaName/"
});

lambdaAssetResult.apply((result)=>{
  //he logging helps it work for production for some reason
  console.log('lambdaAssetResult:', result)
  console.log('lambdaAssetResult Archive:', result?.assets)
})

const lambdaAsset = lambdaAssetResult.apply((result)=>{
  return new pulumi.asset.AssetArchive({'index.js': result!.assets!['dist/index.js']})
});

if (!lambdaAsset) {
  throw new Error("Failed to create archive");
}
Here is the snippet where we define the lambda and apply the AssetArchive for the code.
Copy code
export const myLambda = new aws.lambda.Function(`${env}-customer-service-lambda`, {
  code: lambdaAsset,
  description: `Lambda for things we do. Last updated: ${Date.now()}`,
  role: lambdaRole.arn,
  handler: "index.lambdaEntry",
  runtime: "nodejs18.x",
  kmsKeyArn: encryptionKey.arn,
  environment: {
    variables: {
      RDS: Rds,
      USERNAME: encryptEnv(env == "prod" ? config.requireSecret("USERNAME") : mySqlUser?.user ?? ""),
      PASSWORD: encryptEnv(env == "prod" ? config.requireSecret("PASSWORD") : rdsPassword ?? ""),
      KMS: encryptionKey.arn
    }
  },
  timeout: 10,
  vpcConfig: {
    securityGroupIds: [lambdaSecurityGroup.id],
    subnetIds: privateSubnets,
  },
}, {
  dependsOn: [
    lambdaExecutionPolicyAttachment
  ],
  replaceOnChanges: ["*"]
});
I am open to suggestions. I have tried to add to the dependsOn section the lambdaAsset but I get a slew of issues that I am not able to figure out.
Copy code
Type 'Output<AssetArchive>' is not assignable to type 'Input<Resource>'.
  Type 'Output<AssetArchive>' is not assignable to type 'OutputInstance<Resource>'.
    Types of property 'apply' are incompatible.
      Type '{ <U>(func: (t: AssetArchive) => Promise<U>): Output<U>; <U>(func: (t: AssetArchive) => OutputInstance<U>): Output<...>; <U>(func: (t: AssetArchive) => U): Output<...>; }' is not assignable to type '{ <U>(func: (t: Resource) => Promise<U>): Output<U>; <U>(func: (t: Resource) => OutputInstance<U>): Output<U>; <U>(func: (t: Resource) => U): Output<...>; }'.
        Types of parameters 'func' and 'func' are incompatible.
          Types of parameters 't' and 't' are incompatible.
            Type 'AssetArchive' is missing the following properties from type 'Resource': urn, getProviderts(2322)
const lambdaAsset: pulumi.Output<pulumi.asset.AssetArchive>
I am also thinking of have the github action build the zip archive but I am not clear yet on how to export / import that to the pulumi stack and
code:
value of the aws.lambda.Function. Thank you again for time and advice!