【问题标题】:How do you create an archive file in Terraform?如何在 Terraform 中创建存档文件?
【发布时间】:2018-06-22 09:00:37
【问题描述】:

我在 terraform 中有这段代码:

data "archive_file" "lambdazip" {

  type        = "zip"
  output_path = "lambda_launcher.zip"

  source_dir = "lambda/etc"
  source_dir = "lambda/node_modules"

  source {
    content  = "${data.template_file.config_json.rendered}"
    filename = "config.json"
  }
}

当我执行terraform plan 时出现以下错误:

* data.archive_file.lambdazip: "source": conflicts with source_dir 
("lambda/node_modules")
* data.archive_file.lambdazip: "source_content_filename": conflicts 
with source_dir ("lambda/node_modules")
* data.archive_file.lambdazip: "source_dir": conflicts with 
source_content_filename ("/home/user1/experiments/grascenote-
poc/init.tpl")

我使用的是 terraform 版本 v0.9.11

【问题讨论】:

  • 在archive_file模块中,你不能同时使用source(指定单个源文件的属性以包含到存档中)和source_dir(将该目录的全部内容打包到存档中)。

标签: terraform terraform-template-file


【解决方案1】:

@Ram 是正确的。您不能在同一个 archive_file 块中使用 source_dirsource

config_json.tpl

{"test": "${override}"}

地形 12.x

# create the template file config_json separately from the archive_file block
resource "local_file" "config" {
  content = templatefile("${path.module}/config_json.tpl", {
    override = "my value"
  })
  filename = "${path.module}/lambda/etc/config.json"
}

地形 11.x

data "template_file" "config_json" {
  template = "${file("${path.module}/config_json.tpl")}"
  vars = {
    override = "my value"
  }
}

# create the template file config_json separately from the archive_file block
resource "local_file" "config" {
  content  = "${data.template_file.config_json.rendered}"
  filename = "${path.module}/lambda/etc/config.json"
}

下一步

# now you can grab the entire lambda source directory or specific subdirectories
data "archive_file" "lambdazip" {
  type        = "zip"
  output_path = "lambda_launcher.zip"

  source_dir = "lambda/"
}

地形运行

$ terraform init
$ terraform apply
data.template_file.config_json: Refreshing state...
data.archive_file.lambdazip: Refreshing state...

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + local_file.config
      id:       <computed>
      content:  "{\"test\": \"my value\"}\n"
      filename: "/Users/user/lambda/config.json"


Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

local_file.config: Creating...
  content:  "" => "{\"test\": \"my value\"}\n"
  filename: "" => "/Users/user/lambda/config.json"
local_file.config: Creation complete after 0s (ID: 05894e86414856969d915db57e21008563dfcc38)

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

压缩文件内容

$ unzip -l lambda_launcher.zip
Archive:  lambda_launcher.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       21  01-01-2049 00:00   etc/config.json
       22  01-01-2049 00:00   node_modules/index.js
---------                     -------
       43                     2 files

【讨论】:

    【解决方案2】:

    如果是 node.js 的 lambda 函数,

    您需要将“resource.local_file”与“depends_on”一起使用。 并将“渲染文件”与“目录”分开。

    首先,将静态目录(etc,node_modules)放入“lambda”文件夹,不渲染文件。

    第二,将渲染文件放到任何其他路径中。

    data "template_file" "config_json" {
      template = "${file("${path.module}/config_json.tpl")}"
      vars = {
        foo = "bar"
      }
    }
    
    resource "local_file" "config_json" {
      content  = "${data.template_file.config_json.rendered}"
      filename = "${path.module}/lambda/config.json"
    }
    
    data "archive_file" "lambda_zip" {
      type        = "zip"
      output_path = "${path.module}/lambda_function.zip"
      source_dir = "${path.module}/lambda"
    
      # It is important to this process.
      depends_on = [
        "local_file.config_json" 
      ]
    }
    
    resource "aws_lambda_function" "lambda" {
      filename         = "${path.module}/lambda_function.zip"
      function_name    = "lambda_function"
      role             = "${aws_iam_role.lambda.arn}"
      handler          = "index.handler"
      runtime          = "nodejs10.x"
    }
    
    resource "aws_iam_role" "lambda" {
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-29
      • 1970-01-01
      • 1970-01-01
      • 2016-12-18
      • 2016-09-26
      • 1970-01-01
      • 2021-10-23
      相关资源
      最近更新 更多