简体   繁体   English

Terraform EC2 实例导入 - 用户数据不同

[英]Terraform EC2 instance import - user data different

I am attempting to import an existing EC2 instance into Terraform.我正在尝试将现有的 EC2 实例导入 Terraform。 I have taken the EC2 instance User Data, and added it to my TF config file eg我已经获取了 EC2 实例用户数据,并将其添加到我的 TF 配置文件中,例如

    user_data               = <<EOF
<powershell>
& $env:SystemRoot\System32\control.exe "intl.cpl,,/f:`"UKRegion.xml`""
& tzutil /s "GMT Standard Time" 
Set-Culture en-GB
</powershell>
EOF

The resource imports OK, but when I run terraform plan I get TF wanting to destroy and recreate the instance, as a 'change' in user_data 'forces new resource'.资源导入正常,但是当我运行terraform plan我让 TF 想要销毁并重新创建实例,作为 user_data 中的“强制新资源”中的“更改”。

user_data: "946f756af0df239b19f86a72653e58dcc04c4b27" => "811599030dc713b18c3e35437a82b35095190a81" (forces new resource)

I have tried copy and pasting the user data from EC2 console into the TF file, but this is not working.我尝试将用户数据从 EC2 控制台复制并粘贴到 TF 文件中,但这不起作用。 Is this at all possible?这可能吗?

Per this github issue , it looks like this is an issue with how terraform interprets the user_data as a "computed" value.根据这个 github问题,这似乎是 terraform 如何将 user_data 解释为“计算”值的问题。 There appears to be a work around.似乎有解决办法。

  • First run a plan/apply cycle with your plan command including the extra argument on your command line:首先使用您的计划命令运行计划/应用循环,包括命令行上的额外参数:
    -target=template_file.userdata-consul. -target=template_file.userdata-consul。 This will tell Terraform to do the minimal work it needs to update the template file, which should这将告诉 Terraform 做更新模板文件所需的最少工作,这应该
    leave your launch configuration untouched.保持您的启动配置不变。
  • Now run plan again, and since the template_file has now already been recreated it should interpolate the resolved template as expected into the user_data, and there should then be no diff because the "new" template rendering should be the same as the "old" one.现在再次运行计划,因为 template_file 现在已经被重新创建,它应该按预期将解析的模板插入到 user_data 中,然后应该没有差异,因为“新”模板渲染应该与“旧”模板渲染相同.

You can't copy the user_data value from the state, as it is an encoded string and if you copy it into the resource configuration, it'll get encoded again before it is compared to the current state, and it won't match.您无法从状态复制user_data值,因为它是一个编码字符串,如果您将其复制到资源配置中,它将在与当前状态进行比较之前再次进行编码,并且不会匹配。

But you can copy the current user data value from the Instance settings in the EC2 Console, and paste it into the user_data attribute of the resoure in the .tf file.但是您可以从 EC2 控制台中的实例设置复制当前用户数据值,并将其粘贴到 .tf 文件中资源的user_data属性中。

If there are multiple lines of data, you will need to replace each new-line with a \\n in the file, as you can't use a multi-line string.如果有多行数据,您将需要在文件中用\\n替换每个换行符,因为您不能使用多行字符串。

For example:例如:

resource "aws_instance" "instance1" {
  ami = "ami-0123456789abcdef0"
  instance_type = "t4.medium"
  user_data = "setting1=value\nsetting2=value\nsetting3=value"
}

Note: there is a 16KB size limit on the user data in EC2, but if you're copying from EC2 to your Terraform configuration, this won't be an issue.注意:EC2 中的用户数据有 16KB 大小限制,但如果您从 EC2 复制到您的 Terraform 配置,这将不是问题。 (But it will make it difficult to read and manage, so you may want to consider storing the config in a custom image instead.) (但这会使阅读和管理变得困难,因此您可能需要考虑将配置存储在自定义图像中。)

Or, as you suggested already, if your setup supports this and you can afford to shut the instance down temporarily, remove the user data from the instance settings altogether.或者,正如您已经建议的那样,如果您的设置支持这一点并且您有能力暂时关闭实例,请从实例设置中完全删除用户数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM