Skip to content

Commit 8aa43f0

Browse files
committed
tofu: Add x86_64 support
- Although duplicating code isn't ideal, tofu doesn't support managing resource names and global variables values using variables as declaration very well. For example: We can not do: variable "project" { type = string default = "coreos-${var.arch}-builder" } What creates limitation to merge aarch64 and x86_64. - As a result, add x86_64 now - The goal is to find an efficient way to merge both architectures in the future, since it shares the same code. Signed-off-by: Renata Ravanelli <rravanel@redhat.com>
1 parent f73cee0 commit 8aa43f0

File tree

3 files changed

+282
-0
lines changed

3 files changed

+282
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# OpenTofu
2+
3+
OpenTofu is a Terraform fork, is an open-source infrastructure as code (IaC) tool
4+
lets you define both cloud and on-prem resources in human-readable configuration files
5+
that you can version, reuse, and share.
6+
7+
To proceed with the next steps, ensure that 'tofu' is installed on your system.
8+
See: https://github.com/opentofu/opentofu/releases
9+
10+
## Before starting
11+
12+
### AWS credentials
13+
14+
```bash
15+
# Add your credentials to the environment.
16+
# Be aware for x86_64 the region is us-east-2
17+
HISTCONTROL='ignoreboth'
18+
export AWS_DEFAULT_REGION=us-east-2
19+
export AWS_ACCESS_KEY_ID=XXXX
20+
export AWS_SECRET_ACCESS_KEY=YYYYYYYY
21+
```
22+
23+
Make sure your AMI user has access to this policies:
24+
25+
```json
26+
{
27+
"Version": "2012-10-17",
28+
"Statement": [
29+
{
30+
"Effect": "Allow",
31+
"Action": "ec2:*",
32+
"Resource": "*"
33+
}
34+
]
35+
}
36+
```
37+
38+
### TF vars via environment variables
39+
40+
If you'd like to override the target distro (defaults to `fcos`) you
41+
can:
42+
43+
```
44+
export TF_VAR_distro=rhcos
45+
```
46+
47+
If you are deploying RHCOS you'll need to define variables for splunk configuration:
48+
49+
```
50+
export TF_VAR_splunk_hostname=...
51+
export TF_VAR_splunk_sidecar_repo=...
52+
export TF_VAR_itpaas_splunk_repo=...
53+
```
54+
55+
## Running tofu
56+
```bash
57+
# To begin using it, run 'init' within this directory.
58+
tofu init
59+
# If you don't intend to make any changes to the code, simply run it:
60+
tofu apply
61+
# If you plan to make changes to the code as modules/plugins, go ahead and run it:
62+
tofu init -upgrade
63+
# To destroy it run:
64+
tofu destroy -target aws_instance.coreos-x86_64-builder
65+
```
66+
## Generating additional resources with unique names
67+
68+
When rerunning the Tofu configuration any changes will be
69+
applied to the existing resources. If you intend to add a new
70+
resource with a different name, please be aware that TOFU doesn't
71+
support interpolation in resource names.
72+
73+
To achieve this, you'll need to manually edit the resource name
74+
in the Tofu configuration.
75+
76+
```
77+
resource "aws_instance" "coreos-x86_64-builder"
78+
```
79+
Make sure the resource name is unique, in this case
80+
if I already have a resource named `coreos-x86_64-builder`,
81+
I need to change it to `coreos-x86_64-devel-builder` for example.
82+
83+
I may also want to update the project var:
84+
85+
```
86+
variable "project" {
87+
type = string
88+
default = "coreos-x86_64-devel-builder"
89+
}
90+
```
91+
92+
After it, I can rerun `tofu apply`.
93+
94+
The same is validated to all resources types.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
resource "aws_vpc" "vpc" {
2+
count = var.distro == "fcos" ? 1 : 0
3+
cidr_block = "172.31.0.0/16"
4+
tags = {
5+
Name = "${var.project}-vpc"
6+
}
7+
}
8+
9+
resource "aws_internet_gateway" "gw" {
10+
count = var.distro == "fcos" ? 1 : 0
11+
vpc_id = aws_vpc.vpc[0].id
12+
}
13+
14+
data "aws_availability_zones" "azs" {
15+
state = "available"
16+
}
17+
18+
variable "private_subnet_cidrs" {
19+
type = list(string)
20+
description = "Private Subnet CIDR values"
21+
default = ["172.31.1.0/24", "172.31.2.0/24", "172.31.3.0/24", "172.31.4.0/24", "172.31.5.0/24", "172.31.6.0/24", "172.31.7.0/24", "172.31.8.0/24"]
22+
}
23+
24+
resource "aws_subnet" "private_subnets" {
25+
count = var.distro == "fcos" ? length(data.aws_availability_zones.azs.names) : 0
26+
vpc_id = aws_vpc.vpc[0].id
27+
cidr_block = element(var.private_subnet_cidrs, count.index)
28+
availability_zone = element(data.aws_availability_zones.azs.names, count.index)
29+
tags = {
30+
Name = "${var.project}-private-subnet-${count.index + 1}"
31+
}
32+
}
33+
34+
resource "aws_route_table" "internet_route" {
35+
count = var.distro == "fcos" ? 1 : 0
36+
vpc_id = aws_vpc.vpc[0].id
37+
route {
38+
cidr_block = "0.0.0.0/0"
39+
gateway_id = aws_internet_gateway.gw[0].id
40+
}
41+
tags = {
42+
Name = "${var.project}-ig"
43+
}
44+
}
45+
46+
resource "aws_main_route_table_association" "public-set-main-default-rt-assoc" {
47+
count = var.distro == "fcos" ? 1 : 0
48+
vpc_id = aws_vpc.vpc[0].id
49+
route_table_id = aws_route_table.internet_route[0].id
50+
}

0 commit comments

Comments
 (0)