Reputation: 21
I am trying to run the two commands shown below on AWS lambda using python 3.7:
run_command("terraform init -reconfigure -upgrade")
run_command("terraform plan -out=plan.tfstate")
Below is the run_command function:
def run_command(command):
command_list = command.split(' ')
try:
logger.info("Running shell command: \"{}\"".format(command))
result = subprocess.run(command_list, stdout=subprocess.PIPE);
logger.info("Command output:\n---\n{}\n---".format(result.stdout.decode('UTF-8')))
except Exception as e:
logger.error("Exception: {}".format(e))
return False
return True
I installed the terraform zip as an AWS layer but when I try to run the terraform init/plan commands shown above I get the errors below.
[INFO] 2021-08-11T22:58:59.435Z 515d3670-be7c-46e1-8098-920ec9c63891 Running shell command: "terraform init -reconfigure -upgrade"
[ERROR] 2021-08-11T22:58:59.475Z 515d3670-be7c-46e1-8098-920ec9c63891 Exception: [Errno 2] No such file or directory: 'terraform': 'terraform'
[INFO] 2021-08-11T22:58:59.476Z 515d3670-be7c-46e1-8098-920ec9c63891 Running shell command: "terraform plan -out=plan.tfstate"
[ERROR] 2021-08-11T22:58:59.496Z 515d3670-be7c-46e1-8098-920ec9c63891 Exception: [Errno 2] No such file or directory: 'terraform': 'terraform'
I know that I am in the correct directory with a main.tf file when running the terraform command so that isn't the issue. Thus, why am I getting these errors?
I am not sure why I am getting these errors because I used the same process to run AWS CLI commands on lambda, and it is working correctly.
Upvotes: 1
Views: 880
Reputation: 919
Where did you create the layer? If on a linux machine, please be sure, to set chmod 755 before zipping the terraform binaries. If on windows, see alternative:
If this is not working, here is an alternative:
Run a chmod 755 within your Lambda to the binary before run the terraform cli.
Be sure, you downloaded the Linux version of terraform. I did the same with terraform cli and also with mysqldump binary, so this procedure should work (but not within a layer, maybe you have to pack it into your main source.
Side note: Use Python 3.8 for your lambda
Upvotes: 1