devops@control:~/aws-example$ vi security-group.tf
devops@control:~/aws-example$ terraform fmt
devops@control:~/aws-example$ terraform validate
Success! The configuration is valid.
devops@control:~/aws-example$ cat security-group.tf
resource "aws_security_group" "my_dh_web" {
name = "allow-web"
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
devops@control:~/aws-example$ cat web_deploy.sh
#!/bin/bash
sudo yum install -y https
sudo systemctl --now enable httpd
echo -n " <h1> hello ldh </h1> " | sudo tee /var/www/html/index.html
devops@control:~/aws-example$ cat main.tf
resource "aws_instance" "my_instance" {
ami = data.aws_ami.amazon_linux.id
instance_type = var.instance_type
vpc_security_group_ids = [aws_security_group.my_dh_web.id]
user_data = file("./web_deploy.sh")
tags = local.common_tags
}
resource "aws_eip" "my_eip" {
vpc = true
instance = aws_instance.my_instance.id
}
devops@control:~/aws-example$ cat security-group.tf
resource "aws_security_group" "my_dh_web" {
name = "allow-web"
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
devops@control:~/aws-example$ cat main.tf
resource "aws_instance" "my_instance" {
ami = data.aws_ami.amazon_linux.id
instance_type = var.instance_type
vpc_security_group_ids = [aws_security_group.my_dh_web.id]
key_name = aws_key_pair.my_sshkey.key_name
connection {
type = "ssh"
user = "ec2-user"
private_key = file("./my_sshkey")
host = self.public_ip
}
provisioner "file" {
source = "web_deploy.sh"
destination = "/tmp/web_deploy.sh"
}
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/web_deploy.sh"
]
}
provisioner "local-exec" {
command = "echo ${self.public_ip} > ipaddr.txt"
}
tags = local.common_tags
}
resource "aws_key_pair" "my_sshkey" {
key_name = "my_sshkey"
public_key = file("./my_sshkey.pub")
}
resource "aws_eip" "my_eip" {
vpc = true
instance = aws_instance.my_instance.id
}
'클라우드 > 테라폼(Terraform)' 카테고리의 다른 글
테라폼 Terraform 반복 (0) | 2021.08.06 |
---|---|
테라폼 Terraform 모듈 (0) | 2021.08.06 |
테라폼 Terraform 데이터 소스 (0) | 2021.08.05 |
테라폼 Terraform 로컬 값 (0) | 2021.08.05 |
테라폼 Terraform 출력 값 (0) | 2021.08.05 |