db2791
db2791

Reputation: 1110

Key Files in Terraform

Whenever I add key_name to my amazon resource, I can never actually connect to the resulting instance:

provider "aws" {
    "region" = "us-east-1"
    "access_key" = "**"
    "secret_key" = "****"
}

resource "aws_instance" "api_server" {
    ami = "ami-013f1e6b"
    instance_type = "t2.micro"
    "key_name" = "po"

    tags {
        Name = "API_Server"
    }

}

output "API IP" {
    value = "${aws_instance.api_server.public_ip}"
}

When I do

ssh -i ~/Downloads/po.pem bitnami@IP

I just a blank line in my terminal, as if I was putting in a wrong IP. However, checking the Amazon console, I can see the instance is running. I'm not getting any errors on my Terraform either.

Upvotes: 1

Views: 55

Answers (1)

minamijoyo
minamijoyo

Reputation: 3445

By default all network access is not allowed. You need to explicitly allow network access by setting a security group.

provider "aws" {
    "region" = "us-east-1"
    "access_key" = "**"
    "secret_key" = "****"
}

resource "aws_instance" "api_server" {
    ami = "ami-013f1e6b"
    instance_type = "t2.micro"
    key_name = "po"
    security_groups = ["${aws_security_group.api_server.id}"]

    tags {
        Name = "API_Server"
    }

}

resource "aws_security_group" "api_server" {
  name        = "api_server"

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["XXX.XXX.XXX.XXX/32"] // Allow SSH from your global IP
  }

  egress {
    from_port       = 0
    to_port         = 0
    protocol        = "-1"
    cidr_blocks     = ["0.0.0.0/0"]
  }
}


output "API IP" {
    value = "${aws_instance.api_server.public_ip}"
}

Upvotes: 2

Related Questions