In the previous example, we created an EC2 instance, which we wouldn’t be able to access, that is because we neither provisioned a new key pair nor used existing one, which we could see from the state report:
➜ terraform_demo grep key_name terraform.tfstate
"key_name": "",
➜ terraform_demo
As you can see key_name is empty.
Now, if you already have a key pair which you are using to connect to your instance, which you will find
in EC2 Dashboard, NETWORK & SECURITY – Key Pairs:

then we can specify it in aws_instance section so EC2 can be accessed with that key:
resource "aws_instance" "ubuntu_zesty" {
ami = "ami-6b7f610f"
instance_type = "t2.micro"
key_name = "myec2key"
}
Let’s create an instance:
Comments closed


