| 1 | # Workspace to dump analysis data extracted from the cache and other places. |
| 2 | resource "aws_s3_bucket" "archeologist" { |
| 3 | # Keep it in the same region as the cache |
| 4 | provider = aws.us |
| 5 | |
| 6 | bucket = "nix-archeologist" |
| 7 | } |
| 8 | |
| 9 | data "aws_iam_policy_document" "archaeologist" { |
| 10 | statement { |
| 11 | # Read-only access and listing permissions |
| 12 | # To the cache and releases inventories, |
| 13 | # as well as the bucket where cache bucket logs end up in. |
| 14 | sid = "NixCacheReadOnly" |
| 15 | |
| 16 | actions = [ |
| 17 | "s3:List*", |
| 18 | "s3:Get*" |
| 19 | ] |
| 20 | |
| 21 | resources = [ |
| 22 | "arn:aws:s3:::nix-cache", |
| 23 | "arn:aws:s3:::nix-cache/*", |
| 24 | "arn:aws:s3:::nix-cache-inventory", |
| 25 | "arn:aws:s3:::nix-cache-inventory/*", |
| 26 | "arn:aws:s3:::nix-cache-log", |
| 27 | "arn:aws:s3:::nix-cache-log/*", |
| 28 | "arn:aws:s3:::nix-releases-inventory220231029182031496800000001", |
| 29 | "arn:aws:s3:::nix-releases-inventory220231029182031496800000001/*" |
| 30 | ] |
| 31 | } |
| 32 | |
| 33 | statement { |
| 34 | # Allows fetching information on the bucket |
| 35 | sid = "ListMetrics" |
| 36 | |
| 37 | actions = [ |
| 38 | "cloudwatch:ListMetrics", |
| 39 | "cloudwatch:GetMetricStatistics" |
| 40 | ] |
| 41 | |
| 42 | # We don't have any private metrics, KISS |
| 43 | resources = ["*"] |
| 44 | } |
| 45 | |
| 46 | statement { |
| 47 | # Full access to the Archaeologist bucket |
| 48 | sid = "NixArchaeologistReadWrite" |
| 49 | |
| 50 | actions = [ |
| 51 | "s3:*" |
| 52 | ] |
| 53 | |
| 54 | resources = [ |
| 55 | aws_s3_bucket.archeologist.arn, |
| 56 | "${aws_s3_bucket.archeologist.arn}/*" |
| 57 | ] |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | # This is the role that is given to the AWS Identity Center users |
| 62 | resource "aws_iam_policy" "archologist" { |
| 63 | provider = aws.us |
| 64 | |
| 65 | name = "archeologist" |
| 66 | description = "used by the S3 archeologists" |
| 67 | |
| 68 | policy = data.aws_iam_policy_document.archaeologist.json |
| 69 | } |
| 70 | |
| 71 | # Prepare this role to be attached to the EC2 instance |
| 72 | resource "aws_iam_role" "archeologist-worker" { |
| 73 | provider = aws.us |
| 74 | |
| 75 | name = "archeologist-worker" |
| 76 | |
| 77 | assume_role_policy = <<EOF |
| 78 | { |
| 79 | "Version": "2012-10-17", |
| 80 | "Statement": [ |
| 81 | { |
| 82 | "Action": "sts:AssumeRole", |
| 83 | "Principal": { |
| 84 | "Service": "ec2.amazonaws.com" |
| 85 | }, |
| 86 | "Effect": "Allow", |
| 87 | "Sid": "" |
| 88 | } |
| 89 | ] |
| 90 | } |
| 91 | EOF |
| 92 | } |
| 93 | |
| 94 | resource "aws_iam_role_policy" "archeologist-worker" { |
| 95 | provider = aws.us |
| 96 | |
| 97 | name = "archeologist-worker" |
| 98 | role = aws_iam_role.archeologist-worker.id |
| 99 | |
| 100 | # The EC2 instance gets the same policy as the users |
| 101 | policy = aws_iam_policy.archologist.policy |
| 102 | } |
| 103 | |
| 104 | resource "aws_iam_instance_profile" "archeologist" { |
| 105 | provider = aws.us |
| 106 | |
| 107 | name = "archeologist-worker" |
| 108 | role = aws_iam_role.archeologist-worker.name |
| 109 | # Make sure the role is attached before continuing |
| 110 | depends_on = [aws_iam_role_policy.archeologist-worker] |
| 111 | } |
| 112 | |
| 113 | resource "aws_key_pair" "edef" { |
| 114 | provider = aws.us |
| 115 | |
| 116 | key_name = "edef-key" |
| 117 | public_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGu/CiEnmhIthp0XaGhU1cB18t6Ta/51k1/7EeIzKFwm" |
| 118 | } |
| 119 | |
| 120 | resource "aws_instance" "archeologist" { |
| 121 | provider = aws.us |
| 122 | |
| 123 | ami = "ami-07df5833f04703a2a" # "23.05".us-east-1.x86_64-linux.hvm-ebs |
| 124 | associate_public_ip_address = true |
| 125 | iam_instance_profile = aws_iam_instance_profile.archeologist.id |
| 126 | instance_type = "r5a.2xlarge" |
| 127 | key_name = aws_key_pair.edef.key_name |
| 128 | subnet_id = "subnet-1eb22868" # default subnet us-east-1c |
| 129 | |
| 130 | root_block_device { |
| 131 | volume_size = "1024" # GB |
| 132 | } |
| 133 | |
| 134 | vpc_security_group_ids = [ |
| 135 | "sg-51d35d29", # default |
| 136 | "sg-b2ee60ca", # public-ssh |
| 137 | ] |
| 138 | |
| 139 | tags = { |
| 140 | Name = "archeologist-workspace" |
| 141 | } |
| 142 | } |