I am deploying a lambda function through CDK. The function uses the aws-sdk for nodejs to send a HeadObject request to a multi region access point (MRAP). When i send this HeadObject request, the sdk throws an error with this message:

Please check whether you have installed the "@aws-sdk/signature-v4-crt" package explicitly. 
You must also register the package by calling [require("@aws-sdk/signature-v4-crt");] or an ESM equivalent such as [import "@aws-sdk/signature-v4-crt";]. 
For more information please go to https://github.com/aws/aws-sdk-js-v3#functionality-requiring-aws-common-runtime-crt

I tried following the steps mentioned in the github link, but the error persists. Strangely, when i download the lambda source code, I can clearly see the package inside node_modules extracted lambda function code

My code roughly looks like:

import '@aws-sdk/signature-v4-crt'

import { HeadObjectCommand, S3Client, NotFound } from '@aws-sdk/client-s3'

export const s3ObjectExists = async (): Promise<boolean> => {
  const s3 = new S3Client()
  const bucket = '{MRAP ARN}'
  const key = 'key for s3 item'

  try {
    // throws
    await client.send(
      new HeadObjectCommand({
        Key: key,
        Bucket: bucket,
      })
    )
    return true
  } catch (e) {
    if (e instanceof NotFound) {
      return false
    }

    // This statement is logging, so it's -not- a NotFound error
    log.error('Failed to check if object exists', { error: e })
    throw e
  }
}

In case anyone asks, the s3 api supports the bucket being the ARN of an MRAP. I'm able to run the AWS CLI equivalent of this code just fine. (aws s3api head-object --key {key} --bucket {MRAP arn})

1

There are 1 best solutions below

0
On BEST ANSWER

Found the cause of my problem - It was actually two issues. First, I needed to include aws-crt in the bundlingOptions.nodeModules prop in the NodeJsFunction CDK construct. This tells CDK to install the package, in the lambda, rather than building it into the CDK bundle. Interestingly, I did not need to include aws-crt in my package.json at all.

This resulted in UnknownError being thrown when calling the HeadObject command. This was due to the lambda missing the policy that grants S3:GetObject.

Once i had both of these fixes in place, the issue went away.