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