Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (5.6k points)
I need to spin up few EC2 boxes for different users, each user should be sandboxed from all the others so each EC2 box needs its own SSH key.

What is the best way to accomplish these tasks in Terraform?

1 Answer

0 votes
by (12.4k points)

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"

  }

}

Want to learn more about AWS, then do check out AWS Course offered by Intellipaat.

Related questions

0 votes
1 answer
asked Dec 8, 2020 in AWS by devin (5.6k points)

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer

Browse Categories

...