|
| 1 | +# vim:ts=2:sts=2:sw=2:et |
| 2 | +# |
| 3 | +# Author: Hari Sekhon |
| 4 | +# Date: 2020-10-21 11:19:06 +0100 (Wed, 21 Oct 2020) |
| 5 | +# |
| 6 | +# https://github.com/HariSekhon/DevOps-Python-tools |
| 7 | +# |
| 8 | +# License: see accompanying Hari Sekhon LICENSE file |
| 9 | +# |
| 10 | +# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish |
| 11 | +# |
| 12 | +# https://www.linkedin.com/in/HariSekhon |
| 13 | +# |
| 14 | + |
| 15 | +# ============================================================================ # |
| 16 | +# S e r v e r l e s s |
| 17 | +# ============================================================================ # |
| 18 | + |
| 19 | +# Deploys the Google Cloud Function with a name <service>-<stage>-<func> |
| 20 | +# |
| 21 | +# eg. cloud-sql-backups-production-main |
| 22 | + |
| 23 | +# Requires the same permissions as the simpler adjacent deploy.sh script but also: |
| 24 | +# |
| 25 | +# Deployment Manager Editor |
| 26 | +# Storage Admin (Storage Object Admin is not enough as it needs to create staging buckets and you'll get weird errors otherwise) |
| 27 | +# |
| 28 | +# for the credential file's service account which is also used to run the Cloud Function |
| 29 | + |
| 30 | +# Check generated config after environment variable interpolation: |
| 31 | +# |
| 32 | +# cd "$(dirname $0)" && serverless print |
| 33 | +# |
| 34 | +# My advanced vimrc has a hotkey for this: |
| 35 | +# |
| 36 | +# https://github.com/HariSekhon/DevOps-Bash-tools/blob/master/.vimrc |
| 37 | + |
| 38 | +frameworkVersion: '2' |
| 39 | +plugins: |
| 40 | + - serverless-google-cloudfunctions |
| 41 | + |
| 42 | +# to prevent: |
| 43 | +# |
| 44 | +# Serverless: Configuration warning: Unrecognized provider 'google' |
| 45 | +# Serverless: |
| 46 | +# Serverless: You're relying on provider plugin which doesn't provide a validation schema for its config. |
| 47 | +configValidationMode: off |
| 48 | +# after the GCP plugin matures, switch to |
| 49 | +#configValidationMode: error |
| 50 | + |
| 51 | +service: cloud-sql-backups |
| 52 | +provider: |
| 53 | + name: google |
| 54 | + stage: production |
| 55 | + runtime: python37 |
| 56 | + region: ${env:GOOGLE_REGION, "europe-west1"} |
| 57 | + project: ${env:GOOGLE_PROJECT_ID} |
| 58 | + |
| 59 | + # https://serverless.com/framework/docs/providers/google/guide/credentials/ |
| 60 | + # |
| 61 | + # path to credentials file needs to be absolute |
| 62 | + # |
| 63 | + # download a credentials file for the service account: |
| 64 | + # |
| 65 | + # https://cloud.google.com/iam/docs/creating-managing-service-account-keys#iam-service-account-keys-create-gcloud |
| 66 | + # |
| 67 | + # mkdir -pv ~/.gcloud && gcloud iam service-accounts keys create ~/.gcloud/cloud-function-sql-backup-keyfile.json --iam-account "cloud-function-sql-backup@$(gcloud config list --format="get(core.project)").iam.gserviceaccount.com" |
| 68 | + # |
| 69 | + credentials: ~/.gcloud/cloud-function-sql-backup-keyfile.json |
| 70 | + # |
| 71 | + # or use personal creds - see $GOOGLE_APPLICATION_CREDENTIALS or generate: |
| 72 | + # |
| 73 | + # gcloud auth application-default login # [ --client-id-file=~/.gcloud/keyfile.json ] |
| 74 | + # |
| 75 | + #credentials: ~/.config/gcloud/application_default_credentials.json |
| 76 | + #credentials: ${env:HOME}/.config/gcloud/application_default_credentials.json |
| 77 | + #credentials: /Users/harisekhon/.config/gcloud/application_default_credentials.json |
| 78 | + |
| 79 | +# https://www.serverless.com/framework/docs/providers/google/guide/packaging/ |
| 80 | +package: |
| 81 | + exclude: |
| 82 | + ## needs more granular excluding in production as only the serverless provider npm |
| 83 | + ## package should be excluded (and not the whole node_modules directory) |
| 84 | + #- node_modules/** |
| 85 | + #- deploy.sh |
| 86 | + #- test/** |
| 87 | + #- tests/** |
| 88 | + #- .gitignore |
| 89 | + #- .gcloudignore |
| 90 | + #- .git/** |
| 91 | + #- package.json |
| 92 | + #- package-lock.json |
| 93 | + #- README.md |
| 94 | + # XXX: exclude all hidden files and directories |
| 95 | + - .* |
| 96 | + # XXX: more robust to only whitelist include as per .dockerignore best practice too (unfortunately not supported in .gcloudignore) |
| 97 | + - ./** |
| 98 | + include: |
| 99 | + - main.py |
| 100 | + - requirements.txt |
| 101 | + # or specify your own zipfile and skip packaging: |
| 102 | + #artifact: path/to/my-artifact.zip |
| 103 | + |
| 104 | +# https://www.serverless.com/framework/docs/providers/google/guide/functions/ |
| 105 | +# |
| 106 | +# use an array of includes for bigger serverless deployments of multiple functions |
| 107 | +#functions: |
| 108 | +# - ${file(../foo-functions.yml)} |
| 109 | +# - ${file(../bar-functions.yml)} |
| 110 | +# |
| 111 | +functions: |
| 112 | + #myfunc: |
| 113 | + # handler: http |
| 114 | + # events: |
| 115 | + # - http: path |
| 116 | + # NOTE: the following uses an "event" event (pubSub event in this case). |
| 117 | + # Please create the corresponding resources in the Google Cloud |
| 118 | + # before deploying this service through Serverless |
| 119 | + main: |
| 120 | + handler: main |
| 121 | + memorySize: 128 |
| 122 | + timeout: 60s |
| 123 | + events: |
| 124 | + - event: |
| 125 | + eventType: providers/cloud.pubsub/eventTypes/topic.publish |
| 126 | + #resource: projects/*/topics/my-topic |
| 127 | + resource: projects/${env:GOOGLE_PROJECT_ID}/topics/cloud-sql-backups |
| 128 | +# you can define resources, templates etc. the same way you would in a |
| 129 | +# Google Cloud deployment configuration |
| 130 | +resources: |
| 131 | + resources: |
| 132 | + # generates the bucket that the cloud function will send the backups to |
| 133 | + # there is another bucket also created for uploading the package (sls-cloud-sql-backups-production-1603283101338), not to be confused with this one |
| 134 | + - type: storage.v1.bucket |
| 135 | + name: ${env:GOOGLE_PROJECT_ID}-sql-backups |
| 136 | +# imports: |
| 137 | +# - path: my_template.jinja |
0 commit comments