Call certificate resources inside cloudinit resource by using variables


I’m generating some certificates using tls_locally_signed_cert. So these are, let’s say, tls_locally_signed_cert.server1, .server2, .server3 etc.

I want to inject these certificate for all these servers through cloudinit inside template_cloudinit_config using a templatefile.
What I tried inside the templatefile block was:

server_certificates = tls_locally_signed_cert.${each.key}.cert_pem

But I realised afterwards that this cannot be done, because you can’t call resources using variables, and I get “This character is not used within the language.”

Is there any trick that I can use in this case?

Take a look at using a for_each for the tls_locally_signed_cert resources instead of three separate blocks. You can then use the * wildcard to reference them all together.

Thank you. I knew there was something basic I was missing in the logic, but I kept focussing on the second part. It works as expected.