-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
83 lines (72 loc) · 2.06 KB
/
main.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
provider "aws" {
region = "eu-central-1"
}
resource "aws_instance" "rest_api_prod" {
ami = "ami-0c960b947cbb2dd16"
instance_type = "t3.small"
vpc_security_group_ids = [aws_security_group.security_group.id]
key_name = var.key_pair_name
associate_public_ip_address = true
iam_instance_profile = aws_iam_instance_profile.profile.name
tags = {
Name = "klarna-task-rest-api"
}
connection {
type = "ssh"
user = "ubuntu"
private_key = file("~/.ssh/${var.key_pair_name}.pem")
host = aws_instance.rest_api_prod.public_ip
}
provisioner "file" {
source = "docker-compose.yaml"
destination = "/home/ubuntu/docker-compose.yaml"
}
provisioner "remote-exec" {
inline = [
"sudo apt update",
"sudo apt install -y docker.io awscli",
"sudo curl -L \"https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)\" -o /usr/local/bin/docker-compose",
"sudo chmod +x /usr/local/bin/docker-compose",
"sudo $(aws ecr get-login --no-include-email --region eu-central-1)",
"sudo docker pull ${var.docker_registry}/klarna-task",
"sudo DOCKER_REGISTRY=${var.docker_registry} docker-compose up -d rest-api-prod ",
]
}
}
resource "aws_security_group" "security_group" {
name = "klarna-task-security-group"
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"]
}
}
resource "aws_iam_role" "role" {
name = "klarna-role"
assume_role_policy = file("ops/role-policy.json")
}
resource "aws_iam_policy" "policy" {
name = "klarna-policy"
policy = file("ops/policy.json")
}
resource "aws_iam_role_policy_attachment" "attach" {
role = aws_iam_role.role.name
policy_arn = aws_iam_policy.policy.arn
}
resource "aws_iam_instance_profile" "profile" {
name = "klarna-profile"
role = aws_iam_role.role.name
}