如何使用 Terraform 分配和导出变量 [英] How to assign and export variable using Terraform

查看:29
本文介绍了如何使用 Terraform 分配和导出变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 API 从第三方服务器获取密码,然后将其分配给参数存储:

I want to get password from an third party server using API and then assign it to parameter store:

最初我认为我可以使用 $TF_VAR_password 作为桥梁来实现这一点:

Initially I think I can use $TF_VAR_password as a bridge to achieve that:

第 1 步:创建一个 .env 文件:

step 1: create an .env file:

cat test.env 
export TF_VAR_password=< 3rd party API to get the password, say 123456 >

第二步:使用 Terraform null_resource 执行 test.env 以导出 TF_VAR_password:

step 2: use Terraform null_resource to execute the test.env in order to export TF_VAR_password:

resource "null_resource" "get_password" {
  triggers = {
    always_run = "${timestamp()}"
  }

  provisioner "local-exec" {
    command = "source ./${path.module}/test.env"
  }
}

第 3 步:将 TF_VAR_password(即 var.password)分配给参数存储:

step 3: assign the TF_VAR_password (i.e. var.password) to the parameter store:

resource "aws_ssm_parameter" "abc" {
  value             = var.password  <---------
  ...
}

当我运行上述 Terraform 时,它显示:执行:[/bin/sh"-c"源./../../../../aws/modules/test.env"]

when I run the above Terraform, it shows: Executing: ["/bin/sh" "-c" "source ./../../../../aws/modules/test.env"]

但是当我运行 echo $TF_VAR_password 时,它不会将 $TF_VAR_password 显示为 123456.

But when I run echo $TF_VAR_password it does not show $TF_VAR_password as 123456.

如果我直接运行 source ./../../../../aws/modules/test.env ,我看到 $TF_VAR_password 变成 123456,这意味着脚本本身是正确.

If I run source ./../../../../aws/modules/test.env directly, I see $TF_VAR_password becomes 123456, which means the script itself is correct.

如何从第三方服务器获取密码并将其分配给参数存储?

How can I fetch and assign the password from the third-party server to the parameter store?

推荐答案

HashiCorp 有一个名为 external 这将允许您调用您的 API,获取密码,然后将其返回到您的 TF 脚本以供以后使用.

HashiCorp has dedicated data source called external which would allow you to call your API, get the password, and then return it to your TF script for later use.

这将要求您以 JSON 格式返回结果,如 docs.

This would require you to return results in JSON form as shown in the docs.

这篇关于如何使用 Terraform 分配和导出变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆