mellifluous
mellifluous

Reputation: 2975

How do I use local_file resource to save pem file to disk which was created by tls_private_key resource in Terraform?

I want to save the private_key_pem generated by the tls_private_key resource to a file on disk locally using local_file resource.

resource "tls_private_key" "example" {
  algorithm = "RSA"
  rsa_bits  = 4096
}

resource "aws_key_pair" "generated_key" {
  key_name   = "cloudtls"
  public_key = tls_private_key.example.public_key_openssh
}

resource "aws_instance" "automation" {
  instance_type        = var.instance_type
  ami                  = var.image_id
  iam_instance_profile = aws_iam_instance_profile.ec2_profile.name
  key_name = aws_key_pair.generated_key.key_name
}

resource "local_file" "pem_file" {
  filename = "pemfile.pem"
  #
}

Upvotes: 3

Views: 3407

Answers (1)

mellifluous
mellifluous

Reputation: 2975

The below code saves the private key (.pem file) to the specified path.

resource "local_file" "cloud_pem" { 
  filename = "${path.module}/cloudtls.pem"
  content = tls_private_key.example.private_key_pem
}

Upvotes: 7

Related Questions