title: I can not ssh to newly created ec2 instance within terraform
body: <p>I can not ssh to my newly created ec2 instance using terraform</p>
  <p>Keep getting permission denied (publickey)</p>
  <p>I generated the key using ssh-keygen -t rsa command</p>
  <p>Please help me get this through this error</p>
  <pre><code>ssh -i ~/.ssh/ubuntu/mykey ubuntu@44.228.131.143
  </code></pre>
  <p>Root/main.tf , here I defined it then passed t the variable.tf</p>
  <pre><code>module &quot;compute&quot; {
    source          = &quot;./compute&quot;
    instance_count  = 1
    instance_type   = &quot;t3.micro&quot;
    public_sg       = module.networking.public_sg
    public_subnets  = module.networking.public_subnets
    vol_size        = 10
    public_key_path = &quot;/home/ubuntu/.ssh/mykey.pub&quot;
    key_name        = &quot;mykey&quot;
    user_data_path = &quot;${path.root}/userdata.tpl&quot;
    db_endpoint     = module.database.db_endpoint
    dbuser          = var.dbuser
    dbpassword          = var.dbpassword
    dbname          = var.dbname
  
  }
  </code></pre>
  <p>Compute main.tf , referrenced to my instance</p>
  <pre><code>resource &quot;aws_key_pair&quot; &quot;my_key&quot; {
    key_name   = var.key_name
    public_key = file(var.public_key_path)
  }
  
  resource &quot;aws_instance&quot; &quot;my_instance&quot; {
    count         = var.instance_count
    instance_type = var.instance_type
    ami           = data.aws_ami.server_ami.id
  
    tags = {
      Name = &quot;my-node ${random_id.random[count.index].dec}&quot;
    }
  
    key_name               = aws_key_pair.my_key.id
    vpc_security_group_ids = [var.public_sg]
    subnet_id              = var.public_subnets[count.index]
    user_data = templatefile(var.user_data_path,
      {
        nodename    = &quot;my-node ${random_id.random[count.index].dec}&quot;
        db_endpoint = var.db_endpoint
        dbuser      = var.dbuser
        dbpass      = var.dbpassword
        dbname      = var.dbname
      }
    )
  
    root_block_device {
      volume_size = var.vol_size
    }
  }
  </code></pre>
  <p>Variable.tf which passed over the root/main.tf</p>
  <pre><code>variable &quot;instance_count&quot; {}
  variable &quot;instance_type&quot; {}
  variable &quot;vol_size&quot; {}
  variable &quot;public_sg&quot; {}
  variable &quot;public_subnets&quot; {}
  variable &quot;public_key_path&quot; {}
  variable &quot;key_name&quot; {}
  variable &quot;db_endpoint&quot; {}
  variable &quot;dbname&quot; {}
  variable &quot;dbpassword&quot; {}
  variable &quot;dbuser&quot; {}
  variable &quot;user_data_path&quot; {}
  </code></pre>