provider "aws" {
region = "ap-northeast-2"
}
resource "aws_instance" "example" {
ami = "LINUX AMI IMAGE"
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.webserversg.id]
user_data = <<-EOF
#!/bin/bash
echo "Hello, World" > index.html
nohup busybox httpd -f -p 8080 &
EOF
user_data_replace_on_change = true
tags = {
Name = "terraform-example"
}
}
resource "aws_security_group" "webserversg" {
name = "terraform-example-webserversg"
# 이 부분 추가 #
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
################
...
...
user_data = <<-EOF
#!/bin/bash
echo "Hello, World" > index.html
nohup busybox httpd -f -p 8080 &
EOF
...
...
- 이 부분의
nohup
이 Amazon Linux 에서 다이렉트로 사용되려면 key를 통해 인증하거나 머신 내부에서 다운로드 받아야함
provider "aws" {
region = "ap-northeast-2"
}
resource "aws_instance" "example" {
# 이 부분 수정 #
ami = "ami-086cae3329a3f7d75"
################
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.webserversg.id]
user_data = <<-EOF
#!/bin/bash
echo "Hello, World" > index.html
nohup busybox httpd -f -p 8080 &
EOF
user_data_replace_on_change = true
tags = {
Name = "terraform-example"
}
}
resource "aws_security_group" "webserversg" {
name = "terraform-example-webserversg"
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
variable "service_port" {
description = "Service Port for HTTP Request"
type = number
default = 8080
}
...
...
user_data = <<-EOF
#!/bin/bash
echo "Hello, World" > index.html
nohup busybox httpd -f -p ${var.service_port} &
EOF
...
...
resource "aws_security_group" "webserversg" {
name = "terraform-example-webserversg"
ingress {
from_port = var.service_port
to_port = var.service_port
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
variable "service_port" {
description = "Service Port for HTTP Request"
type = number
default = 8080
}
...
...
...
...
output "public_ip" {
value = aws_instance.example.public_ip
description = "Public IP for Web Server"
}
output "service_url" {
value = "http://${aws_instance.example.public_ip}:${var.service_port}"
description = "Web Server Service URL"
}
...
...