Skip to content

Instantly share code, notes, and snippets.

@devops-school
Last active March 24, 2024 14:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save devops-school/bb921213ae6fe3d702e049d56f102c0b to your computer and use it in GitHub Desktop.
Save devops-school/bb921213ae6fe3d702e049d56f102c0b to your computer and use it in GitHub Desktop.
### 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