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

查看:282
本文介绍了如何通过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:\ ProgramData \ Amazon \ EC2-Windows \ Launch \ Scripts \ InitializeInstance.ps1时出错 -Schedule':退出状态1.输出:术语'C:\ ProgramData \ Amazon \ EC2-Windows \ Launch \ Scripts \ InitializeInstance.ps1' 无法识别为cmdlet,函数,脚本文件或 可操作的程序.检查名称的拼写,或者路径是否为 (包括),验证路径正确,然后重试.在第1行 字符:72
  • C:\ ProgramData \ Amazon \ EC2-Windows \ Launch \ Scripts \ InitializeInstance.ps1 <<<< -日程
    • CategoryInfo:ObjectNotFound:(C:\ ProgramData ... izeInstance.ps1:String)[], CommandNotFoundException
    • FullyQualifiedErrorId:CommandNotFoundException
  • aws_instance.my-test-instance: Error running command 'C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 -Schedule': exit status 1. Output: The term 'C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:72
  • C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 <<<< -Schedule
    • CategoryInfo : ObjectNotFound: (C:\ProgramData...izeInstance.ps1:String) [], CommandNotFoundException
    • FullyQualifiedErrorId : CommandNotFoundException

推荐答案

您正在使用

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脚本,在这种情况下,您将需要使用

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.

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

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>

使用userdata文件启动实例:

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天全站免登陆