Skip to content

Commit

Permalink
fix: resizing the output solution (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
tpryan committed Mar 29, 2023
1 parent 6f8df8d commit 3a90f59
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 16 deletions.
17 changes: 16 additions & 1 deletion .deploystack/deploystack.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

title: Load Balanced VMs
name: terraform-google-load-balanced-vms
collect_project: true
Expand All @@ -19,4 +33,5 @@ products:
description: |
This solution deploys a group of VMs managed by a load balancer. The VMs serve
a static website. It also utilizes Auto Scaling and Auto healing to respond to
demand and to make cluster more resilient.
demand and to make cluster more resilient.
3 changes: 2 additions & 1 deletion .deploystack/messages/description.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
This solution deploys a group of VMs managed by a load balancer. The VMs serve
a static website. It also utilizes Auto Scaling and Auto healing to respond to
demand and to make cluster more resilient.
demand and to make cluster more resilient.

25 changes: 20 additions & 5 deletions .deploystack/scripts/postinstall.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
endpoint=$(terraform output load_balancer_endpoint)
#!/bin/bash
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

endpoint=$(terraform output load_balancer_endpoint)
endpoint=${endpoint/\"/}
endpoint=${endpoint/\"/}
echo "Waiting for the client to be active"

attempt_counter=0
max_attempts=100

until $(curl --output /dev/null --silent --head --fail $endpoint); do
until curl --output /dev/null --silent --head --fail "$endpoint"; do
if [ ${attempt_counter} -eq ${max_attempts} ];then
repo=$(git config --get remote.origin.url)
echo "Max attempts reached."
echo "Solution was not successfully installed."
echo
echo
echo "If the problem persists, please file an issue with the Github repo:"
echo "${repo/.git/}/issues"
exit 1
fi

printf '.'
attempt_counter=$(($attempt_counter+1))
attempt_counter=$((attempt_counter+1))
sleep 5
done

echo "Success, architecture is ready."
echo "To see for yourself, go check out:"
echo "$endpoint"
echo "$endpoint"
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ module "load_balanced_vms" {
}
```

## Open in Cloud Shell
An other way of using this Terraform solution is with DeployStack, which will
ask for setting options in Cloud Shell.

<a href="https://shell.cloud.google.com/cloudshell/editor?show=terminal&cloudshell_git_repo=https://github.com/GoogleCloudPlatform/terraform-google-load-balanced-vms&cloudshell_image=gcr.io%2Fds-artifacts-cloudshell%2Fdeploystack_custom_image" target="_new">
<img alt="Open in Cloud Shell" src="https://gstatic.com/cloudssh/images/open-btn.svg">
</a>

Functional examples are included in the
[examples](./examples/) directory.

Expand Down
28 changes: 19 additions & 9 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
*/

locals {
default_machine_type = "e2-medium"
subnet_name = "${var.deployment_name}-subnet-01"
custom_network = var.network_id != ""
network_id = local.custom_network ? var.network_id : module.vpc[0].network_id
subnet_self_link = var.subnet_self_link != "" ? var.subnet_self_link : module.vpc[0].subnets["${var.region}/${local.subnet_name}"].self_link
network_project_id = var.network_project_id != "" ? var.network_project_id : var.project_id
exemplar_machine_type = "e2-medium"
node_machine_type = "e2-micro"
subnet_name = "${var.deployment_name}-subnet-01"
custom_network = var.network_id != ""
network_id = local.custom_network ? var.network_id : module.vpc[0].network_id
subnet_self_link = var.subnet_self_link != "" ? var.subnet_self_link : module.vpc[0].subnets["${var.region}/${local.subnet_name}"].self_link
network_project_id = var.network_project_id != "" ? var.network_project_id : var.project_id
}
# Enabling services in your GCP project
Expand Down Expand Up @@ -83,13 +84,22 @@ data "local_file" "index" {
# Create Instance Exemplar on which to base Managed VMs
resource "google_compute_instance" "exemplar" {
name = "${var.deployment_name}-exemplar"
machine_type = local.default_machine_type
machine_type = local.exemplar_machine_type
zone = var.zone
project = var.project_id
labels = var.labels

tags = ["http-server", "private-ssh"]
metadata_startup_script = "apt-get update -y \n apt-get install nginx -y \n printf '${data.local_file.index.content}' | tee /var/www/html/index.html \n chgrp root /var/www/html/index.html \n chown root /var/www/html/index.html \n chmod +r /var/www/html/index.html"
metadata_startup_script = <<EOF
apt-get update -y
apt-get install nginx -y
printf '${data.local_file.index.content}' | tee /var/www/html/index.html
chgrp root /var/www/html/index.html
chown root /var/www/html/index.html
chmod +r /var/www/html/index.html
sleep 300
shutdown -h now
EOF

boot_disk {
auto_delete = true
Expand Down Expand Up @@ -146,7 +156,7 @@ resource "google_compute_instance_template" "main" {
metadata_startup_script = "sed -i.bak \"s/{{NODENAME}}/$HOSTNAME/\" /var/www/html/index.html"

instance_description = "${var.deployment_name} node"
machine_type = local.default_machine_type
machine_type = local.node_machine_type
can_ip_forward = false

// Create a new boot disk from an image
Expand Down

0 comments on commit 3a90f59

Please sign in to comment.