【问题标题】:How to run an apache server in custom vpc(non default)aws through terraform?如何通过 terraform 在自定义 vpc(非默认)aws 中运行 apache 服务器?
【发布时间】:2017-05-17 20:13:12
【问题描述】:

我通过 terraform 在 aws 中创建了一个自定义 vpc,其中包含 1 个公共子网和 2 个私有子网。现在我需要通过 Terraform 在私有子网中启动一个运行 apache 的实例。我在私有子网中启动一个实例的代码是这样的

resource "aws_instance" "my_apache" {
    ami = "ami-8437a5e4"
    key_name = "clust"
    subnet_id = "${aws_subnet.my_private1.id}"
    vpc_security_group_ids = ["sg-40542d3b"]
    availability_zone = "us-west-2a"
    instance_type = "t2.micro"
    tags {
      Name = "apache"
      }
      provisioner "remote-exec" {
        inline = [
          "sudo apt-get update",
          "sudo apt-get install apache2",
          "sudo service apache2 start"
        ]
      }
}

实例正在启动,但 apache 服务器未在实例中运行。我收到类似 dis 的错误。

aws_instance.my_apache (remote-exec): Connecting to remote host via SSH...
aws_instance.my_apache (remote-exec):   Host: 172.16.2.163
aws_instance.my_apache (remote-exec):   User: root
aws_instance.my_apache (remote-exec):   Password: false
aws_instance.my_apache (remote-exec):   Private key: false
aws_instance.my_apache (remote-exec):   SSH Agent: true
aws_instance.my_apache: Still creating... (3m0s elapsed)
^CInterrupt received. Gracefully shutting down...
aws_instance.my_apache: Still creating... (3m10s elapsed)
aws_instance.my_apache (remote-exec): Connecting to remote host via SSH...
aws_instance.my_apache (remote-exec):   Host: 172.16.2.163
aws_instance.my_apache (remote-exec):   User: root
aws_instance.my_apache (remote-exec):   Password: false
aws_instance.my_apache (remote-exec):   Private key: false
aws_instance.my_apache (remote-exec):   SSH Agent: true

它一直在继续。

可能是什么问题?如何让 apache 在该实例中运行?

【问题讨论】:

  • 您在运行 terraform 的地方有什么样的连接?看起来它无权访问私有子网以连接到您的实例以运行配置

标签: amazon-web-services terraform


【解决方案1】:

首先,您要在私有子网中创建实例,因此请确保您已从运行 terraform 的机器连接到您的实例。

您的日志指定的第二件事:

aws_instance.my_apache (remote-exec):   Password: false
aws_instance.my_apache (remote-exec):   Private key: false

使用provisnors连接:

 connection {
    type     = "ssh"
    user     = "root"
    private_key = "${var.private_key}"
}

参考:https://www.terraform.io/docs/provisioners/connection.html

【讨论】:

    猜你喜欢
    • 2023-01-08
    • 2019-09-08
    • 2015-05-18
    • 2019-06-10
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多