-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/env bash | ||
set -o errexit | ||
set -o nounset | ||
set -o pipefail | ||
|
||
PROJECT_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
source "$PROJECT_ROOT"/env | ||
source "$PROJECT_ROOT/../"/common/functions.sh | ||
|
||
gcloud functions delete random-cache | ||
gcloud beta compute networks vpc-access connectors delete cache-connector --region us-central1 | ||
gcloud compute instances delete redis-cache --zone ${ZONE} | ||
gcloud compute firewall-rules delete allow-redis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env bash | ||
set -o errexit | ||
set -o nounset | ||
set -o pipefail | ||
set -x | ||
|
||
# Reference | ||
# https://cloud.google.com/functions/docs/connecting-vpc | ||
# https://medium.com/google-cloud/connecting-cloud-functions-with-compute-engine-using-serverless-vpc-access-79c5cd7420c7 | ||
|
||
PROJECT_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
source "$PROJECT_ROOT"/env | ||
source "$PROJECT_ROOT/../"/common/functions.sh | ||
|
||
|
||
enable_project_api ${PROJECT_ID} vpcaccess.googleapis.com | ||
|
||
gcloud compute firewall-rules create allow-redis --network ${NETWORK} --allow tcp:6379 | ||
gcloud compute instances create-with-container redis-cache \ | ||
--machine-type=f1-micro \ | ||
--container-image=marketplace.gcr.io/google/redis4 \ | ||
--tags=allow-redis \ | ||
--zone=${ZONE} \ | ||
--network-interface=network=${NETWORK},subnet=${SUBNET},\ | ||
no-address,private-network-ip=10.0.50.9 | ||
|
||
gcloud beta compute networks vpc-access connectors create \ | ||
cache-connector \ | ||
--network ${NETWORK} \ | ||
--region us-central1 \ | ||
--range 172.16.16.240/28 | ||
|
||
|
||
|
||
gcloud projects add-iam-policy-binding $PROJECT_ID \ | ||
--member=serviceAccount:service-$PROJECT_NUMBER@gcf-admin-robot.iam.gserviceaccount.com \ | ||
--role=roles/viewer | ||
|
||
gcloud projects add-iam-policy-binding $PROJECT_ID \ | ||
--member=serviceAccount:service-$PROJECT_NUMBER@gcf-admin-robot.iam.gserviceaccount.com \ | ||
--role=roles/compute.networkUser | ||
|
||
VPC_CONNECTOR=projects/${PROJECT_ID}/locations/us-central1/connectors/cache-connector | ||
|
||
gcloud beta functions deploy random-cache --entry-point main \ | ||
--runtime python37 \ | ||
--trigger-http \ | ||
--region us-central1 \ | ||
--vpc-connector $VPC_CONNECTOR \ | ||
--set-env-vars REDIS_HOST=10.0.50.9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
REGION=us-west1 | ||
ZONE=us-west1-b | ||
NETWORK=custom-vpc-1 | ||
SUBNET=subnet-oregon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import os | ||
import datetime | ||
import random | ||
|
||
import redis | ||
|
||
r = redis.StrictRedis(host=os.environ['REDIS_HOST'], decode_responses=True) | ||
|
||
|
||
def main(request=None): | ||
cache_key = datetime.datetime.now().minute | ||
val = r.get(cache_key) | ||
if not val: | ||
val = random.random() | ||
out = f'set value: {val}' | ||
r.set(cache_key, val) | ||
else: | ||
out = f'value from cache: {val}' | ||
#r.delete(cache_key) | ||
return out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
redis==3.3.6 |