【发布时间】:2021-10-15 09:01:25
【问题描述】:
我正在尝试使用 python 3.7 在 AWS lambda 上运行下面显示的两个命令:
run_command("terraform init -reconfigure -upgrade")
run_command("terraform plan -out=plan.tfstate")
下面是run_command函数:
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
我将 terraform zip 安装为 AWS 层,但是当我尝试运行上面显示的 terraform init/plan 命令时,出现以下错误。
[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'
我知道我在运行 terraform 命令时位于带有 main.tf 文件的正确目录中,所以这不是问题。那么,为什么会出现这些错误?
我不确定为什么会出现这些错误,因为我使用相同的流程在 lambda 上运行 AWS CLI 命令,并且它工作正常。
【问题讨论】:
-
听起来
terraform没有正确安装在PATH中 -
我不建议在 Lambda 中运行
terraform。 Lambdas 的一大警告是它们只运行 900 秒(15 分钟)。有些事情的部署时间超过 15 分钟。例如:AWS 中的 Elasticsearch 实例、对大型 DynamoDB 表的更新等。然后您的 terraform 超时可能会使您的 Terraform 状态和基础设施损坏。请不要那样做。 -
@Jens 如果他们只是运行
plan,这是问题中所述的,那么他们应该没问题。我同意在 Lambda 环境中运行apply不会很好。
标签: amazon-web-services aws-lambda terraform