Tuesday, December 13, 2016

My Guide to terraform - Part 3

By now we know what is Terraform, why it is there and how to create a resource with it. In this post, I'm going to modify resource which I created and lastly destroy the infrastructure we create.

Updating an infrastructure
Let's modify the instance type. Previously we create an instance with t2.micro size so now I need something bigger than that, so I modify my previous script.


resource "aws_instance" "example" {
  ami           = "ami-13be557e"
  instance_type = "t2.medium"
}

First thing first, before do any thing to your infrastructure, execute a dry run.

It clearly specifies there will be an update in resources ,( -/+ ) symbol stands that.

instance_type: "t2.micro" => "t2.medium" (forces new resource)

This is what we expected, whenever an update happens on ec2 instance type AWS destroy existing ec2 and gives us a new instance. Let me apply it and check the status.


As we expected our infrastructure is changed and new resource with t2.medium is available.


Destroying an infrastructure.

Before moving to destroy I' wanted to do an experiment. Usually, If you need to modify or delete an AWS resource you need an ID but in our previous example we didn't specify any identification, so where they come from. Definitely, it should be retrieved from the status file. So I'm going to delete the status file and apply another instance size change.


Then I execute terraform show
It gives me there is no status.
Then I execute terraform apply
This time it gives me shine new instance instead of updating existing instance.

So keep in mind, never ever delete the status file, that why terraform also keeping a backup file whenever a change happens.So the best place to keep this is your repository. Make sure to commit this status file along with other source code.



Removing an infrastructure also soo easy with terraform. Same steps as before, first we change the code, go for a dry run then apply the changes.
This time dry run is little bit different
terraform plan --destroy
to actual deletion
terraform destroy

No comments:

Post a Comment