Terraform SSH connection to AWS EC2

The instances are created via Terraform, but Terraform can only used existing key pairs. First thing is to create the key pair as explained in the AWS documentation1.

NOTE: However, if you are not familiar with Terraform, please go through the following blogs before this blog

  1. Basic example of creating AWS EC2 with Terraform
  2. Creating AWS S3 bucket with Terraform

Connect to the EC2 instance, the most important thing is key value pair which is create in the time of the EC2 instance is created. But this is not currently possible in the Terraform. Therefore we need to create the mykey first.

For example

aws ec2 create-key-pair --key-name mykey --query 'KeyMaterial' --output text > ~/.aws/mykey.pem

If you are connecting from the Linux or Mac, because need read permission.

chmod 400  ~/.aws/mykey.pem

To display

aws ec2 describe-key-pairs --key-name mykey

To retrieve the public key from the pem file (Optional)

ssh-keygen -y -f ~/.aws/mykey.pem

Before the access via SSH you have to enable the SSH in the default security group of you VPC where you created the instance:

Let's create a example Ansible host as follows, in the resource.tf

resource "aws_instance" "ansible-host" {
  ami = "${lookup(var.amis, "ubuntu-server")}"
  instance_type = "t2.micro"
  tags = {
      "Name"="${var.instance_names[0]}"
      "training"="${var.training_name}"
  }
  key_name="mykey"

 
  provisioner "remote-exec" {
    inline = [
        "sudo apt-add-repository ppa:ansible/ansible -y",
        "sudo apt-get update",
        "sudo apt-get install ansible -y"
    ]

    connection {
        type = "ssh"
        user = "ubuntu"
        private_key = "${file("/Users/ojitha/.aws/mykey.pem")}"
    }
  }
}

As shown in the above line# 11, we can create and run the ansible installation. For that you need a ssh connection with the private key line# 18.

Here is the providers.tf file

provider "aws" {
  region = "${var.my_region}"
}

terraform {
    required_version =  ">= 0.11.13"
    backend "s3" {
        bucket = "ojitha"
        key = "test/ansible_training"
        region = "ap-southeast-2"
        encrypt = "true"
    }
}

Here the variables.tf file which output the public dns ip to connect to the ansible host via SSH.

variable "instance_names" {
  type = "list"
  description = "tags for the instances 0,1,2"
}

variable "amis" {
  type = "map"
  description = "all the instances are from this ami"
}

variable "my_region" {
  type = "string"
  description = "always my region is this"
}

variable "training_name" {
    type="string"
}

output "ansible_public_ip" {
  value = "${aws_instance.ansible-host.public_dns}"
}

The terraform.tfvars file is here

amis = {
    "ubuntu-server" = "ami-001dae151248753a2"
}

my_region = "ap-southeast-2"

instance_names = ["ansible-host"]

training_name = "..."

You can find the public dns ip as output when you run the terraform apply. Now use the following command to connect to the ansible host via SSH node.

ssh -i ~/.aws/mykey.pem ubuntu@<public dns ip of the ansible-host>

Comments

Popular posts from this blog

How To: GitHub projects in Spring Tool Suite

Spring 3 Part 7: Spring with Databases

Parse the namespace based XML using Python