Home > Back-end >  AWS S3 Error occurred while GetObject. S3 Error Code: NoSuchKey with Terraform
AWS S3 Error occurred while GetObject. S3 Error Code: NoSuchKey with Terraform

Time:12-15

I'm getting Error occurred while GetObject. S3 Error Code: NoSuchKey even though I've created a bucket, is there a wait time to use the key after creating the bucket?

Here's my terraform script

resource "aws_s3_bucket" "lambda_fn_upload" {
  bucket = var.bucket
  acl = "private"
}

The bucket gets created successfully

aws_s3_bucket.lambda_fn_upload: Creation complete after 3s [id=lambda-fns-relieved-copper]

and key gets created as well using the bucket object

aws_s3_bucket_object.order_status_file_upload: Creation complete after 4s [id=lambda-fns/orderStatus/function.zip]

And when I try to use the s3_key in my lambda, it fails

resource "aws_lambda_function" "order_status" {
  # For files larger than 10 MB, consider uploading using Amazon S3.
  s3_bucket         = aws_s3_bucket.lambda_fn_upload.id
  s3_key            = "lambda-fns/orderStatus/function.zip"
  # filename      = "../orderStatus/function.zip"
  function_name = "orderStatus"
  role          = aws_iam_role.lambda_fn_role.arn
  handler       = "dist/handlers.orderStatus"

  source_code_hash = filebase64sha256("../orderStatus/function.zip")

  runtime = "nodejs12.x"

  depends_on = [
    aws_s3_bucket.lambda_fn_upload
  ]
}

I get the following error

Error: error creating Lambda Function (1): InvalidParameterValueException: Error occurred while GetObject. S3 Error Code: NoSuchKey. S3 Error Message: The specified key does not exist.

The issue happens when I run both scripts at the same time, if run the bucket script and then run the lambda script it doesn't fail.

CodePudding user response:

The problem is that your order_status resource depends on the creation of the S3 bucket, which is fine, but it should also depend on the upload of the packaged Lambda function. Modify the Lambda function dependency, as follows:

depends_on = [
    aws_s3_bucket.lambda_fn_upload,
    aws_s3_bucket_object.order_status_file_upload
]
  • Related