-
-
Save mkol5222/7632063d93b1ffec98a4d63d78bd47d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Option 1 - Terraform can generate SSL/SSH private keys using the tls_private_key resource | |
variable "key_name" {} | |
resource "tls_private_key" "example" { | |
algorithm = "RSA" | |
rsa_bits = 4096 | |
} | |
resource "aws_key_pair" "generated_key" { | |
key_name = "var.key_name" | |
public_key = "${tls_private_key.example.public_key_openssh}" | |
} | |
resource "aws_instance" "web" { | |
ami = "ami-5b673c34" | |
instance_type = "t2.micro" | |
key_name = "${aws_key_pair.generated_key.key_name}" | |
tags = { | |
Name = "HelloWorld" | |
} | |
} | |
### Option 2 - Retrieving the Public Key for Your Key Pair on Linux and use with aws_key_pair using | |
chmod 400 my-key-pair.pem | |
ssh-keygen -y -f /path_to_key_pair/my-key-pair.pem >> terraform_ec2_key.pub | |
provider "aws" { | |
region = "ap-south-1" | |
access_key = "" | |
secret_key = "" | |
} | |
resource "aws_key_pair" "terraform-demo" { | |
key_name = "terraform-demo" | |
public_key = "file(terraform_ec2_key.pub)" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment