Search code examples
openstackterraformterraform-provider-openstack

How to obtain public key from Openstack using Terraform and save it locally?


I'm trying to obtain a public key from Openstack (to later save it on local machine) using data source openstack_compute_keypair_v2 in Terraform:

data "openstack_compute_keypair_v2" "app_public_key" {
    name = "app-key"
}

, but when I run terraform apply nothing happens. (The key exists and the name is also correct.)

I assumed (maybe wrong) that I need to save them manually so I used local_file resource.

resource "local_file" "app_cert" {
    content = "${openstack_compute_keypair_v2.app_public_key.public_key}"
    filename = "${path.module}/app.cert"
}

But I am getting: Error: resource 'local_file.app_cert' config: unknown resource 'openstack_compute_keypair_v2.app_public_key' referenced in variable openstack_compute_keypair_v2.app_public_key.public_key

According to the documentation there is such attribute. What am I doing wrong?

Thanks for help!


Solution

  • So near. That second bit is just missing data. So it should be:

    resource "local_file" "app_cert" {
        content = "${data.openstack_compute_keypair_v2.app_public_key.public_key}"
        filename = "${path.module}/app.cert"
    }