-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute.tf
135 lines (115 loc) · 3.24 KB
/
compute.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
locals {
ami = var.ami != "" ? var.ami : data.aws_ami.ubuntu.image_id
}
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["099720109477"]
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"]
}
}
module "security_group" {
source = "terraform-aws-modules/security-group/aws"
version = "3.16.0"
name = "flask_app"
description = "Security group for flask_app"
vpc_id = module.vpc.vpc_id
ingress_cidr_blocks = ["0.0.0.0/0"]
ingress_rules = ["http-80-tcp", "ssh-tcp"]
egress_rules = ["all-all"]
}
module "elb_http" {
source = "terraform-aws-modules/elb/aws"
version = "2.4.0"
name = "flask-app"
subnets = module.vpc.public_subnets
security_groups = [module.security_group.this_security_group_id]
internal = false
idle_timeout = 65
connection_draining = true
connection_draining_timeout = 15
listener = [
{
instance_port = "80"
instance_protocol = "http"
lb_port = "80"
lb_protocol = "http"
},
]
health_check = {
target = "HTTP:80/"
interval = 30
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 5
}
number_of_instances = 1
instances = [aws_instance.ec2.id]
tags = merge(
{
"Terraform" = "true"
},
var.tags,
)
}
resource "aws_key_pair" "ec2_keypair" {
key_name = "flask_app_key"
public_key = file("keys/id_rsa.pub")
}
locals {
user_data = <<EOF
#!/bin/bash
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
EOF
}
resource "aws_instance" "ec2" {
ami = local.ami
instance_type = var.instance_type
key_name = "flask_app_key"
subnet_id = element(module.vpc.public_subnets, 0)
vpc_security_group_ids = [module.security_group.this_security_group_id]
associate_public_ip_address = true
user_data_base64 = base64encode(local.user_data)
tags = merge(
{
"Name" = "flask_app"
"Terraform" = "true"
},
var.tags
)
}
data "local_file" "app" {
filename = "docker/flask_app/app.py"
}
resource "null_resource" "deploy" {
triggers = {
app = data.local_file.app.content
}
connection {
type = "ssh"
user = "ubuntu"
host = aws_instance.ec2.public_ip
private_key = file("keys/id_rsa")
}
provisioner "file" {
source = "docker/"
destination = "/home/ubuntu/"
}
provisioner "remote-exec" {
inline = [
"sleep 120",
"cd /home/ubuntu",
"sudo docker-compose down",
"sudo docker-compose up --build -d"
]
}
depends_on = [aws_instance.ec2]
}