Search code examples
amazon-web-servicesterraformterraform-provider-aws

How to partially manage a resource in terraform


CloudFormation import can be made to partially manage a resource (since CloudFormation has no natural drift detection). This allows us to do something like this:

  1. CloudFormation Template 1 creates a vpc-1, route-tables-1 etc. So it sort of owns it.
  2. Each person in the team has their own vpc, route tables etc. and they manage it via their personal CloudFormation templates. Say these are templates A, B, C ...
  3. When they want to peer their vpc with vpc-1 and modify route-tables-1 to allow connection between the vpcs they simply update their template to import vpc-1 and do the necessary modifications in their own template. So you might have connections and routing between vpc-1 <-> vpc-A set up by developer A using their CloudFormation template-A. Similarly vpc-1 <-> vpcB is created by developer B and so on.
  4. The developers can destroy their respective stacks. All that will do is remove the peering connection and modify the route tables in vpc-1 to remove just that bit which routed traffic from vpc-1 to their vpc. Other things remain untouched. E.g if dev-A destroys their stack, it's not going to affect vpc-1 <-> vpc-B bits which pertains to dev-B

I'm having to deal with terraform now to create yet another vpc, say vpc-z. As I understand terraform always tries to sync the state entirely according to its world-view - i.e there is automatic drift-detection + correction on running terraform apply/destroy. So if I create a vpc-z using terraform and want to peer with vpc-1 above (and modify its route tables to allow traffic), I think I'll need to import the vpc and route-table resources. However once I do that, terraform will always ensure that that state at the time of import is maintained. Now if developer-A tears down their stack and removes the routing configuration from vpc-1 <-> vpc-A, the next time I apply terraform, will it detect that some routes have changed in vpc-1 (since the time it was imported here) and will it try to fix them back (and likely fail)?

In short can I ask terraform to reference an existing resource, modify certain aspect of it and care only about those aspects during future apply/destroy and not other things which might have been changed by someone else (either manually or through a different CloudFormation/Terraform template etc)?


Solution

  • "I think I'll need to import the vpc and route-table resources"

    I believe that assumption is incorrect. You can use a data source for both the VPC, and the route-table resources, and just have Terraform manage new route resources. Data sources allow you to have Terraform look up existing resources without managing them, for the purposes of referencing them in other resources that Terraform does manage.