2017-07-30 69 views
1

每當我添加key_name我的亞馬遜資源,我從來沒有真正連接到生成的實例:密鑰文件中Terraform

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}" 
} 

當我做

ssh -i ~/Downloads/po.pem [email protected]

我只是一個空白行在我的終端中,好像我輸入了一個錯誤的IP。但是,檢查亞馬遜控制檯,我可以看到該實例正在運行。我在Terraform上也沒有收到任何錯誤。

回答

2

默認情況下,所有網絡訪問都是不允許的。您需要通過設置安全組來明確允許網絡訪問。

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}" 
}