Using data source to generate static data for AWS resources

Hi there,

I’m trying to create terraform templates for an existing environment. After a brief study of template configuration language I’m thinking I could use “data source” to retrieve configuration of all the existing resources and convert them to a permanent resource template file. I understand how to use a “data source” to retrieve data of an existing resource. However it is not clear if there is an option to convert them to a template tf file for future use instead of having to rely on “data source” all the time.

Is this something anyone has implemented in their terraform journey?

Thanks

I’m not sure if I understand exactly what you want to achieve.
Do you want to bring the existing resources under Terraform control and use it to manage them in the future?

In that case, you need to write resource definitions which match at least somewhat and then import the resources, do a plan, and adjust the definitions until they match.

To make things easier on myself, I only do a few resources at a time. That way, the plan output is shorter and easier to understand.

Not a trivial job, but itis doable.

What I’m trying to do is to import the template configuration for existing resources. Currently “terraform import” only imports the state but it doesn’t create the template though the documentation says it may be available in future. Therefore I’m thinking of using “data source” at least to get access to attributes of existing resources. However “data source” doesn’t appear to be giving access to all the attributes hence the approach of using “data source” as an alternative may not be technically feasible. Any suggestions would be highly appreciated.

Yes you have to create the resources also, but you don’t have to guess all the parameters. It’s really one big exercise in reverse engineering.

Once you have a resource defined with the required parameters, you can do an import. The parameters don’t have to have the correct value, they just need to be defined so the resource definition will be accepted by TF.

Next, you do terraform plan, do not apply. TF will now compare your resource definition with the imported state. The plan will show you what TF wants to change in order for the existing resource to match your definition.

So take this information and use it the other way around: change your resource definition to match the state, so that you can generate a new plan which should be empty. Once you have the definition matching so the plan is empty, move on to the next resource.

Hope this helps even if it is not the shortcut you were looking for.