@@ -52,6 +52,11 @@ variable "cloud_init_complete_signal" {
5252 default = " /tmp/rke2-cloud-init-complete"
5353}
5454
55+ variable "aws_provider_region" {
56+ description = " AWS region for AWS CLI operations"
57+ type = string
58+ }
59+
5560# Generate a random string (token)
5661resource "random_string" "K8S_TOKEN" {
5762 length = 32 # Length of the token
@@ -175,19 +180,25 @@ resource "null_resource" "rke2-cloud-init-wait" {
175180 provisioner "local-exec" {
176181 command = << EOF
177182 echo "Waiting for RKE2 Cloud-Init setup to complete on Control Plane Node..."
183+
184+ # Set AWS region
185+ export AWS_DEFAULT_REGION=${ var . aws_provider_region }
186+
178187 INSTANCE_ID=$(aws ec2 describe-instances \
188+ --region ${ var . aws_provider_region } \
179189 --filters "Name=private-ip-address,Values=${ local . CONTROL_PLANE_NODE_1 } " \
180190 --query "Reservations[0].Instances[0].InstanceId" \
181191 --output text)
182192
183193 echo "Instance ID: $INSTANCE_ID"
184194
185195 # Wait for cloud-init to complete (max 30 minutes)
186- for i in {1..60} ; do
196+ for i in $(seq 1 60) ; do
187197 echo "Check $i/60: Waiting for cloud-init completion..."
188198
189199 # Check if cloud-init is done
190200 STATUS=$(aws ssm send-command \
201+ --region ${ var . aws_provider_region } \
191202 --instance-ids $INSTANCE_ID \
192203 --document-name "AWS-RunShellScript" \
193204 --parameters 'commands=["cloud-init status"]' \
@@ -197,6 +208,7 @@ resource "null_resource" "rke2-cloud-init-wait" {
197208 if [ "$STATUS" != "failed" ]; then
198209 sleep 10
199210 RESULT=$(aws ssm get-command-invocation \
211+ --region ${ var . aws_provider_region } \
200212 --command-id $STATUS \
201213 --instance-id $INSTANCE_ID \
202214 --query "StandardOutputContent" \
@@ -207,6 +219,7 @@ resource "null_resource" "rke2-cloud-init-wait" {
207219
208220 # Check if RKE2 setup completed
209221 RKE2_CHECK=$(aws ssm send-command \
222+ --region ${ var . aws_provider_region } \
210223 --instance-ids $INSTANCE_ID \
211224 --document-name "AWS-RunShellScript" \
212225 --parameters 'commands=["systemctl is-active rke2-server || systemctl is-active rke2-agent"]' \
@@ -216,6 +229,7 @@ resource "null_resource" "rke2-cloud-init-wait" {
216229 if [ "$RKE2_CHECK" != "failed" ]; then
217230 sleep 5
218231 RKE2_STATUS=$(aws ssm get-command-invocation \
232+ --region ${ var . aws_provider_region } \
219233 --command-id $RKE2_CHECK \
220234 --instance-id $INSTANCE_ID \
221235 --query "StandardOutputContent" \
@@ -451,12 +465,16 @@ resource "null_resource" "download-k8s-kubeconfig" {
451465if [ "${ var . use_cloud_init } " = "true" ]; then
452466 # Cloud-Init approach: Use AWS CLI to download kubeconfig
453467 echo "Downloading kubeconfig for ${ each . key } via AWS CLI..."
468+ export AWS_DEFAULT_REGION=${ var . aws_provider_region }
469+
454470 INSTANCE_ID=$(aws ec2 describe-instances \
471+ --region ${ var . aws_provider_region } \
455472 --filters "Name=private-ip-address,Values=${ each . value } " \
456473 --query "Reservations[0].Instances[0].InstanceId" \
457474 --output text)
458475
459476 COMMAND_ID=$(aws ssm send-command \
477+ --region ${ var . aws_provider_region } \
460478 --instance-ids $INSTANCE_ID \
461479 --document-name "AWS-RunShellScript" \
462480 --parameters 'commands=["sudo cat /home/ubuntu/.kube/${ each . key } .yaml 2>/dev/null || sudo cat /etc/rancher/rke2/rke2.yaml"]' \
@@ -465,6 +483,7 @@ if [ "${var.use_cloud_init}" = "true" ]; then
465483
466484 sleep 10
467485 aws ssm get-command-invocation \
486+ --region ${ var . aws_provider_region } \
468487 --command-id $COMMAND_ID \
469488 --instance-id $INSTANCE_ID \
470489 --query "StandardOutputContent" \
@@ -496,12 +515,16 @@ resource "null_resource" "download-kubectl-file" {
496515if [ "${ var . use_cloud_init } " = "true" ]; then
497516 # Cloud-Init approach: Download kubectl via AWS CLI
498517 echo "Downloading kubectl binary via AWS CLI..."
518+ export AWS_DEFAULT_REGION=${ var . aws_provider_region }
519+
499520 INSTANCE_ID=$(aws ec2 describe-instances \
521+ --region ${ var . aws_provider_region } \
500522 --filters "Name=private-ip-address,Values=${ local . CONTROL_PLANE_NODE_1 } " \
501523 --query "Reservations[0].Instances[0].InstanceId" \
502524 --output text)
503525
504526 COMMAND_ID=$(aws ssm send-command \
527+ --region ${ var . aws_provider_region } \
505528 --instance-ids $INSTANCE_ID \
506529 --document-name "AWS-RunShellScript" \
507530 --parameters 'commands=["sudo cp /var/lib/rancher/rke2/bin/kubectl /tmp/kubectl && sudo chmod 755 /tmp/kubectl"]' \
@@ -510,6 +533,7 @@ if [ "${var.use_cloud_init}" = "true" ]; then
510533
511534 sleep 5
512535 COMMAND_ID2=$(aws ssm send-command \
536+ --region ${ var . aws_provider_region } \
513537 --instance-ids $INSTANCE_ID \
514538 --document-name "AWS-RunShellScript" \
515539 --parameters 'commands=["base64 /tmp/kubectl"]' \
@@ -518,6 +542,7 @@ if [ "${var.use_cloud_init}" = "true" ]; then
518542
519543 sleep 10
520544 aws ssm get-command-invocation \
545+ --region ${ var . aws_provider_region } \
521546 --command-id $COMMAND_ID2 \
522547 --instance-id $INSTANCE_ID \
523548 --query "StandardOutputContent" \
0 commit comments