Skip to content

Instantly share code, notes, and snippets.

@iamtheindian
Created June 16, 2020 09:29
Show Gist options
  • Save iamtheindian/bd06120badd8d971460fa04f966e054c to your computer and use it in GitHub Desktop.
Save iamtheindian/bd06120badd8d971460fa04f966e054c to your computer and use it in GitHub Desktop.
#generate private key
resource "tls_private_key" "tkey" {
algorithm = "RSA"
}
#assigne public openssh to the aws key pair
resource "aws_key_pair" "deployer" {
key_name = "deployer-key"
public_key = tls_private_key.tkey.public_key_openssh
depends_on=[tls_private_key.tkey]
}
#use key_name variable in the field of aws_instance's key_name
#if you want to save this key then use this
resource "local_file" "lf" {
content = tls_private_key.tkey.private_key_pem
filename= "awskey.pem"
}
#now this will save in you local system
#if you want to remote-exec the just use this << tls_private_key.tkey.private_key_pem >> in private_key argument of connection
#or you can use variables
variable "aws_key_pair_name" {
default = aws_key_pair.deployer.key_name
}
variable "login_private_key" {
defautl = tls_private_key.tkey.private_key_pem
}
#now use this variables in your terraform file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment