|
| 1 | +# |
| 2 | +# Terraform file for deploying lnt.llvm.org. |
| 3 | +# |
| 4 | + |
| 5 | +variable "lnt_db_password" { |
| 6 | + type = string |
| 7 | + description = "The database password for the lnt.llvm.org database." |
| 8 | + sensitive = true |
| 9 | +} |
| 10 | + |
| 11 | +variable "lnt_auth_token" { |
| 12 | + type = string |
| 13 | + description = "The authentication token to perform destructive operations on lnt.llvm.org." |
| 14 | + sensitive = true |
| 15 | +} |
| 16 | + |
| 17 | +locals { |
| 18 | + # The Docker image to use for the webserver part of the LNT service |
| 19 | + lnt_image = "d9ffa5317a9a42a1d2fa337cba97ec51d931f391" |
| 20 | + |
| 21 | + # The port on the EC2 instance used by the Docker webserver for communication |
| 22 | + lnt_host_port = "80" |
| 23 | +} |
| 24 | + |
| 25 | +terraform { |
| 26 | + backend "s3" { |
| 27 | + bucket = "lnt.llvm.org-test-bucket" # TODO: Adjust this for the real LLVM Foundation account |
| 28 | + key = "terraform.tfstate" |
| 29 | + region = "us-west-2" |
| 30 | + encrypt = true |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +locals { |
| 35 | + availability_zone = "us-west-2a" |
| 36 | +} |
| 37 | + |
| 38 | +provider "aws" { |
| 39 | + region = "us-west-2" |
| 40 | +} |
| 41 | + |
| 42 | +# |
| 43 | +# Setup the EC2 instance |
| 44 | +# |
| 45 | +data "aws_ami" "amazon_linux_2023" { |
| 46 | + most_recent = true |
| 47 | + owners = ["amazon"] |
| 48 | + |
| 49 | + filter { |
| 50 | + name = "name" |
| 51 | + values = ["al2023-ami-ecs-hvm-*-kernel-*-x86_64"] |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +data "cloudinit_config" "startup_scripts" { |
| 56 | + base64_encode = true |
| 57 | + |
| 58 | + part { |
| 59 | + filename = "ec2-startup.sh" |
| 60 | + content_type = "text/x-shellscript" |
| 61 | + content = file("${path.module}/ec2-startup.sh") |
| 62 | + } |
| 63 | + |
| 64 | + part { |
| 65 | + content_type = "text/cloud-config" |
| 66 | + content = yamlencode({ |
| 67 | + write_files = [ |
| 68 | + { |
| 69 | + path = "/etc/lnt/compose.yaml" |
| 70 | + permissions = "0400" # read-only for owner |
| 71 | + content = file("${path.module}/../docker/compose.yaml") |
| 72 | + }, |
| 73 | + { |
| 74 | + path = "/etc/lnt/ec2-volume-mapping.yaml" |
| 75 | + permissions = "0400" # read-only for owner |
| 76 | + content = file("${path.module}/ec2-volume-mapping.yaml") |
| 77 | + }, |
| 78 | + { |
| 79 | + path = "/etc/lnt/compose.env" |
| 80 | + permissions = "0400" # read-only for owner |
| 81 | + content = templatefile("${path.module}/compose.env.tpl", { |
| 82 | + __db_password__ = var.lnt_db_password, |
| 83 | + __auth_token__ = var.lnt_auth_token, |
| 84 | + __lnt_image__ = local.lnt_image, |
| 85 | + __lnt_host_port__ = local.lnt_host_port, |
| 86 | + }) |
| 87 | + } |
| 88 | + ] |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +resource "aws_security_group" "server" { |
| 94 | + name = "lnt.llvm.org/server-security-group" |
| 95 | + description = "Allow SSH and HTTP traffic" |
| 96 | + |
| 97 | + ingress { |
| 98 | + description = "Allow incoming SSH traffic from anywhere" |
| 99 | + from_port = 22 |
| 100 | + to_port = 22 |
| 101 | + protocol = "tcp" |
| 102 | + cidr_blocks = ["0.0.0.0/0"] |
| 103 | + } |
| 104 | + |
| 105 | + ingress { |
| 106 | + description = "Allow incoming HTTP traffic from anywhere" |
| 107 | + from_port = 80 |
| 108 | + to_port = 80 |
| 109 | + protocol = "tcp" |
| 110 | + cidr_blocks = ["0.0.0.0/0"] |
| 111 | + } |
| 112 | + |
| 113 | + egress { |
| 114 | + description = "Allow outgoing traffic to anywhere" |
| 115 | + from_port = 0 |
| 116 | + to_port = 0 |
| 117 | + protocol = "-1" |
| 118 | + cidr_blocks = ["0.0.0.0/0"] |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +resource "aws_instance" "server" { |
| 123 | + ami = data.aws_ami.amazon_linux_2023.id |
| 124 | + availability_zone = local.availability_zone |
| 125 | + instance_type = "t2.micro" # TODO: Adjust the size of the real instance |
| 126 | + associate_public_ip_address = true |
| 127 | + security_groups = [aws_security_group.server.name] |
| 128 | + tags = { |
| 129 | + Name = "lnt.llvm.org/server" |
| 130 | + } |
| 131 | + |
| 132 | + user_data_base64 = data.cloudinit_config.startup_scripts.rendered |
| 133 | +} |
| 134 | + |
| 135 | +# |
| 136 | +# Setup the EBS volume attached to the instance that stores the DB |
| 137 | +# and other instance-related configuration (e.g. the schema files, |
| 138 | +# profiles and anything else that should persist). |
| 139 | +# |
| 140 | +resource "aws_ebs_volume" "persistent_state" { |
| 141 | + availability_zone = local.availability_zone |
| 142 | + # TODO: Put a real size once we're ready to go to production |
| 143 | + size = 20 # GiB |
| 144 | + type = "gp2" |
| 145 | + tags = { |
| 146 | + Name = "lnt.llvm.org/persistent-state" |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +resource "aws_volume_attachment" "persistent_state_attachment" { |
| 151 | + instance_id = aws_instance.server.id |
| 152 | + volume_id = aws_ebs_volume.persistent_state.id |
| 153 | + device_name = "/dev/sdh" |
| 154 | +} |
0 commit comments