codeaprendiz.github.io

DevOps Essentials

View on GitHub

terraform COMMANDS

apply

apply

$ terraform apply out.terraform

console

console

$ terraform console

destroy

destroy

fmt

fmt

$ terraform fmt 25-instance.tf 
25-instance.tf

graph

graph


## import
[import](https://www.terraform.io/docs/commands/import.html)

- The terraform import command is used to import existing resources into Terraform.
- Support you local terraform state differs from remote state. In the remote state you have on VM added externally.
You want to import that into the existing state.
- This will sync the local terraform.tfstate file with that of remote. However, you will need to create the corresponding resource files (*.tf) by yourself.
> terraform import aws_instance.<name> <id-of-instance-from-console-aws>
```bash
terraform import aws_instance.example i-1246434554345

init

init

plan

plan

refresh

refresh

$ terraform refresh -var-file=../../terraform.tfvars
aws_key_pair.mykey: Refreshing state... [id=mykey]
aws_default_vpc.default: Refreshing state... [id=vpc-62320e18]
aws_default_subnet.default_az1: Refreshing state... [id=subnet-3857b819]
data.aws_ami.ubuntu: Refreshing state...
aws_security_group.sg_22: Refreshing state... [id=sg-0f148e80e2263d39d]
aws_instance.web: 

show

show

taint

taint

$ terraform taint aws_key_pair.mykey
Resource instance aws_key_pair.mykey has been marked as tainted.
terraform plan -var-file=../../terraform.tfvars
.
.
.
Terraform will perform the following actions:

  # aws_key_pair.mykey is tainted, so must be replaced
-/+ resource "aws_key_pair" "mykey" {
      ~ fingerprint = "24:f6:e5:e0:4e:e9:db:3e:93:f4:a0:ed:1c:0b:52:31" -> (known after apply)
      ~ id          = "mykey" -> (known after apply)
        key_name    = "mykey"
      ~ key_pair_id = "key-013a2ee21d0df0283" -> (known after apply)
        public_key  = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDLmviSRpF6tvGLpMExQZ94I9mqhVtINyjH1rEeDwSPB+1v00P608klF++uhRvREUyNID/i2w1cwjOi9H1isoZNoVaSKdiVIqEIp2HaIhTtKTo+NqVCdIeQXnwK2bS/OeIVScYn6YZjPAcWgMk0GH1q9zIn+sz1own3QxD4qysvoqN5bD9BxsDboLxKw803jJlcjRiSrdWbmEOoIXrEbv9t99ZxsYLIDIDZJskiAcvqKEywt+dRpvM/49c3pJ5JVluNNJypTObM2GI4piC1TcOL6xMgs+UsbDgEsHPYHXZnYENX/GGXYJcdDITCtMHap8O33EvOh5piIXHF6kQEgXDRCE61AEWnHQeGERJtTqlCiBAUUDNzAHt9UjZJVmioLJ+vLeW9M3KjUCh5MWxZ7hIJ7xvLxermNlRahzTlaGLhvkiyoaUyuh+qhpBIfCv7lioLaagv9jegc6AErMqDGFgPieJh479z+QqagPODwP0UB0jt07b7V/3csImEiy1aKY2wOQriXCkcAnSvKuAuiT2a2gOfPMJlzVd8hLrA2f5YuLWLm8p8rLXvkNSJ8YKNbQx1z5pII9R54YEOixjgp9bDNJL39ekrxlzLj72gpWg+AF+FFVNJ4gfZDjBSxUbkPhox7jhIV4DOCvc6W5uLkCXe62R9aNn1/Hkxcq99eoen1w== codeaprendiz@gmail.com"
      - tags        = {} -> null
    }

Plan: 1 to add, 0 to change, 1 to destroy.
$ terraform untaint aws_key_pair.mykey           
Resource instance aws_key_pair.mykey has been successfully untainted.