问题
# example.tf
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-0d44833027c1a3297"
instance_type = "t2.micro"
security_groups = ["${aws_security_group.example.name}"]
key_name = "${aws_key_pair.generated_key.key_name}"
provisioner "remote-exec" {
inline = [
"cd /home/ubuntu/",
"nohup python3 -m http.server 8080 &",
]
connection {
type = "ssh"
private_key = "${tls_private_key.example.private_key_pem}"
user = "ubuntu"
timeout = "1m"
}
}
}
resource "tls_private_key" "example" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "aws_key_pair" "generated_key" {
key_name = "example_key_pair"
public_key = "${tls_private_key.example.public_key_openssh}"
}
resource "aws_security_group" "example" {
name = "grant ssh"
description = "grant ssh"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
ami-0d44833027c1a3297
is an ubuntu AMI with a hello world index.html sitting at /home/ubuntu/
Although, I get no errors and the ec2 instance is up and running, the http server doesn't run.
Note: If I ssh manually and apply the same set of commands in the inline section, I am able to successfully run the http server. Any help would be appreciated!
回答1:
Fix:
inline = [
....
"sleep 20",
]
I believe the the provisioner exit before the server process began(race condition) and hence the sleep was necessary.
Note: got the idea of sleep from Hashicorp Packer documentation(https://www.packer.io/docs/provisioners/shell.html)
来源:https://stackoverflow.com/questions/51777850/run-simple-web-server-with-terraform-remote-exec