Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.12-rc1 file function fails due to UTF-8 error #21260

Closed
sgript opened this issue May 9, 2019 · 3 comments
Closed

0.12-rc1 file function fails due to UTF-8 error #21260

sgript opened this issue May 9, 2019 · 3 comments
Labels
Milestone

Comments

@sgript
Copy link

sgript commented May 9, 2019

Hey there,

I'm trying to upload lambda code using the archive_file function, however, keep running into this error:

Error: Error in function call

  on ../../modules/aws/lambda/main.tf line 13, in resource "aws_lambda_function" "lambda_at_edge":
  13:   source_code_hash = "${base64sha256(file("${data.archive_file.lambda.output_path}"))}"
    |----------------
    | data.archive_file.lambda.output_path is "../../modules/aws/lambda/lambda_at_edge.zip"

Call to function "file" failed: contents of
../../modules/aws/lambda/lambda_at_edge.zip are not valid UTF-8; use the
filebase64 function to obtain the Base64 encoded contents or the other file
functions (e.g. filemd5, filesha256) to obtain file hashing results instead.

Exited with code 1

The code looks like:

  type        = "zip"
  source_dir  = "${path.module}/lambda_at_edge"
  output_path = "${path.module}/lambda_at_edge.zip"
}

resource "aws_lambda_function" "lambda_at_edge" {
  filename         = "${data.archive_file.lambda.output_path}"
  function_name    = "${var.application}-lambda-at-edge"
  role             = "${var.iam_role_arn}"
  handler          = "index.handler"
  runtime          = "nodejs6.10"
  source_code_hash = "${base64sha256(file("${data.archive_file.lambda.output_path}"))}"
  publish          = "true"
}

The lambda function itself is just a regular node function.

I have looked at the official docs and other examples and can't seem to see what the issues are. Is this related to the new version of Terraform?

Thanks.

@apparentlymart
Copy link
Member

Hi @sgript! Sorry for this difference in behavior.

In Terraform v0.12, the file function can only load text files (UTF-8 encoded) because Terraform language strings are always unicode and so loading raw binary data into them can risk corruption of that data. Although in this particular case this used to work in Terraform 0.11 (because the file result was immediately passed to the base64sha256 function in memory), the file function is conservative now to avoid that risk.

The fix for this is, thankfully, straightforward: the new function filebase64sha256 combines the functionality of file and base64sha256 together, avoiding the need to return the raw binary data as a string, so if you rewrite your source_code_hash as follows then I think it should work:

  source_code_hash = filebase64sha256(data.archive_file.lambda.output_path)

The terraform 0.12upgrade command described in the upgrade guide includes a rule to make this change automatically, so I'd recommend using that tool to apply all of the upgrade rules at once, to see what else it proposes. If you were to make the above change manually then the upgrade tool wouldn't be able to read it anymore (because it's no longer valid 0.11 syntax), so best to run terraform 0.12upgrade first -- before making any other changes -- and then see what else might need tweaking from there.

@apparentlymart apparentlymart added this to the v0.12.0 milestone May 9, 2019
@sgript
Copy link
Author

sgript commented May 9, 2019

This seemed to work, thanks @apparentlymart!

@sgript sgript closed this as completed May 9, 2019
@ghost
Copy link

ghost commented Jul 26, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Jul 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants