53 votes
Accepted

Terraform: apply only one tf file

Not really. The standard way to work around this though is to use eg: terraform apply -target=aws_security_group.my_sg but that's only going to apply one security group at a time, so will get ...
user avatar
  • 1,055
32 votes

How to use terraform import with module

The terraform import command uses the "Resource Address" syntax, which is a way to talk about objects in a configuration from outside of that configuration. (This is as opposed to references ...
user avatar
29 votes
Accepted

How can I manage secrets in .tf and .tfstate?

Terraform supports adding an additional file with variables during invocation. documentation: https://www.terraform.io/intro/getting-started/variables.html#from-a-file We are using that feature to ...
user avatar
24 votes
Accepted

How to use cloud-init with Terraform?

Cloud-init files are essentially bootstrap codes, that run before each startup, and can - among others - modify files, set up services, create users, etc. Not all types of droplets support all ...
user avatar
  • 1,557
24 votes
Accepted

How to test a Terraform Configuration?

There is currently no full solution to this integrated into Terraform, but there are some building blocks that could be useful to assist in writing tests in a separate programming language. Terraform ...
user avatar
18 votes
Accepted

How to support releasing new versions of the code, running in parallel with the last stable release?

There are a few different ways to achieve goals of this sort, each with some different tradeoffs. I'm going to describe the most common ones below. The simplest approach is to use Terraform's ...
user avatar
15 votes

How to test a Terraform Configuration?

We recently open sourced Terratest, our swiss army knife for testing infrastructure code. Today, you're probably testing all your infrastructure code manually by deploying, validating, and ...
user avatar
13 votes

How to test a Terraform Configuration?

As an update to this question, there is now Kitchen-Terraform which allows the testing of Terraform Configuration files without breaking production environments. The repository also includes a few ...
user avatar
  • 2,993
10 votes
Accepted

How can I manage changes in desired capacity due to scaling policies in AutoScaling Group state?

The desired_capacity in Terraform is marked in the documentation as optional. So with a proper min_size value, Terraform can wait until the minimum capacity is reached before continuing. The above, ...
user avatar
10 votes

How can I manage secrets in .tf and .tfstate?

If you're on AWS, then have a look at "The Right Way to Manage Secrets" by Segment.io on the AWS Blog. We advocate using chamber to all of our customers for managing secrets. It works by leveraging ...
user avatar
10 votes
Accepted

How do I detach an existing volume and attach a new volume with Terraform?

The old volume is never detached because skip_destroy = true. From the terraform docs: skip_destroy - (Optional, Boolean) Set this to true if you do not wish to detach the volume from the instance ...
user avatar
10 votes

Terraform: apply only one tf file

If you organize your code by modules, you could apply terraform only on a module, eg : # securitygroup.tf module "securitygroup" { source = "git@github.com:user/securitygroup-terraform-module.git?...
user avatar
  • 215
9 votes
Accepted

How can I keep track of my Cloud Resource use?

A judicious usage of one of the cloud "orchestration" tools, such as Terraform or possibly Fugue seems to be the best way. You can start small, pick a less important and not very extensive ...
user avatar
8 votes

How can I manage secrets in .tf and .tfstate?

We avoid terraform handle our secrets. Even if you manage to inject secrets by a var file "secrtes.tfvars" as pointed out above, these secrets will be stored in your terraform (remote-)state. You can ...
user avatar
  • 141
8 votes
Accepted

Terraform: Is there a concise syntax for specifying multiple tags for a resource?

For an auto-scaling-group this is the most concise syntax available. For most other resources you use the tags syntax which looks like: tags { Key1 = "value1" Key2 = "value2" }
user avatar
  • 1,082
8 votes

Does terraform support blue green deployments and roll backs out of the box

It all depends on how you are deploying your code. (First, IMHO, I don't think that Terraform is the right hammer for this operation. Terraform responsibility is to build the necessary infrastructure ...
user avatar
  • 181
8 votes
Accepted

Get private key from terraform state?

If you need to use a value like this private key for later steps, the common answer is to return it as a root module output value. Since the private key is sensitive, you can mark it as such to ...
user avatar
7 votes

How to test a Terraform Configuration?

In addition to all the other options mentioned, I would like to mention that InSpec 2.0 added support for cloud provider APIs. Basically, you can continue writing the IaC with Terraform, then write ...
user avatar
6 votes

How to use cloud-init with Terraform?

When you create an Auto Scaling group with Terraform, you can specify the user_data to be used by instances created by this ASG. Documented here - https://www.terraform.io/docs/providers/aws/r/...
user avatar
6 votes

Baking Immutable Images

An idea key to your question is that of immutable infrastructure, which is the idea that an image is built once, deployed many times, and never changed at runtime. If the contents of the image need to ...
user avatar
6 votes
Accepted

How to apply using Terraform to launch multiple EC2 Resource with different configs (VPC not maintained by TF)

You can use a remote backend as a data source. That is working well for us thus far but this project is not very mature and we will likely refactor a time or ten. data "terraform_remote_state" "...
user avatar
6 votes
Accepted

How should we automatically rebuild immutable infrastructure when new packages are available?

Part of adopting the Immutable Infrastructure Pattern is decomposing your system into small manageable pieces that can move through CI/CD Pipeline very quickly, this means that OS patches can be done ...
user avatar
6 votes

How can I get terraforms extern to execute `ssh-keygen -y -f ~/.ssh/id_rsa`?

So what happens in first case is, as Dan's already said, there's no shell used and as such nothing to expand the ~. Quoting the documentation about program: Terraform does not execute the program ...
user avatar
  • 11.1k
6 votes
Accepted

Terraform | The "count" object can be used only in "resource" and "data" blocks,

The resource "azurerm_network_interface_backend_address_pool_association" "network_interface" block doesn't have the count argument set, so count.index is not meaningful in that block. Given the ...
user avatar
6 votes

How to use terraform import with module

Run terraform plan first and use the names in the output. Goal here is to import in the resources that are listed in the plan output so next time you run plan they won't be listed for add. So ...
user avatar
6 votes

Is there a way to lint Terraform?

terraform fmt The terraform fmt command is used to rewrite Terraform configuration files to a canonical format and style.
user avatar
6 votes

Terraform apply output only

Running terraform refresh should take care of any new outputs. It will read the latest data from each resource and then update all of the outputs in terms of those updates, which includes re-...
user avatar
5 votes

How to reconcile Infrastructure as Code and not storing env config in code?

There are, as always, a few ways to solve this. You can use a central source to keep secrets that each server reads from ala Hashicorp Vault. While popular this is not my preferred approach as its ...
user avatar
  • 735
5 votes

How to test a Terraform Configuration?

On Aws-Side there is https://github.com/k1LoW/awspec - it should be possible, to feed in terraform.state and test, wheter terraform applied correct. But I think, beyond testing on low level the tool, ...
user avatar
  • 141
5 votes

Terraform: Is there a concise syntax for specifying multiple tags for a resource?

Terraform v0.12 added support for Dynamic Nested Blocks. The following example is derived from their blog post about the new features (see section Dynamic Nested Blocks): locals { standard_tags = { ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible