Back
Terraform can generate SSL/SSH private keys using the "tls_private_key resource".
So if you want to generate SSH keys on the fly you could do something like this:
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}"}data "aws_ami" "ubuntu" { most_recent = true filter { name = "name" values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"] } filter { name = "virtualization-type" values = ["hvm"] } owners = ["099720109477"] # Canonical}resource "aws_instance" "web" { ami = "${data.aws_ami.ubuntu.id}" instance_type = "t2.micro" key_name = "${aws_key_pair.generated_key.key_name}" tags { Name = "HelloWorld" }}
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}"
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"]
name = "virtualization-type"
values = ["hvm"]
owners = ["099720109477"] # Canonical
resource "aws_instance" "web" {
ami = "${data.aws_ami.ubuntu.id}"
instance_type = "t2.micro"
key_name = "${aws_key_pair.generated_key.key_name}"
tags {
Name = "HelloWorld"
Want to learn more about AWS, then do check out AWS Course offered by Intellipaat.
Learn how we helped 50,000+ professionals like you !
31k questions
32.8k answers
501 comments
693 users