This repository has been archived by the owner on May 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.tf
106 lines (88 loc) · 2.91 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
provider "template" {
version = "~> 2.1"
}
resource "aws_cloudwatch_log_group" "bastion" {
name = "bastion"
}
data "aws_iam_policy_document" "assume_role" {
statement {
actions = ["sts:AssumeRole"]
principals {
identifiers = ["ecs-tasks.amazonaws.com"]
type = "Service"
}
}
}
resource "aws_iam_role" "execution_role" {
name = "bastion-execution"
assume_role_policy = data.aws_iam_policy_document.assume_role.json
}
data "aws_iam_policy_document" "execution_role" {
statement {
actions = ["ecr:GetAuthorizationToken"]
resources = ["*"]
}
statement {
actions = [
"ecr:BatchCheckLayerAvailability",
"ecr:BatchGetImage",
"ecr:GetDownloadUrlForLayer",
]
resources = [var.image_repository_arn]
}
statement {
actions = [
"logs:CreateLogStream",
"logs:PutLogEvents",
]
resources = ["*"]
}
}
resource "aws_iam_role_policy" "execution_role" {
name = "bastion-execution"
policy = data.aws_iam_policy_document.execution_role.json
role = aws_iam_role.execution_role.id
}
resource "aws_iam_role" "task_role" {
name = "bastion-task"
assume_role_policy = data.aws_iam_policy_document.assume_role.json
}
resource "aws_iam_role_policy" "task_role" {
policy = var.task_role_policy_json
role = aws_iam_role.task_role.id
}
resource "aws_ecs_cluster" "bastion" {
name = "bastions"
}
data "template_file" "container_definitions" {
template = file("${path.module}/container-definitions.tpl.json")
vars = {
assume_role_for_authorized_keys = var.public_key_fetcher_role_arn
image = "${var.image_repository_url}:latest"
log_group_name = aws_cloudwatch_log_group.bastion.name
name = "bastion"
region = var.region
}
}
resource "aws_ecs_task_definition" "bastion" {
container_definitions = data.template_file.container_definitions.rendered
cpu = "256"
execution_role_arn = aws_iam_role.execution_role.arn
family = "bastions"
memory = "512"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
task_role_arn = aws_iam_role.task_role.arn
}
module "api" {
source = "./api"
cluster_arn = aws_ecs_cluster.bastion.arn
cluster_name = aws_ecs_cluster.bastion.name
cluster_subnet_ids = var.public_subnet_ids
cluster_vpc_default_security_group_id = var.vpc_default_security_group_id
cluster_vpc_id = var.vpc_id
container_name = "bastion"
execution_role_arn = aws_iam_role.execution_role.arn
task_family = aws_ecs_task_definition.bastion.family
task_role_arn = aws_iam_role.task_role.arn
}