Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fragaLY/3120f7eb965c29fb0891629a06749d6b to your computer and use it in GitHub Desktop.
Save fragaLY/3120f7eb965c29fb0891629a06749d6b to your computer and use it in GitHub Desktop.
Google Cloud Challenge Labs
resource "google_compute_instance" "tf-instance-1" {
name = "tf-instance-1"
machine_type = "n1-standard-2"
zone = "us-east1-c"
allow_stopping_for_update = true
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "tf-vpc-376868"
subnetwork = "subnet-01"
}
}
resource "google_compute_instance" "tf-instance-2" {
name = "tf-instance-2"
machine_type = "n1-standard-2"
zone = "us-east1-c"
allow_stopping_for_update = true
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "tf-vpc-376868"
subnetwork = "subnet-02"
}
}
resource "google_storage_bucket" "storage-bucket" {
name = "tf-bucket-526577"
location = "US"
force_destroy = true
uniform_bucket_level_access = true
}
variable "region" {
description = "The region of GCP."
type = string
default = "us-east1"
}
variable "project_id" {
description = "The ID of the project in which to provision resources."
type = string
default = "qwiklabs-gcp-00-af4991fbea28"
}
variable "zone" {
description = "The zone of GCP."
type = string
default = "us-east1-c"
}
terraform {
backend "gcs" {
bucket = "tf-bucket-526577"
prefix = "terraform/state"
}
required_providers {
google = {
source = "hashicorp/google"
version = "3.55.0"
}
}
}
provider "google" {
project = var.project_id
region = var.region
zone = var.zone
}
module "instances" {
source = "./modules/instances"
}
module "storage" {
source = "./modules/storage"
}
module "vpc" {
source = "terraform-google-modules/network/google"
version = "3.4.0"
project_id = "qwiklabs-gcp-00-af4991fbea28"
network_name = "tf-vpc-376868"
routing_mode = "GLOBAL"
subnets = [
{
subnet_name = "subnet-01"
subnet_ip = "10.10.10.0/24"
subnet_region = "us-east1"
},
{
subnet_name = "subnet-02"
subnet_ip = "10.10.20.0/24"
subnet_region = "us-east1"
}
]
}
resource "google_compute_firewall" "tf-firewall" {
name = "tf-firewall"
network = "projects/qwiklabs-gcp-00-af4991fbea28/global/networks/tf-vpc-376868"
allow {
protocol = "tcp"
ports = ["80"]
}
source_ranges = ["0.0.0.0/0"]
}
#Task 1
export PROJECT_ID=$(gcloud config get-value project)
export ZONE="us-east1-b"
export MACHINE_NAME="nucleus-jumphost-298"
export MACHINE_TYPE="f1-micro"
export REGION="us-east1"
gcloud compute instances create $MACHINE_NAME --machine-type $MACHINE_TYPE --zone $ZONE
#Task 2
export PORT="8083"
gcloud config set compute/region us-east1
gcloud config set compute/zone us-east1-b
gcloud container clusters create nucleus-cluster
gcloud container clusters get-credentials nucleus-cluster
kubectl create deployment nucleus-app --image=gcr.io/google-samples/hello-app:2.0
kubectl expose deployment nucleus-app --type=LoadBalancer --port $PORT
kubectl get service
#Task 3
export FIREWALL_RULE_NAME="accept-tcp-rule-517"
#create a firewall rule
gcloud compute firewall-rules create $FIREWALL_RULE_NAME \
--network=default \
--action=allow \
--direction=ingress \
--source-ranges=0.0.0.0/0 \
--target-tags=allow-health-check \
--rules=tcp:80
#create an instance template
gcloud compute instance-templates create nucleus-backend-template \
--region=$REGION \
--network=default \
--subnet=default \
--tags=allow-health-check \
--machine-type=e2-medium \
--image-family=debian-11 \
--image-project=debian-cloud \
--metadata=startup-script="apt-get update
apt-get install -y nginx
service nginx start
sudo sed -i -- 's/nginx/Google Cloud Platform - '"\$HOSTNAME"'/' /var/www/html/index.nginx-debian.html"
#create an instance managed group
gcloud compute instance-groups managed create nucleus-backend-group \
--template=nucleus-backend-template --size=2 --zone=$ZONE
#create a global static external IP address for loadbalancer
gcloud compute addresses create nucleus-lb-ipv4 \
--ip-version=IPV4 \
--global
#create a health check for the loadbalancer
gcloud compute health-checks create http nucleus-http-basic-check \
--port 80
#create a backend service (as a target pool) go to Google Cloud Console and set up ports and utilization metric
gcloud compute backend-services create nucleus-backend-service \
--protocol=HTTP \
--port-name=http \
--health-checks=nucleus-http-basic-check \
--global
#adding instance group as a backend to backend service
gcloud compute backend-services add-backend nucleus-backend-service \
--instance-group=nucleus-backend-group \
--instance-group-zone=$ZONE \
--global
#create a URL map to route the incoming requests to the default backend service
gcloud compute url-maps create nucleus-map-http \
--default-service nucleus-backend-service
#create a target HTTP proxy to route requests to your URL map
gcloud compute target-http-proxies create nucleus-http-lb-proxy \
--url-map nucleus-map-http
#create a global forwarding rule to route incoming requests to the proxy
gcloud compute forwarding-rules create nucleus-http-content-rule \
--address=nucleus-lb-ipv4 \
--global \
--target-http-proxy=nucleus-http-lb-proxy \
--ports=80
#link to badge: https://partner.cloudskillsboost.google/public_profiles/e9c751fc-07fb-4c0e-b835-71d09d355ec4/badges/2800934?utm_medium=social&utm_source=linkedin&utm_campaign=ql-social-share
export BUCKET_NAME="memories-bucket-29941"
export TOPIC_NAME="memories-topic-932"
export CLOUD_FUNCTION_NAME="memories-thumbnail-creator"
export PROJECT_ID=$(gcloud config get-value project)
gsutil mb -p $PROJECT_ID gs://$BUCKET_NAME
gcloud pubsub topics create $TOPIC_NAME
mkdir ~/tbf
cd ~/tbf
#insert there the code below
nano index.js
npm init
#insert there the code below
nano package.json
gcloud functions deploy $CLOUD_FUNCTION_NAME \
--stage-bucket $BUCKET_NAME \
--trigger-bucket $BUCKET_NAME \
--source=. \
--entry-point=thumbnail \
--runtime nodejs14 \
--allow-unauthenticated
# index.js file
/* globals exports, require */
//jshint strict: false
//jshint esversion: 6
"use strict";
const crc32 = require("fast-crc32c");
const { Storage } = require('@google-cloud/storage');
const gcs = new Storage();
const { PubSub } = require('@google-cloud/pubsub');
const imagemagick = require("imagemagick-stream");
exports.thumbnail = (event, context) => {
const fileName = event.name;
const bucketName = event.bucket;
const size = "64x64"
const bucket = gcs.bucket(bucketName);
const topicName = $TOPIC_NAME;
const pubsub = new PubSub();
if ( fileName.search("64x64_thumbnail") == -1 ){
// doesn't have a thumbnail, get the filename extension
var filename_split = fileName.split('.');
var filename_ext = filename_split[filename_split.length - 1];
var filename_without_ext = fileName.substring(0, fileName.length - filename_ext.length );
if (filename_ext.toLowerCase() == 'png' || filename_ext.toLowerCase() == 'jpg'){
// only support png and jpg at this point
console.log(`Processing Original: gs://${bucketName}/${fileName}`);
const gcsObject = bucket.file(fileName);
let newFilename = filename_without_ext + size + '_thumbnail.' + filename_ext;
let gcsNewObject = bucket.file(newFilename);
let srcStream = gcsObject.createReadStream();
let dstStream = gcsNewObject.createWriteStream();
let resize = imagemagick().resize(size).quality(90);
srcStream.pipe(resize).pipe(dstStream);
return new Promise((resolve, reject) => {
dstStream
.on("error", (err) => {
console.log(`Error: ${err}`);
reject(err);
})
.on("finish", () => {
console.log(`Success: ${fileName} → ${newFilename}`);
// set the content-type
gcsNewObject.setMetadata(
{
contentType: 'image/'+ filename_ext.toLowerCase()
}, function(err, apiResponse) {});
pubsub
.topic(topicName)
.publisher()
.publish(Buffer.from(newFilename))
.then(messageId => {
console.log(`Message ${messageId} published.`);
})
.catch(err => {
console.error('ERROR:', err);
});
});
});
}
else {
console.log(`gs://${bucketName}/${fileName} is not an image I can handle`);
}
}
else {
console.log(`gs://${bucketName}/${fileName} already has a thumbnail`);
}
};
# package.json file
{
"name": "thumbnails",
"version": "1.0.0",
"description": "Create Thumbnail of uploaded image",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"@google-cloud/pubsub": "^2.0.0",
"@google-cloud/storage": "^5.0.0",
"fast-crc32c": "1.0.4",
"imagemagick-stream": "4.1.1"
},
"devDependencies": {},
"engines": {
"node": ">=4.3.2"
}
}
#link to badge: https://partner.cloudskillsboost.google/public_profiles/e9c751fc-07fb-4c0e-b835-71d09d355ec4/badges/2798790?utm_medium=social&utm_source=linkedin&utm_campaign=ql-social-share
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment