【问题标题】:Terraform Remote Exec Host IP from EC2 resource built来自 EC2 资源的 Terraform 远程执行主机 IP 已构建
【发布时间】:2020-01-31 23:54:44
【问题描述】:

Looooong 时间潜伏者和第一次在这里发布海报 o/

我目前正在尝试构建一个附加了 EBS 块设备的 AWS EC2 实例,然后需要安装 MongoDB。

所以我已经完成了构建 EC2 实例并附加 EBS 卷的路线,但是我需要在实例上运行的 remote-exec 需要一个主机 IP 来连接,以运行 MongoDB 安装命令。

无论我尝试什么,它都会在 SSH 上超时。现在我可能只是错过了一步或以错误的方式进行此操作,但希望您能提供帮助。

任何帮助将不胜感激。 :D

以下是我拼凑的代码示例:

    provider "aws" {
  region     = "eu-west-1"
  access_key = "xxxxxxx"
  secret_key = "xxxxxxxx"
}

resource "tls_private_key" "mongo" {
  algorithm = "RSA"
  rsa_bits  = 4096
}

resource "aws_key_pair" "generated_key" {
  key_name   = "MongoKey"
  public_key = "${tls_private_key.mongo.public_key_openssh}"
}

data "aws_ami" "ubuntu" {
  most_recent = true
  owners      = ["099720109477"]

  filter {
    name = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]
  }


}


resource "aws_instance" "web" {
    ami                         = "${data.aws_ami.ubuntu.id}"
    instance_type               = "t2.micro"
    key_name                    = "MongoKey"
    monitoring                  = true
    associate_public_ip_address = true


  root_block_device {
    volume_size = 40
  }

  ebs_block_device {
    volume_size = 100
    device_name = "xvda"    
  }

  tags = {
    Name = "MongoDB"
  }


    provisioner "remote-exec" {
            connection {
                type      = "ssh"
                user      = "ubuntu"
                host      = "MongoDB"
              }

      inline = [
        "sudo apt-get install gnupg",
        "wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -",
        "echo deb [ arch=amd64,arm64,s390x ] http://repo.mongodb.com/apt/ubuntu xenial/mongodb-enterprise/4.2 multiverse | sudo tee /etc/apt/sources.list.d/mongodb-enterprise.list",
        "sudo apt-get update",
        "sudo apt-get install -y mongodb-enterprise",
        "sudo service mongod start",
        "sudo service mongod status"
      ]
  }

【问题讨论】:

    标签: amazon-ec2 terraform


    【解决方案1】:

    您可以尝试如下更改您的连接部分

    connection {
                type        = "ssh"
                user        = "ubuntu"
                host        = "${aws_instance.web.private_ip}"
                private_key = "${tls_private_key.mongo.private_key_pem}"
    
              }
    

    【讨论】:

      【解决方案2】:

      如果您在使用remote-exec 方法时继续面临连接问题/困难,我建议您考虑使用user-data 参数作为替代。 user-data 脚本将在您的实例初始化期间运行,因此您不必打开任何 ssh 会话来配置您的资源。

      您可以通过将您的 aws_instance 资源更新为以下内容来完成此操作:

      resource "aws_instance" "web" {
          ami                         = "${data.aws_ami.ubuntu.id}"
          instance_type               = "t2.micro"
          key_name                    = "MongoKey"
          monitoring                  = true
          associate_public_ip_address = true
      
      
        root_block_device {
          volume_size = 40
        }
      
        ebs_block_device {
          volume_size = 100
          device_name = "xvda"    
        }
      
        tags = {
          Name = "MongoDB"
        }
      
          user_data = << EOF
              #! /bin/bash
              sudo apt-get install gnupg,
              wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -,
              echo deb [ arch=amd64,arm64,s390x ] http://repo.mongodb.com/apt/ubuntu xenial/mongodb-enterprise/4.2 multiverse | sudo tee /etc/apt/sources.list.d/mongodb-enterprise.list,
              sudo apt-get update,
              sudo apt-get install -y mongodb-enterprise,
              sudo service mongod start,
              sudo service mongod status
          EOF
        }
      

      希望这会有所帮助。

      more userdata examples

      【讨论】:

        猜你喜欢
        • 2019-09-16
        • 1970-01-01
        • 2012-11-15
        • 2023-02-11
        • 1970-01-01
        • 1970-01-01
        • 2021-01-03
        • 2018-08-01
        • 1970-01-01
        相关资源
        最近更新 更多