在应用期间使用 terraform 创建 ec2 后如何 运行 scritps?

How to run scritps after create ec2 using terraform during apply?

在 terraform 中有一个在 aws 中创建 EC2 机器的示例。

# Create a new instance of the latest Ubuntu 20.04 on an
# t3.micro node with an AWS Tag naming it "HelloWorld"
provider "aws" {
  region = "us-west-2"
}

data "aws_ami" "ubuntu" {
  most_recent = true

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

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["099720109477"] # Canonical
}

resource "aws_instance" "web" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"

  tags = {
    Name = "HelloWorld"
  }
}

但是我也可以 运行 里面的一些脚本吗?喜欢安装詹金斯?安装 docker,或只是 运行 命令:sudo yum update -y 在 terraform apply 操作期间?

如果是这样,我会非常适合类似的示例或指南资源。

是的,你可以。在 AWS 中,您使用 UserData 来表示:

can be used to perform common automated configuration tasks and even run scripts after the instance starts.

在terraform中,对应的属性是user_data

要使用它来安装 Jenkins,您可以尝试以下操作:

resource "aws_instance" "web" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"

  user_data = <<-EOL
  #!/bin/bash -xe

  apt update
  apt install openjdk-8-jdk --yes
  wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
  echo "deb https://pkg.jenkins.io/debian binary/" >> /etc/apt/sources.list
  apt update
  apt install -y jenkins
  systemctl status jenkins
  find /usr/lib/jvm/java-1.8* | head -n 3  
  EOL

  tags = {
    Name = "HelloWorld"
  }
}

请注意,上面的代码只是示例,我不能保证它能在 Ubuntu 20.04 上运行。但它适用于 18.04。此外,Jenksis 在端口 8080 上工作,因此如果您想直接访问 jenkins,例如没有 ssh 隧道,您的安全组需要允许它。

注:

AWS 允许我们在现有实例的控制台中直接更新“用户数据”,前提是它处于停止状态。人们会期望 Terraform 也以相同的方式运行,您将在 Terraform 脚本中更新您的“user_data”部分,并且在下一次应用期间,如果服务器处于停止状态,它会在 AWS 中更新。但是没有。

如果您正在通过 terraform 更新现有 EC2 实例中的 user_data,该实例将被重新创建 --> 销毁并添加。 “Terraform Plan”无论如何都会显示这一点(我还不确定它背后的逻辑是什么,但这就是今天的样子,所以要小心)。

    # aws_instance.my-instance **must be replaced**\
.....
.....
    Plan: **1 to add**, 0 to change, **1 to destroy**.

同样的问题,如果您在 AWS 控制台中手动进行更改 - terraform 将获取更改并告知它必须重新创建服务器。

还有 Provisioner 选项,但 Terraform 对此有注释

/// 来自 TF 文档 /// 注意:Provisioner 只能作为最后的手段使用。对于大多数常见情况,有更好的选择。有关详细信息,请参阅供应商主页。 ///

请先看看这些 link,然后再决定: https://www.terraform.io/docs/provisioners/index.html https://www.terraform.io/docs/provisioners/remote-exec.html

无论如何我都会发布一个简单的例子。

resource "aws_instance" "WebServer"  {
  ami           = "ami-SomeValid_AMI_ID"
  instance_type = "t2.micro"
  key_name = "SomeValid_keypair"
   
    provisioner "remote-exec" {
    
    inline = [
    "sudo amazon-linux-extras install -y nginx1.12",
    "sudo systemctl start nginx"
    ]
    
    connection {
    type = "ssh"
    user = "ec2-user"
    private_key = file("F:\PathToMyKeysFolder\SomeValid_keypair.pem")
    host = self.public_ip
    
    }
    
    }