【问题标题】:Terraform, getting output from null_resource, local-exec and the AWS CLITerraform,从 null_resource、local-exec 和 AWS CLI 获取输出
【发布时间】:2016-11-28 14:46:54
【问题描述】:

我正在使用 Terraform 在 AWS 中自动配置 Cognito 身份池。 AWS 提供商还不支持 Cognito,所以我一直在使用 null_resource 和 local-exec 来调用 AWS CLI。

我有以下资源:

resource "null_resource" "create-identitypool" {
    provisioner "local-exec" {
        command = "aws cognito-identity create-identity-pool --identity-pool-name terraform_identitypool --no-allow-unauthenticated-identities --developer-provider-name login.terraform.myapp"
    }
}

给出以下输出:

null_resource.create-identitypool (local-exec): {
null_resource.create-identitypool (local-exec):     "IdentityPoolId": "eu-west-1:22549ad3-1611-......",
null_resource.create-identitypool (local-exec):     "AllowUnauthenticatedIdentities": false,
null_resource.create-identitypool (local-exec):     "DeveloperProviderName": "login.terraform.myapp",
null_resource.create-identitypool (local-exec):     "IdentityPoolName": "terraform_identitypool"
null_resource.create-identitypool (local-exec): }
null_resource.create-identitypool: Creation complete

下一步是将我已经创建的一些角色添加到身份池中:

resource "null_resource" "attach-policies-identitypool" {
    provisioner "local-exec" {
        command = "aws cognito-identity set-identity-pool-roles --identity-pool-id ${null_resource.create-identitypool.IdentityPoolId} --roles authenticated=authroleXXX,unauthenticated=unauthroleXXX"
    }
}

问题是我无法提取 IdentityPoolId ${null_resource.create-identitypool.IdentityPoolId} 以在第二个资源中使用。我知道 null_resource 没有输出属性,所以我怎样才能从命令行输出中获取这个 JSON 对象。我还想使用 tirggers 并运行 aws cognito-identity list-identity-pools 和可能的 delete-identity-pool 以使这一切都可以重复,我还需要输出。

有什么想法吗?如果我在其他地方错过了这些信息,我们深表歉意。我也在 Terraform 邮件列表上问过这个问题,但我想我会尝试更多的受众。

谢谢, 蒂姆

【问题讨论】:

  • 我不必这样做,所以我不能保证,但你可以尝试让命令将其输出重定向到一个文件(也许通过sed 发送它以提取您需要的确切值),然后在您需要输出的地方使用文件资源。然后确保在需要文件的资源中添加适当的depends_on 属性,以确保它们在生成后运行。
  • 另外,如果他们的 GItHub 存储库中还没有关于从 null_resource 定义输出的功能请求,您可能需要打开一个。我相信其他人会发现它很有用,并且很有可能在未来的版本中实现它。
  • @tim_barber_7BB 你最终是怎么做到的?有人在这吗?
  • @Christine 我没有解决这个问题。我认为这将是该过程的手动部分(除非有人想出什么办法),目前我不会再花时间调查。

标签: amazon-web-services aws-cli amazon-cognito terraform


【解决方案1】:

Terraform 0.8 中有一个新的数据源external,允许您运行外部命令并提取输出。见data.external

数据源应用于检索 Cognito 数据,而不是执行它。由于这是一个 Terraform 数据源,它应该没有任何副作用。

【讨论】:

    【解决方案2】:

    保罗的回答是正确的。但是,外部数据只有在 shell 脚本以 JSON 格式发回数据时才有效,这需要更多的工作。

    因此,Matti Paksula 为此制作了一个模块。 (https://github.com/matti/terraform-shell-resource)。

    使用该模块,我们可以获得任何 shell 脚本 local-exec 调用的 stdout、stderr 和退出状态。

    这是一个示例 main.tf 文件。您可以以任何方式修改它,以运行任何您想要的命令,包括您问题中的命令。

    #  Defining a variable , we will feed to the shell script
    variable "location" { default = "us-central1-f" }
    
    
    # Calling Matti's Module
    module "shell_execute" {
      source  = "github.com/matti/terraform-shell-resource"
      command = "./scripts/setenv.sh"
    }
    
    
    # Creating a shell script on the fly
    resource "local_file" "setenvvars" {
      filename = "./scripts/setenv.sh"
      content  = <<-EOT
        #!/bin/bash
        export LOCATION=${var.modinput_location}
        echo LOCATION $LOCATION
      EOT
    }
    
    #  Now, we get back the output of the script
    output "shell_stdout" {
      value = module.shell_execute.stdout
    }
    
    #  Now, we get back if there are any errors
    output "shell_stderr" {
      value = module.shell_execute.stderr
    }
    
    #  Now, we get back exit status of the script
    output "shell_exitstatus" {
      value = module.shell_execute.exitstatus
    }

    【讨论】:

    • shell_execute 在 linux_amd64 上使用 v1.0.5 为我工作
    • 注意安全隐患 - terraform-shell-resource 模块使用临时文件来捕获输出。
    猜你喜欢
    • 2020-03-22
    • 2020-12-24
    • 2020-07-07
    • 1970-01-01
    • 2011-11-09
    • 2020-07-01
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    相关资源
    最近更新 更多