Как создать архивный файл в 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


person Know Nothing    schedule 12.01.2018    source источник
comment
В модуле archive_file вы не можете использовать источник (указывает атрибуты одного исходного файла для включения в архив) и source_dir (упаковать все содержимое этого каталога в архив) вместе.   -  person Ram    schedule 13.01.2018


Ответы (2)


@ Ram правильный. Вы не можете использовать source_dir и source в одном блоке archive_file.

config_json.tpl

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

Terraform 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"
}

Terraform 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 run

$ 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.

Содержимое ZIP-файла

$ 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
person SomeGuyOnAComputer    schedule 20.04.2019

В случае лямбда-функции node.js

Вам нужно использовать "resource.local_file" вместе с "depends_on". И отдельный «файл рендеринга» с «каталогом».

Во-первых, поместите статический каталог (и т. Д., Node_modules) в папку «лямбда» без рендеринга файлов.

Во-вторых, поместите файлы рендеринга в любой другой путь.

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" {
...
person GNOKOHEAT    schedule 15.10.2019