【问题标题】:Running terraform init and plan commands on AWS lambda在 AWS lambda 上运行 terraform init 和 plan 命令
【发布时间】: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


【解决方案1】:

您在哪里创建了图层?如果在 linux 机器上,请确保在压缩 terraform 二进制文件之前设置 chmod 755。如果在 Windows 上,请参阅替代方案:

如果这不起作用,这里有一个替代方法

在运行 terraform cli 之前,在 Lambda 中运行 chmod 755 到二进制文件。

请确保您下载了 Linux 版本的 terraform。我对 terraform cli 和 mysqldump 二进制文件做了同样的事情,所以这个过程应该可以工作(但不是在一个层内,也许你必须将它打包到你的主源中。

旁注: 为您的 lambda 使用 Python 3.8

【讨论】:

猜你喜欢
  • 2021-10-31
  • 2022-08-24
  • 2018-11-28
  • 1970-01-01
  • 1970-01-01
  • 2021-12-17
  • 2019-10-30
  • 2022-12-19
  • 2018-11-21
相关资源
最近更新 更多