@@ -17,30 +17,14 @@ provider "kubernetes" {} # make sure you set the env variable KUBE_CONFIG_PATH t
1717provider "helm" {} # make sure you set the env variable KUBE_CONFIG_PATH to local_file_path (file_path variable)
1818
1919provider "rancher2" {
20- alias = " authenticate"
21- bootstrap = true
2220 api_url = " https://${ local . domain } .${ local . zone } "
23- timeout = " 300s"
24- }
25-
26- resource "rancher2_bootstrap" "authenticate" {
27- provider = rancher2. authenticate
28- initial_password = module. rancher . admin_password
29- password = module. rancher . admin_password
30- token_update = true
31- token_ttl = 86400 # 24 hours
32- }
33-
34- provider "rancher2" {
35- alias = " default"
36- api_url = " https://${ local . domain } .${ local . zone } "
37- token_key = rancher2_bootstrap. authenticate . token
21+ token_key = module. rancher . admin_token
3822 timeout = " 300s"
3923}
4024
4125locals {
4226 identifier = var. identifier
43- example = " basic "
27+ example = " downstream "
4428 project_name = " tf-${ substr (md5 (join (" -" , [local . example , local . identifier ])), 0 , 5 )} "
4529 username = local. project_name
4630 domain = local. project_name
@@ -56,11 +40,12 @@ locals {
5640 os = " sle-micro-61"
5741 aws_access_key_id = var. aws_access_key_id
5842 aws_secret_access_key = var. aws_secret_access_key
59- aws_session_token = var. aws_session_token
6043 aws_region = var. aws_region
44+ aws_session_token = var. aws_session_token
45+ aws_instance_type = " m5.large"
46+ node_count = 3
6147 email = (var. email != " " ? var. email : " ${ local . identifier } @${ local . zone } " )
6248 acme_server_url = " https://acme-v02.api.letsencrypt.org"
63- private_ip = replace (module. rancher . private_endpoint , " http://" , " " )
6449}
6550
6651data "http" "myip" {
@@ -114,111 +99,46 @@ module "rke2_image" {
11499 version = " v1.4.0"
115100 server_use_strategy = " skip"
116101 image_use_strategy = " find"
117- image_type = local. os
102+ image_type = local. os # this is not required to match Rancher, it just seemed easier in this example
118103}
119104
120- # this adds the private (10.) IP to the domain
121- # the private IP communicates to the agents where to find Rancher
122- resource "aws_route53_record" "modified" {
105+ # you can add this one multiple times, or use a loop to deploy multiple clusters
106+ module "downstream" {
123107 depends_on = [
124108 module . rancher ,
109+ module . rke2_image ,
125110 ]
126- zone_id = module. rancher . domain_object . zone_id
127- name = module. rancher . domain_object . name
128- type = module. rancher . domain_object . type
129- ttl = 30
130- records = concat ([local . private_ip ], tolist (module. rancher . domain_object . records ))
131- allow_overwrite = true
132- }
133-
134- resource "rancher2_machine_config_v2" "aio" {
135- depends_on = [
136- rancher2_bootstrap . authenticate ,
137- module . rancher ,
138- aws_route53_record . modified ,
139- ]
140- provider = rancher2. default
141- generate_name = " all-in-one-config"
142- amazonec2_config {
143- ami = module. rke2_image . image . id
144- region = local. aws_region
145- security_group = [module . rancher . security_group . name ]
146- subnet_id = module. rancher . subnets [keys (module. rancher . subnets )[0 ]]. id
147- vpc_id = module. rancher . vpc . id
148- zone = replace ( # it is looking for just the last letter of the availability zone, eg. for us-west-2a it just wants 'a'
149- module. rancher . subnets [keys (module. rancher . subnets )[0 ]]. availability_zone ,
150- local. aws_region ,
151- " "
152- )
153- session_token = trimspace (chomp (local. aws_session_token ))
154- instance_type = " m5.large"
155- ssh_user = " ec2-user"
156- userdata = <<- EOT
157- #cloud-config
158- bootcmd:
159- - echo ${ local . private_ip } ${ local . domain } .${ local . zone } >> /etc/hosts
160- EOT
161- tags = join (" ," , [" Id" , local . identifier , " Owner" , local . owner ])
111+ source = " ./modules/downstream"
112+ # general
113+ name = " tf-all-in-one-config"
114+ identifier = local. identifier
115+ owner = local. owner
116+ # aws access
117+ aws_access_key_id = local. aws_access_key_id
118+ aws_secret_access_key = local. aws_secret_access_key
119+ aws_session_token = trimspace (chomp (local. aws_session_token ))
120+ aws_region = local. aws_region
121+ aws_region_letter = replace (
122+ module. rancher . subnets [keys (module. rancher . subnets )[0 ]]. availability_zone ,
123+ local. aws_region ,
124+ " "
125+ )
126+ # aws project info
127+ vpc_id = module. rancher . vpc . id
128+ security_group_id = module. rancher . security_group . id
129+ load_balancer_security_groups = module. rancher . load_balancer_security_groups
130+ subnet_id = module. rancher . subnets [keys (module. rancher . subnets )[0 ]]. id
131+ # node info
132+ aws_instance_type = local. aws_instance_type
133+ ami_id = module. rke2_image . image . id
134+ ami_ssh_user = module. rke2_image . image . user
135+ ami_admin_group = module. rke2_image . image . admin_group
136+ node_count = local. node_count
137+ direct_node_access = {
138+ runner_ip = local.runner_ip
139+ ssh_access_key = local.key
140+ ssh_access_user = local.project_name
162141 }
163- }
164-
165- resource "terraform_data" "patch_machine_config" {
166- depends_on = [
167- module . rancher ,
168- aws_route53_record . modified ,
169- rancher2_machine_config_v2 . aio ,
170- ]
171- triggers_replace = {
172- config = rancher2_machine_config_v2.aio.id
173- }
174- provisioner "local-exec" {
175- command = <<- EOT
176- ${ path . module } /addKeyToMachineTemplate.sh "${ local . aws_access_key_id } " "${ local . aws_secret_access_key } "
177- EOT
178- }
179- }
180-
181- resource "rancher2_cluster_v2" "rke2_cluster" {
182- depends_on = [
183- rancher2_bootstrap . authenticate ,
184- module . rancher ,
185- aws_route53_record . modified ,
186- rancher2_machine_config_v2 . aio ,
187- terraform_data . patch_machine_config ,
188- ]
189- provider = rancher2. default
190- name = " ${ local . project_name } -s1-cluster"
191- kubernetes_version = local. rke2_version
192- enable_network_policy = true
193- rke_config {
194- machine_pools {
195- name = " mp1"
196- control_plane_role = true
197- etcd_role = true
198- worker_role = true
199- quantity = 1
200- machine_config {
201- kind = rancher2_machine_config_v2. aio . kind
202- name = rancher2_machine_config_v2. aio . name
203- }
204- }
205- rotate_certificates {
206- generation = 1
207- }
208- }
209- timeouts {
210- create = " 120m" # 2 hours
211- }
212- }
213-
214- resource "rancher2_cluster_sync" "sync" {
215- depends_on = [
216- rancher2_bootstrap . authenticate ,
217- module . rancher ,
218- aws_route53_record . modified ,
219- rancher2_machine_config_v2 . aio ,
220- rancher2_cluster_v2 . rke2_cluster ,
221- ]
222- provider = rancher2. default
223- cluster_id = rancher2_cluster_v2. rke2_cluster . cluster_v1_id
142+ # rke2 info
143+ rke2_version = local. rke2_version
224144}
0 commit comments