zkt26/sk1/terraform/ec2.tf
2026-05-13 21:49:47 +02:00

83 lines
2.0 KiB
HCL

resource "tls_private_key" "main" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "aws_key_pair" "main" {
key_name = "${local.name_prefix}-key"
public_key = tls_private_key.main.public_key_openssh
}
resource "aws_eip" "nginx" {
domain = "vpc"
tags = {
Name = "${local.name_prefix}-nginx-eip"
}
}
resource "aws_eip_association" "nginx" {
instance_id = aws_instance.nginx.id
allocation_id = aws_eip.nginx.id
}
resource "aws_instance" "nginx" {
ami = data.aws_ami.amazon_linux.id
instance_type = var.nginx_instance_type
subnet_id = aws_subnet.public[0].id
vpc_security_group_ids = [aws_security_group.nginx.id]
key_name = aws_key_pair.main.key_name
iam_instance_profile = aws_iam_instance_profile.ec2.name
root_block_device {
volume_size = 30
volume_type = "gp3"
encrypted = true
}
user_data = <<-EOF
#!/bin/bash
set -e
dnf update -y
dnf install -y docker
systemctl enable docker
systemctl start docker
curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
mkdir -p /opt/app
EOF
tags = {
Name = "${local.name_prefix}-nginx"
}
}
resource "aws_instance" "app" {
ami = data.aws_ami.amazon_linux.id
instance_type = var.app_instance_type
subnet_id = aws_subnet.public[0].id
vpc_security_group_ids = [aws_security_group.app.id]
key_name = aws_key_pair.main.key_name
iam_instance_profile = aws_iam_instance_profile.ec2.name
root_block_device {
volume_size = 30
volume_type = "gp3"
encrypted = true
}
user_data = <<-EOF
#!/bin/bash
set -e
dnf update -y
dnf install -y docker aws-cli
systemctl enable docker
systemctl start docker
mkdir -p /opt/app
EOF
tags = {
Name = "${local.name_prefix}-app"
}
}