-
Notifications
You must be signed in to change notification settings - Fork 1
/
ec2.tf
86 lines (70 loc) · 2.36 KB
/
ec2.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# ec2.tf
resource "aws_launch_template" "web_app" {
name_prefix = "web-app"
image_id = var.ami_id
instance_type = var.instance_type
tag_specifications {
resource_type = "instance"
tags = {
Name = "web-app-ec2"
}
}
iam_instance_profile {
name = aws_iam_instance_profile.ec2_instance_profile.name
}
network_interfaces {
associate_public_ip_address = true
security_groups = [aws_security_group.ec2_sg.id]
subnet_id = aws_subnet.public[0].id # Specify the public subnet
}
user_data = base64encode(<<-EOF
#!/bin/bash
sudo yum update -y
sudo yum -y install docker
sudo service docker start
sudo systemctl enable docker
sudo usermod -a -G docker ec2-user
sudo chmod 666 /var/run/docker.sock
sudo yum install git -y
sudo yum install -y amazon-ssm-agent # Install SSM agent
sudo systemctl enable amazon-ssm-agent
sudo systemctl start amazon-ssm-agent
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
git clone https://github.com/DrCBeatz/ai_greeting_cards.git
cd ai_greeting_cards
sudo chown -R ec2-user:ec2-user /ai_greeting_cards
cat <<EOT >> .env
${local.env_file_content}
EOT
sudo chmod a+w .env
docker-compose -f docker-compose.prod.yml up -d --build
docker-compose -f docker-compose.prod.yml exec web python manage.py migrate
EOF
)
}
resource "aws_autoscaling_group" "web_app_asg" {
desired_capacity = 1
max_size = 3
min_size = 1
vpc_zone_identifier = aws_subnet.public.*.id
mixed_instances_policy {
launch_template {
launch_template_specification {
launch_template_id = aws_launch_template.web_app.id
version = "$Latest"
}
override {
instance_type = var.instance_type
}
}
instances_distribution {
on_demand_base_capacity = 0
on_demand_percentage_above_base_capacity = 0
spot_allocation_strategy = "lowest-price"
}
}
target_group_arns = [aws_lb_target_group.web_app_tg.arn]
health_check_type = "EC2"
health_check_grace_period = 300
}