Skip to content

Instantly share code, notes, and snippets.

@xandout
Last active November 22, 2023 21:12
Show Gist options
  • Save xandout/c4070c55a618029dbc72622cafcaaa53 to your computer and use it in GitHub Desktop.
Save xandout/c4070c55a618029dbc72622cafcaaa53 to your computer and use it in GitHub Desktop.
Terraform Stateless

Create new resources each time terraform runs

This configuration will allow you to deploy resources many times in a row with no "stateful" behavior.

terraform apply
resource "null_resource" "remove_state" {
provisioner "local-exec" {
command = "rm -rf *.tfstate*"
}
}
resource "aws_instance" "test" {
depends_on = ["null_resource.remove_state"]
ami = "ami-a4c7edb2"
instance_type = "t2.micro"
subnet_id = "subnet-abc123"
vpc_security_group_ids = ["sg-abc123"]
disable_api_termination = true
root_block_device {
delete_on_termination = true
volume_size = 100
volume_type = "gp2"
}
key_name = "my_priv_key"
tags {
Name = "dev-terraform"
}
}
@zaitsxl
Copy link

zaitsxl commented Jan 24, 2020

Okay and how would you keep track on your infra with this "create and forget" behavior? Isn't it better to use say ansible which acts like this by default?

@Jayonics
Copy link

Okay and how would you keep track on your infra with this "create and forget" behavior? Isn't it better to use say ansible which acts like this by default?

I know your comment is almost 4 years old at this point but I wanted to point out that I 100% agree. But...

The whole reason I looked this up in the first place is because I wanted to template some infrastructure in Azure DevOps; pipelines, permissions, service connections, etc. But there's no Ansible module specifically for azure devops, there's support for using azure devops build agents as an ansible controller but not what I'm looking for...

On the other hand Terraform has a pretty good (not fully featured) azure devops provider, and so yeah... while ansible would be a much better tool for the job, there's no modules for that. Mixing template instances in entirely new environments with the same state is not desirable so... what you gonna do

I'm sure there's probably a better way of doing what he did here than deleting the state file every run though...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment