如何通过 Terraform 执行 PowerShell 命令 [英] How to execute PowerShell command through Terraform

查看:48
本文介绍了如何通过 Terraform 执行 PowerShell 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 AMI 创建一个 Windows Ec2 实例并在其上执行一个 powershell 命令:

I am trying to create a Windows Ec2 instance from AMI and executing a powershell command on that as :

data "aws_ami" "ec2-worker-initial-encrypted-ami" {
    filter {
        name   = "tag:Name"
        values = ["ec2-worker-initial-encrypted-ami"]
    }  
}

resource "aws_instance" "my-test-instance" {
  ami             = "${data.aws_ami.ec2-worker-initial-encrypted-ami.id}"
  instance_type   = "t2.micro"

  tags {
    Name = "my-test-instance"
  }

  provisioner "local-exec" {
    command = "C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 -Schedule",
    interpreter = ["PowerShell"]
  }

}

我面临以下错误:

  • aws_instance.my-test-instance:错误运行命令C:ProgramDataAmazonEC2-WindowsLaunchScriptsInitializeInstance.ps1-Schedule:退出状态 1. 输出:术语 'C:ProgramDataAmazonEC2-WindowsLaunchScriptsInitializeInstance.ps1'未被识别为 cmdlet、函数、脚本文件的名称,或可运行的程序.检查名称的拼写,或者路径是否包括,验证路径是否正确,然后重试.在线:1字符:72
  • C:ProgramDataAmazonEC2-WindowsLaunchScriptsInitializeInstance.ps1<<<<-日程
    • CategoryInfo : ObjectNotFound: (C:ProgramData...izeInstance.ps1:String) [],CommandNotFoundException
    • FullyQualifiedErrorId : CommandNotFoundException

    推荐答案

    您使用的是 local-exec 在运行 Terraform 的工作站上运行请求 powershell 代码的配置程序:

    You are using a local-exec provisioner which runs the request powershell code on the workstation running Terraform:

    local-exec 配置器在资源之后调用本地可执行文件被建造.这会在运行 Terraform 的机器上调用一个进程,不在资源上.

    The local-exec provisioner invokes a local executable after a resource is created. This invokes a process on the machine running Terraform, not on the resource.

    听起来您想在生成的实例上执行 powershell 脚本,在这种情况下,您需要使用 remote-exec 配置器,它将在目标资源上运行你的 powershell:

    It sounds like you want to execute the powershell script on the resulting instance in which case you'll need to use a remote-exec provisioner which will run your powershell on the target resource:

    remote-exec 配置器调用远程资源上的脚本创建后.这可用于运行配置管理工具,引导到集群等.

    The remote-exec provisioner invokes a script on a remote resource after it is created. This can be used to run a configuration management tool, bootstrap into a cluster, etc.

    您还需要包含连接详细信息,例如:

    You will also need to include connection details, for example:

      provisioner "remote-exec" {
        command = "C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 -Schedule",
        interpreter = ["PowerShell"]
        connection {
          type     = "winrm"
          user     = "Administrator"
          password = "${var.admin_password}"
        }
      }
    

    这意味着该实例也必须准备好接受 WinRM 连接.

    Which means this instance must also be ready to accept WinRM connections.

    不过,还有其他选项可以完成此任务.比如使用userdataTerraform 也支持.这可能类似于以下示例:

    There are other options for completing this task though. Such as using userdata, which Terraform also supports. This might look like the following example:

    <powershell>
    C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 -Schedule
    </powershell>
    

    使用用户数据文件启动实例:

    resource "aws_instance" "my-test-instance" {
      ami             = "${data.aws_ami.ec2-worker-initial-encrypted-ami.id}"
      instance_type   = "t2.micro"
    
      tags {
        Name = "my-test-instance"
      }
    
      user_data = "${file(userdata.txt)}"
    }
    

    文件插值会读取userdata 文件作为字符串传递给 userdata 以用于实例启动.一旦实例启动,它应该会按照您的预期运行脚本.

    The file interpolation will read the contents of the userdata file as string to pass to userdata for the instance launch. Once the instance launches it should run the script as you expect.

    这篇关于如何通过 Terraform 执行 PowerShell 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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