API Key Authentication with Google Cloud API Gateway and Cloud Run Posted on February 10, 2024February 15, 2024 By Justin Table of Contents Toggle IntroductionWARNINGBasic StepsPrerequisitesGoogle Cloud SetupEnable Needed Google Cloud ServicesConsole MethodCommand Line MethodCreate a Google Cloud Service AccountConsole MethodCommand Line MethodBackend API CreationCreate a simple Python-based APImain.pyrequirements.txtDockerfilestart.shDeploy Code as a Docker Container to Cloud RunTestingReenable AuthenticationGrant Service Account PermissionsAPI Gateway SetupCreate API Gateway APITesting Your APICloud Run URLAPI Gateway URLGET TestsPOST TestsConclusion API Gateway Setup That that all that preliminary stuff is out of the way, we can get to the actual point of this post: setting up the Google API Gateway. Just to review, the API Gateway is going to sit between our users/clients and the Google Cloud Run service we created above. Instead of sending requests to the Cloud Run URLs we used previously, we’ll send them to the new URLs that we’ll create in the API Gateway setup process. And for authentication, we’ll set up the Gateway to use API keys. Granted, it’s not as secure as other options, but there are times when we need it. Before we get to creating that API key, we need to do four things: Create an API Gateway API Create an API Gateway Config Create an API Gateway Enable the API Gateway API Create API Gateway API Creating an API is pretty easy using the gcloud Google Command Line Interface: gcloud api-gateway apis create {{API_ID}} --project={{PROJECT_ID}} Where: {{API_ID}}: Name/ID of the new API Gateway API we want to create (e.g. demo-api). The name must: Have a maximum length of 63 characters Contain only lowercase letters, numbers, or dashes (no underscores) Not start with a dash {{PROJECT_ID}} = Name of the Google Cloud project you’re using for the tutorial (e.g. api-gateway-key-auth-demo) The process may take a few minutes to complete, so don’t worry if you find yourself staring at the “Waiting for API {{API_ID}} to be create…” message for a little bit. Once it completes, you can list all your APIs by using the gcloud api-gateway apis list –project={{PROJECT_ID}} command: and find details about a specific API using gcloud api-gateway apis describe {{API_ID}} –project={{PROJECT_ID}} : Take note of that managedService value. You’ll need it later. Pages: 1 2 3 4 5 6 7 8 9 10 11 12 Backend Stuff APICloud RunGCPPythonTutorial