Merge branch '21507-long-project-title-wrap'
[arvados.git] / tools / salt-install / terraform / aws / services / main.tf
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: CC-BY-SA-3.0
4
5 terraform {
6   required_version = "~> 1.3.0"
7   required_providers {
8     aws = {
9       source = "hashicorp/aws"
10       version = "~> 4.38.0"
11     }
12   }
13 }
14
15 provider "aws" {
16   region = local.region_name
17   default_tags {
18     tags = merge(local.custom_tags, {
19       Arvados = local.cluster_name
20       Terraform = true
21     })
22   }
23 }
24
25 resource "aws_iam_instance_profile" "keepstore_instance_profile" {
26   name = "${local.cluster_name}-keepstore-00-iam-role"
27   role = data.terraform_remote_state.data-storage.outputs.keepstore_iam_role_name
28 }
29
30 resource "aws_iam_instance_profile" "compute_node_instance_profile" {
31   name = "${local.cluster_name}-compute-node-00-iam-role"
32   role = local.compute_node_iam_role_name
33 }
34
35 resource "aws_iam_instance_profile" "dispatcher_instance_profile" {
36   name = "${local.cluster_name}_dispatcher_instance_profile"
37   role = aws_iam_role.cloud_dispatcher_iam_role.name
38 }
39
40 resource "aws_secretsmanager_secret" "ssl_password_secret" {
41   name = local.ssl_password_secret_name
42   recovery_window_in_days = 0
43 }
44
45 resource "aws_iam_instance_profile" "default_instance_profile" {
46   name = "${local.cluster_name}_default_instance_profile"
47   role = aws_iam_role.default_iam_role.name
48 }
49
50 resource "aws_instance" "arvados_service" {
51   for_each = toset(concat(local.public_hosts, local.private_hosts))
52   ami = local.instance_ami_id
53   instance_type = try(var.instance_type[each.value], var.instance_type.default)
54   user_data = templatefile("user_data.sh", {
55     "hostname": each.value,
56     "deploy_user": var.deploy_user,
57     "ssh_pubkey": file(local.pubkey_path)
58   })
59   private_ip = local.private_ip[each.value]
60   subnet_id = contains(local.user_facing_hosts, each.value) ? local.public_subnet_id : local.private_subnet_id
61   vpc_security_group_ids = [ local.arvados_sg_id ]
62   iam_instance_profile = try(local.instance_profile[each.value], local.instance_profile.default).name
63   tags = {
64     Name = "${local.cluster_name}_arvados_service_${each.value}"
65   }
66   root_block_device {
67     volume_type = "gp3"
68     volume_size = try(var.instance_volume_size[each.value], var.instance_volume_size.default)
69   }
70   metadata_options {
71     # Sets IMDSv2 to required. Default is "optional".
72     http_tokens = "required"
73   }
74   lifecycle {
75     ignore_changes = [
76       # Avoids recreating the instance when the latest AMI changes.
77       # Use 'terraform taint' or 'terraform apply -replace' to force
78       # an AMI change.
79       ami,
80     ]
81   }
82 }
83
84 resource "aws_iam_policy" "compute_node_ebs_autoscaler" {
85   name = "${local.cluster_name}_compute_node_ebs_autoscaler"
86   policy = jsonencode({
87     Version: "2012-10-17",
88     Id: "compute-node EBS Autoscaler policy",
89     Statement: [{
90       Effect: "Allow",
91       Action: [
92           "ec2:AttachVolume",
93           "ec2:DescribeVolumeStatus",
94           "ec2:DescribeVolumes",
95           "ec2:DescribeTags",
96           "ec2:ModifyInstanceAttribute",
97           "ec2:DescribeVolumeAttribute",
98           "ec2:CreateVolume",
99           "ec2:DeleteVolume",
100           "ec2:CreateTags"
101       ],
102       Resource: "*"
103     }]
104   })
105 }
106
107 resource "aws_iam_policy_attachment" "compute_node_ebs_autoscaler_attachment" {
108   name = "${local.cluster_name}_compute_node_ebs_autoscaler_attachment"
109   roles = [ local.compute_node_iam_role_name ]
110   policy_arn = aws_iam_policy.compute_node_ebs_autoscaler.arn
111 }
112
113 resource "aws_iam_policy" "cloud_dispatcher_ec2_access" {
114   name = "${local.cluster_name}_cloud_dispatcher_ec2_access"
115   policy = jsonencode({
116     Version: "2012-10-17",
117     Id: "arvados-dispatch-cloud policy",
118     Statement: [{
119       Effect: "Allow",
120       Action: [
121         "ec2:DescribeKeyPairs",
122         "ec2:ImportKeyPair",
123         "ec2:RunInstances",
124         "ec2:DescribeInstances",
125         "ec2:CreateTags",
126         "ec2:TerminateInstances"
127       ],
128       Resource: "*"
129     },
130     {
131       Effect: "Allow",
132       Action: [
133         "iam:PassRole",
134       ],
135       Resource: "arn:aws:iam::*:role/${aws_iam_instance_profile.compute_node_instance_profile.name}"
136     }]
137   })
138 }
139
140 resource "aws_iam_role" "cloud_dispatcher_iam_role" {
141   name = "${local.cluster_name}-dispatcher-00-iam-role"
142   assume_role_policy = "${file("../assumerolepolicy.json")}"
143 }
144
145 resource "aws_iam_policy_attachment" "cloud_dispatcher_ec2_access_attachment" {
146   name = "${local.cluster_name}_cloud_dispatcher_ec2_access_attachment"
147   roles = [ aws_iam_role.cloud_dispatcher_iam_role.name ]
148   policy_arn = aws_iam_policy.cloud_dispatcher_ec2_access.arn
149 }
150
151 resource "aws_eip_association" "eip_assoc" {
152   for_each = local.private_only ? [] : toset(local.public_hosts)
153   instance_id = aws_instance.arvados_service[each.value].id
154   allocation_id = local.eip_id[each.value]
155 }
156
157 resource "aws_iam_role" "default_iam_role" {
158   name = "${local.cluster_name}-default-iam-role"
159   assume_role_policy = "${file("../assumerolepolicy.json")}"
160 }
161
162 resource "aws_iam_policy" "ssl_privkey_password_access" {
163   name = "${local.cluster_name}_ssl_privkey_password_access"
164   policy = jsonencode({
165     Version: "2012-10-17",
166     Statement: [{
167       Effect: "Allow",
168       Action: "secretsmanager:GetSecretValue",
169       Resource: "${aws_secretsmanager_secret.ssl_password_secret.arn}"
170     }]
171   })
172 }
173
174 # Every service node needs access to the SSL privkey password secret for
175 # nginx to be able to use it.
176 resource "aws_iam_policy_attachment" "ssl_privkey_password_access_attachment" {
177   name = "${local.cluster_name}_ssl_privkey_password_access_attachment"
178   roles = [
179     aws_iam_role.cloud_dispatcher_iam_role.name,
180     aws_iam_role.default_iam_role.name,
181     local.keepstore_iam_role_name,
182   ]
183   policy_arn = aws_iam_policy.ssl_privkey_password_access.arn
184 }