Skip to content

candidosales/cloud-function-go

Repository files navigation

🐹 API in GO to create Cloud Function

An API for deploying Google Cloud Functions in Go.

preview

🎯 Features

  • Create, update or delete a Cloud Function;
  • Get a Cloud Function via function ID;
  • Set IAM Policy to enable unauthenticated users invoke the Cloud Function;

⚙️ Create a private key to access the GCP

For this method, you'll need to create a service account, and download a key.

  1. In the GCP Console, go to the Create service account key page;
  2. From the Service account drop-down list, select New service account;
  3. In the Service account name field, enter a name;
  4. From the Role drop-down list, select Project > Owner or if you wanna more restrict roles you can choose: cloudfunctions.admin (Cloud Functions Admin) and iam.serviceAccountUser (Service Account User);
    1. The service account needs to Cloud Functions Admin to have permission to update the IAM policies;
  5. Click Create. A JSON file that contains your key downloads to your computer;
  6. Save the JSON file to a safe place on your local machine (I create a hidden folder and save it in ~/.google-cloud-keys/key.json );
  7. Update the constant projectID with the name of your project at main.go;

Run

GOOGLE_APPLICATION_CREDENTIALS=~/.google-cloud-keys/key.json go run *.go

Send the below request to http://localhost:8080/

API

Create or update a Cloud Function

Accepts HTTP POST

Path /

Request

{
   "name":"cloud-function-test",
   "files":[
      {
         "name":"index.js",
         "content":"exports.helloWorld = (req, res) => {\n  let message = req.query.message || req.body.message || 'Hello World! 4';\n  res.status(200).send(message);\n};"
      },
      {
         "name":"package.json",
         "content":"{\n  \"name\": \"sample-http\",\n  \"version\": \"0.0.1\"\n}"
      }
   ]
}

Response

{
   "message":"Created the Cloud Function"
}

Result

preview

preview

Get a Cloud Function via function ID

Accepts HTTP GET

Path /

Request

  • function_id - string representing the cloud function ID
curl -X GET http://localhost:8080?function_id=trud_test

Response

{
   "cloudFunction":{
      "availableMemoryMb":128,
      "entryPoint":"helloWorld",
      "httpsTrigger":{
         "url":"https://us-central1-vendasta-hackathon.cloudfunctions.net/trud_test"
      },
      "ingressSettings":"ALLOW_ALL",
      "name":"projects/vendasta-hackathon/locations/us-central1/functions/trud_test",
      "runtime":"nodejs8",
      "serviceAccountEmail":"vendasta-hackathon@appspot.gserviceaccount.com",
      "sourceUploadUrl":"https://storage.googleapis.com/gcf-upload-us-central1-ed9343f1-111c-435e-b16c-28bb09e2f13f/f3679a71-e48e-4c94-8d26-307775296a73.zip?GoogleAccessId=service-481416019804@gcf-admin-robot.iam.gserviceaccount.com&Expires=1590974019&Signature=HDKLbM4x1StRLa8ICqR%2B2R5WEHRmB%2BeqdM2e7btRh1Jb5%2BDqn5SMh1SziOh48ZYDDTjSHpxtQO17%2F3ZeopBXhtEEg2ytGF%2BzJXRA5C2k5BuTX4ULK9OyLQwhE1TDGK5DqJ4JZ%2Fnylfnpdvs4UtqpP4s3Rt4yBszBM5HfIwXYsK7S2HXmskEMc7U9a8rXP6QdqSEwRyTth%2FWTorLt8S9qB0VB8lX8l7xpIsesL0R0mxhfLhn63TQggkx61yOu9qa%2FShjcnqRquOGDEG%2BQ2zEsvKijCaGDZDLq%2Fx0qSvfXSHAQ41uHHnv%2B7Xy6R31j%2Bhe53H3lKLmvUrgL6oM%2Bc5maHQ%3D%3D",
      "status":"ACTIVE",
      "timeout":"60s",
      "updateTime":"2020-06-01T00:44:07.102Z",
      "versionId":"3"
   }
}
Delete a Cloud Function via function ID

Accepts HTTP DELETE

Path /

Request

  • function_id - string representing the cloud function ID
curl -X DELETE http://localhost:8080?function_id=xdne_xpto

Response

{
   "cloudFunction":{
      "metadata":{
         "@type":"type.googleapis.com/google.cloud.functions.v1.OperationMetadataV1",
         "target":"projects/vendasta-hackathon/locations/us-central1/functions/xdne_xpto",
         "type":"DELETE_FUNCTION",
         "request":{
            "@type":"type.googleapis.com/google.protobuf.Empty"
         },
         "updateTime":"2020-07-11T00:15:13Z"
      },
      "name":"operations/dmVuZGFzdGEtaGFja2F0aG9uL3VzLWNlbnRyYWwxL3hkbmVfeHB0by9FUzlUX1N1RzVPaw"
   }
}
Set IAM Policy to `allUsers` have access to invoker the Cloud Function

Accepts HTTP POST

Path /policy

Request

{
   "function_id":"trud_test"
}

Response

{
   "policy":{
      "bindings":[
         {
            "members":[
               "allUsers"
            ],
            "role":"roles/cloudfunctions.invoker"
         }
      ],
      "etag":"BwWm/F4UOXk=",
      "version":1
   }
}

Libraries

Upgrade Go Fiber

go get -u github.com/gofiber/fiber/v2
go get -u github.com/gofiber/fiber/v2/middleware/cors
go get -u github.com/gofiber/fiber/v2/middleware/logger

📚 References

Author

⚠️ Copyright and license

Code and documentation copyright 2020-2030 the Authors and Code released under the MIT License. Docs released under Creative Commons.