Home > database >  How to prevent the instance getting deleted when creating new instance in terraform in a single code
How to prevent the instance getting deleted when creating new instance in terraform in a single code

Time:01-21

I have tried creating an instance using terraform by getting values either windows ami or linux ami using parameters in jenkins pipeline.

The loophole is :-

When I choose windows instance ,it creates instance in AWS, next time I choose linux ,windows gets deleted and newly Linux is created.

Expected Output

The old instance should not get deleted when creating the new one

I have tried using the following codes:-

provider "aws"{
region="us-east-1"
}
resource "aws_instance" "my-instance" {
  ami           = lookup(var.ami,var.name)
  instance_type = "t2.micro"
  count=1
  key_name      = "nits"

  tags = {
    Name  = var.name
  }
}
variable "ami" {
  default = {
    "Linux" = "ami-08e4e35cccc6189f4"
    "Windows" = "ami-0d43d465e2051057f"
  }
}
variable "name" {
  default ="Linux"
}
pipeline{
 agent any
     tools{
         terraform 'terra'
     }
 parameters{
   choice(name:'ami', choices: ['Linux','Windows'])
   choice(name:'Actions', choices:['apply','destroy'])
}
stages{
    stage('Git checkout'){
     steps{
        //
       }
    }
    stage('Terraform Init'){
        steps{
            sh label: '', script:'terraform init'
        }
    }
    stage('Terraform apply'){
        steps{
            sh label:'',script:'terraform ${Actions} -var name="${ami}" --auto-approve'
        }
    }
  }
}

If I can try with modules, kindly suggest me the ways.

CodePudding user response:

The Terraform language is declarative, meaning what you described using your terraform configuration is the intended goal rather than the steps to reach that goal. No matter how many times you run the pipeline terraform will make sure that only one resource get provisioned (because in your configuration you have only placed one aws_instance) and then maintain the infrastructure information in terraform.tf state file

For your expected output there are quite a few ways:

  1. Add a stage in the pipeline before the terraform commands which will add a example.tf file with the content :

     resource "aws_instance" "my-other-instance" {
        /......
     }
    

Make sure to add example.tf in the same directory as the previous tf files. So every time you run the pipeline a new instance will be created without others getting deleted. But here you need to adjust the variables so that every new instance gets different ami and name.

  1. Add a stage in the pipeline before the terraform commands which deletes the terraform.tf state file so that terrform forgets the infrastructure it built the previous time and then init and apply.

CodePudding user response:

That's not how Terraform works. You are trying to use Terraform as a scripting language, but it is not a scripting language. Terraform templates are not scripts, they are a declarative template to specify what infrastructure you want to exist, which you then pass to Terraform, which does what it needs to do in order to make that infrastructure exist. If you run it again with changes, then you are telling it: "I want you to change what currently exists, to match this new template".

If you want both resources to exist, then you have to declare them both separately in Terraform.

If you want to build some sort of interactive pipeline that creates EC2 instances on demand, then you may want to build that with a scripting language like Python Boto3, instead of Terraform.

  •  Tags:  
  • Related