terraform-tofu-labs/2-simple-example/code/main.tf

56 lines
1.2 KiB
Terraform
Raw Permalink Normal View History

2024-04-15 22:33:12 +00:00
provider "aws" {
2024-04-16 15:32:58 +00:00
region = "eu-west-1"
2024-04-15 22:33:12 +00:00
}
resource "aws_security_group" "web_server_sg_tf" {
name = "web-server-sg-tf"
description = "Allow HTTP to web server"
vpc_id = module.vpc.vpc_id
2024-04-16 15:32:58 +00:00
ingress {
description = "SSH ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
2024-04-15 22:33:12 +00:00
ingress {
description = "HTTP ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "HTTPS ingress"
from_port = 443
to_port = 443
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"]
}
}
resource "aws_instance" "test_ami" {
2024-04-16 15:32:58 +00:00
ami = "ami-029b91ed285a24a90"
instance_type = "t4g.nano"
2024-04-15 22:33:12 +00:00
associate_public_ip_address = true
2024-04-16 15:32:58 +00:00
subnet_id = module.vpc.public_subnets[0]
2024-04-15 22:33:12 +00:00
vpc_security_group_ids = [aws_security_group.web_server_sg_tf.id]
2024-04-16 15:32:58 +00:00
user_data = <<-EOF
#!/bin/bash
sudo dnf install -y nginx
2024-04-16 16:12:40 +00:00
sudo systemctl enable nginx
sudo systemctl start nginx
2024-04-16 15:32:58 +00:00
EOF
2024-04-15 22:33:12 +00:00
}