|
| 1 | +terraform { |
| 2 | + required_providers { |
| 3 | + ct = { |
| 4 | + source = "poseidon/ct" |
| 5 | + version = "0.13.0" |
| 6 | + } |
| 7 | + aws = { |
| 8 | + source = "hashicorp/aws" |
| 9 | + version = "~> 5.0" |
| 10 | + } |
| 11 | + http = { |
| 12 | + source = "hashicorp/http" |
| 13 | + version = "2.1.0" |
| 14 | + } |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +provider "aws" {} |
| 19 | +provider "ct" {} |
| 20 | +provider "http" {} |
| 21 | + |
| 22 | +variable "project" { |
| 23 | + type = string |
| 24 | + default = "coreos-x86_64-builder" |
| 25 | +} |
| 26 | + |
| 27 | +# Which distro are we deploying a builder for? Override the |
| 28 | +# default by setting the env var: TF_VAR_distro=rhcos |
| 29 | +variable "distro" { |
| 30 | + type = string |
| 31 | + default = "fcos" |
| 32 | +} |
| 33 | +check "health_check_distro" { |
| 34 | + assert { |
| 35 | + condition = anytrue([ |
| 36 | + var.distro == "fcos", |
| 37 | + var.distro == "rhcos" |
| 38 | + ]) |
| 39 | + error_message = "Distro must be 'fcos' or 'rhcos'" |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +# Variables used for splunk deployment, which is only |
| 44 | +# for RHCOS builders. Define them in the environment with: |
| 45 | +# export TF_VAR_splunk_hostname=... |
| 46 | +# export TF_VAR_splunk_sidecar_repo=... |
| 47 | +# export TF_VAR_itpaas_splunk_repo=... |
| 48 | +variable "splunk_hostname" { |
| 49 | + type = string |
| 50 | + default = "" |
| 51 | +} |
| 52 | +variable "splunk_sidecar_repo" { |
| 53 | + type = string |
| 54 | + default = "" |
| 55 | +} |
| 56 | +variable "itpaas_splunk_repo" { |
| 57 | + type = string |
| 58 | + default = "" |
| 59 | +} |
| 60 | +# Check that if we are deploying a RHCOS builder the splunk |
| 61 | +# variables have been defined. |
| 62 | +check "health_check_rhcos_splunk_vars" { |
| 63 | + assert { |
| 64 | + condition = !(var.distro == "rhcos" && anytrue([ |
| 65 | + var.splunk_hostname == "", |
| 66 | + var.splunk_sidecar_repo == "" |
| 67 | + ])) |
| 68 | + error_message = "Must define splunk env vars for RCHOS builders" |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +locals { |
| 73 | + fcos_snippets = [ |
| 74 | + file("../../coreos-x86_64-builder.bu"), |
| 75 | + ] |
| 76 | + rhcos_snippets = [ |
| 77 | + file("../../coreos-x86_64-builder.bu"), |
| 78 | + templatefile("../../builder-splunk.bu", { |
| 79 | + SPLUNK_HOSTNAME = var.splunk_hostname |
| 80 | + SPLUNK_SIDECAR_REPO = var.splunk_sidecar_repo |
| 81 | + }) |
| 82 | + ] |
| 83 | +} |
| 84 | +data "ct_config" "butane" { |
| 85 | + strict = true |
| 86 | + content = file("../../builder-common.bu") |
| 87 | + snippets = var.distro == "rhcos" ? local.rhcos_snippets : local.fcos_snippets |
| 88 | +} |
| 89 | + |
| 90 | +data "aws_region" "aws_region" {} |
| 91 | + |
| 92 | +# Gather information about the AWS image for the current region |
| 93 | +data "http" "stream_metadata" { |
| 94 | + url = "https://builds.coreos.fedoraproject.org/streams/stable.json" |
| 95 | + |
| 96 | + request_headers = { |
| 97 | + Accept = "application/json" |
| 98 | + } |
| 99 | +} |
| 100 | +# Lookup the x86_64 AWS image for the current AWS region |
| 101 | +locals { |
| 102 | + ami = lookup(jsondecode(data.http.stream_metadata.body).architectures.x86_64.images.aws.regions, data.aws_region.aws_region.name).image |
| 103 | +} |
| 104 | + |
| 105 | +variable "rhcos_aws_vpc_prod" { |
| 106 | + description = "RHCOS Prod US East 2" |
| 107 | + default = "vpc-0e33d95334e362c7e" |
| 108 | +} |
| 109 | +variable "rhcos_aws_subnet_internal" { |
| 110 | + description = "RHCOS Prod US East 2 subnet" |
| 111 | + default = "subnet-02014b5e587d01fd2" |
| 112 | +} |
| 113 | +# If we are RHCOS we'll be using an already existing VPC/subnet rather |
| 114 | +# than the newly created one. |
| 115 | +locals { |
| 116 | + aws_vpc_id = var.distro == "rhcos" ? var.rhcos_aws_vpc_prod : aws_vpc.vpc[0].id |
| 117 | + aws_subnet_id = var.distro == "rhcos" ? var.rhcos_aws_subnet_internal : aws_subnet.private_subnets[0].id |
| 118 | +} |
| 119 | + |
| 120 | +resource "aws_instance" "coreos-x86_64-builder" { |
| 121 | + tags = { |
| 122 | + Name = "${var.project}-${formatdate("YYYYMMDD", timestamp())}" |
| 123 | + } |
| 124 | + ami = local.ami |
| 125 | + user_data = data.ct_config.butane.rendered |
| 126 | + instance_type = "t2.medium" |
| 127 | + vpc_security_group_ids = [aws_security_group.sg.id] |
| 128 | + subnet_id = local.aws_subnet_id |
| 129 | + root_block_device { |
| 130 | + volume_size = "50" |
| 131 | + volume_type = "gp3" |
| 132 | + } |
| 133 | + associate_public_ip_address = var.distro == "fcos" ? "true" : "false" |
| 134 | +} |
| 135 | + |
| 136 | +output "instance_ip_addr" { |
| 137 | + value = var.distro == "rhcos" ? aws_instance.coreos-x86_64-builder.private_ip : aws_instance.coreos-x86_64-builder.public_ip |
| 138 | +} |
0 commit comments