如何通过bash脚本在terraform中使用外部数据源

时间:2018-09-10 16:48:26

标签: terraform

我有一个bash脚本,它将返回一个AMI ID。我想使用从bash脚本返回的AMI ID作为启动配置的输入。

data "external" "amiid" {
  program = ["bash", "${path.root}/scripts/getamiid.sh"]
}

resource "aws_launch_configuration" "bastion-lc" {
  name_prefix                 = "${var.lc_name}-"
  image_id                    = "${data.external.amiid.result}"
  instance_type               = "${var.instance_type}"
  placement_tenancy           = "default"
  associate_public_ip_address = false
  security_groups             = ["${var.bastion_sg_id}"]
  iam_instance_profile        = "${aws_iam_instance_profile.bastion-profile.arn}"

  lifecycle {
   create_before_destroy = true
  }
}

当我使用terraform计划运行此程序时,出现错误消息

* module.bastion.data.external.amiid: 1 error(s) occurred:

* module.bastion.data.external.amiid: data.external.amiid: command "bash" produced invalid JSON: invalid character 'a' looking for beginning of object key string

这是getamiid.sh脚本:

#!/bin/bash
amiid=$(curl -s "https://someurl" | jq -r 'map(select(.tags.osVersion | startswith("os"))) | max_by(.tags.creationDate) | .id')
echo -n "{ami_id:\"${amiid}\"}"

运行脚本时,它将返回:

{ami_id:"ami-xxxyyyzzz"}

0 个答案:

没有答案