使用terraform import时遇到的问题
总结
只是想进行Terraform 导入操作,但却遇到了困难,所以我把这当作备忘录记录下来。
错误①:在配置中不存在资源地址XX。
说没有资源被指责为错误。
$ terraform import module.network.aws_security_group.XXX sg-XXX
Error: resource address "module.network.aws_security_group.XXX" does not exist in the configuration.
Before importing this resource, please create its configuration in module.network. For example:
resource "aws_security_group" "XXX" {
# (resource arguments)
}
错误②:地址无效
被批评说地址不正确
$ terraform import module.network.module.aws_security_group.XXX XXX
Error: Invalid address
on <import-address> line 1:
1: module.network.module.aws_security_group.XXX
Resource specification must include a resource type and name.
错误③ 导入命令需要两个参数。
如果导入命令的定义方式不同,就会被责备。
$ terraform import aws --regions=ap-northeast-1 --resources=security-group --filter=aws_security_group=sg-XXX
The import command expects two arguments.
Usage: terraform import [options] ADDR ID
Import existing infrastructure into your Terraform state.
This will find and import the specified resource into your Terraform
state, allowing existing infrastructure to come under Terraform
management without having to be initially created by Terraform.
The ADDR specified is the address to import the resource to. Please
see the documentation online for resource addresses. The ID is a
resource-specific ID to identify that resource being imported. Please
reference the documentation for the resource type you're importing to
determine the ID syntax to use. It typically matches directly to the ID
that the provider uses.
The current implementation of Terraform import can only import resources
into the state. It does not generate configuration. A future version of
Terraform will also generate configuration.
Because of this, prior to running terraform import it is necessary to write
a resource configuration block for the resource manually, to which the
imported object will be attached.
This command will not modify your infrastructure, but it will make
network requests to inspect parts of your infrastructure relevant to
the resource being imported.
Options:
-backup=path Path to backup the existing state file before
modifying. Defaults to the "-state-out" path with
".backup" extension. Set to "-" to disable backup.
-config=path Path to a directory of Terraform configuration files
to use to configure the provider. Defaults to pwd.
If no config files are present, they must be provided
via the input prompts or env vars.
-allow-missing-config Allow import when no resource configuration block exists.
-input=true Ask for input for variables if not directly set.
-lock=true Lock the state file when locking is supported.
-lock-timeout=0s Duration to retry a state lock.
-no-color If specified, output won't contain any color.
-state=PATH Path to the source state file. Defaults to the configured
backend, or "terraform.tfstate"
-state-out=PATH Path to the destination state file to write to. If this
isn't specified, the source state file will be used. This
can be a new or existing path.
-var 'foo=bar' Set a variable in the Terraform configuration. This
flag can be set multiple times. This is only useful
with the "-config" flag.
-var-file=foo Set variables in the Terraform configuration from
a file. If "terraform.tfvars" or any ".auto.tfvars"
files are present, they will be automatically loaded.
-ignore-remote-version Continue even if remote and local Terraform versions
differ. This may result in an unusable workspace, and
should be used with extreme caution.
解决方案
$ terraform import module.ディレクトリ.ファイル名.ファイル内で定義している名前 セキュリティグループID
module.network.aws_security_group.XXX: Importing from ID "XXX"...
module.network.aws_security_group.XXX: Import prepared!
Prepared aws_security_group for import
module.network.aws_security_group.XXX: Refreshing state... [id=sg-XXX]
Import successful!
The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.
文獻參考