title: Use of S3 in Terraform aws_instance.connection.private_key and aws_key_pair.public_key
body: <p>I am trying to use <strong>terraform cloud</strong> and don't want to commit SSH key inside the repository. My current configuration looks like this:</p>
  
  <pre><code>
  resource "aws_key_pair" "project" {
    key_name   = "project"
    public_key = file(".ssh/id_rsa.pub")
  }
  
  resource "aws_instance" "example" {
    ami           = "ami-08ee2516c7709ea48"
    instance_type = "t2.micro"
    security_groups = [
      aws_security_group.ssh_allow.name,
      aws_security_group.http_allow.name
    ]
    key_name = aws_key_pair.project.key_name
  
    connection {
      type        = "ssh"
      user        = "centos"
      private_key = file(".ssh/id_rsa")
      host        = self.public_ip
    }
  
    provisioner "remote-exec" {
      inline = [
        "sudo yum -y install nginx",
        "sudo systemctl start nginx"
      ]
    }
  
    lifecycle {
      create_before_destroy = true
    }
  
  </code></pre>
  
  <p>Is it possible to reference the file from remote storage, for example S3?</p>
  
  <p>something like this:</p>
  
  <pre><code>resource "aws_key_pair" "project" {
    key_name   = "project"
    public_key = s3_file("bucket/ssh/id_rsa.pub")
  }
  
  </code></pre>
  
  <h1>Is it at all possible?</h1>
  
  <p>I have tried to use <strong>data</strong> providers</p>
  
  <pre><code>data "aws_s3_bucket_object" "public_key" {
    bucket = "com.project.infrastructure"
    key    = ".ssh/project.pub"
  }
  
  data "aws_s3_bucket_object" "private_key" {
    bucket = "com.project.infrastructure"
    key    = ".ssh/project"
  }
  
  resource "aws_key_pair" "project" {
    key_name   = "project"
    public_key = data.aws_s3_bucket_object.public_key.body
  }
  </code></pre>
  
  <p>but getting this error:</p>
  
  <pre class="lang-sh prettyprint-override"><code>Error: "public_key": required field is not set
  
    on example.tf line 28, in resource "aws_key_pair" "project":
    28: resource "aws_key_pair" "project" {
  
  
  </code></pre>
  
  <p>AWS security credentials are correct and I can download file from the bucket with</p>
  
  <pre class="lang-sh prettyprint-override"><code>aws s3api get-object --bucket com.project.infrastructure --key .ssh/project private_key
  
  </code></pre>
  
  <h1>Experiment with null_resource also failed</h1>
  
  <pre><code>resource "null_resource" "download_key" {
    provisioner "local-exec" {
      when    = create
      command = "aws s3api get-object --bucket com.project.infrastructure --key .ssh/project.pub project.pub"
    }
    provisioner "local-exec" {
      when    = create
      command = "aws s3api get-object --bucket com.project.infrastructure --key .ssh/project project"
    }
  }
  
  resource "aws_key_pair" "project" {
    key_name   = "project"
    depends_on = [null_resource.download_key]
    public_key = file("project.pub")
  }
  
  resource "aws_instance" "example" {
    ami           = "ami-08ee2516c7709ea48"
    instance_type = "t2.micro"
    security_groups = [
      aws_security_group.ssh_allow.name,
      aws_security_group.http_allow.name
    ]
    key_name = aws_key_pair.project.key_name
  
    connection {
      type        = "ssh"
      user        = "centos"
      private_key = file("project")
      host        = self.public_ip
    }
  
  
    provisioner "local-exec" {
      command = "echo ${aws_instance.example.public_ip} &gt; ip_address.txt"
    }
  
    provisioner "remote-exec" {
      inline = [
        "sudo yum -y install nginx",
        "sudo systemctl start nginx"
      ]
    }
  
    depends_on = [aws_s3_bucket.saritasa_bucket, aws_key_pair.project]
  
    lifecycle {
      create_before_destroy = true
    }
  
    tags = module.project_config.tags
  
  }
  
  </code></pre>
  
  <p><strong>then I get the following error</strong>:</p>
  
  <pre><code>➜ terraform plan    
  
  Error: Error in function call
  
    on example.tf line 42, in resource "aws_key_pair" "project":
    42:   public_key = file("proect.pub")
  
  Call to function "file" failed: no file exists at project.pub.
  
  </code></pre>