在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"
}
}
但是我还可以在其中运行一些脚本吗?喜欢安装詹金斯?安装泊坞窗,还是只运行命令:sudo yum update -y
在Terraform应用操作期间?
如果是这样,我将很适合作为此类示例或指导资源。
是的你可以。在AWS中,将UserData用于以下用途:
可用于执行常见的自动化配置任务,甚至在实例启动后运行脚本。
在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隧道),则安全组将需要允许它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句