3
votes

I have written Terraform to create a Lambda function in AWS. This includes specifying my python code zipped. Running from Command Line into my tech box, all goes well. The terraform apply action sees my zip moved into AWS and used to create the lambda.

Key section of code :

resource "aws_lambda_function" "meta_lambda" {
              filename = "get_resources.zip"
              source_code_hash = filebase64sha256("get_resources.zip")
              .....

Now, to get this into other environments, I have to push my Terraform via Azure DevOps. However, when I try to build in DevOps, I get the following :

Error: Error in function call on main.tf line 140, in resource "aws_lambda_function" "meta_lambda": 140: source_code_hash = filebase64sha256("get_resources.zip") Call to function "filebase64sha256" failed: no file exists at get_resources.zip.

I have a feeling that I am missing a key concept here as I can see the .zip in the repo - so do not understand how it is not found by the build?

Any hints/clues as to what I am doing wrong, gratefully welcome.

enter image description here

3
Are you certain that the file is there before Terraform runs? Can you add a step that lists the directory or runs something like tree first?ydaetskcoR
ydaetskcoR, can you post as an answer and I'll apply the bounty (see my answer) as even if I found the solution by other means, I appreciate your input :)SimonB

3 Answers

3
votes

Chaps, I'm afraid that I may have just been over my head here - new to terraform & DevOps !
I had a word with our (more) tech folks and they have sorted this.

The reason I think yours if failing is becuase the Tar Terraform step needs to use a different command line so it gets the zip file included into the artifacts. tar -cvpf terraform.tar .terraform .tf tfplan tar --recursion -cvpf terraform.tar --exclude='/.git' --exclude='.gitignore' .

.. it that means anything to you ! Whatever they did, it works !

As there is a bounty on this, I still going to assign it as I am grateful for the input ! Sorry if this was a it of a newbie error.

2
votes

You can try building your package with terraform AWS lambda build module. As it has been very useful for the processTerraform Lambda build module

0
votes

According to the document example, in the source_code_hash argument, filebase64sha256 ("get_resources.zip") needs to be enclosed in double quotes.

enter image description here

You can refer to this document for details.