Cloud computing has transformed how we deploy and manage applications, offering unparalleled scalability, flexibility, and efficiency. However, despite its benefits, navigating cloud consoles can often be a frustrating experience, with clunky interfaces and limited real-time feedback.
But what if there’s a way to bypass these limitations and interact with your cloud infrastructure using nothing more than a cURL command? Here comes the Cloud Functions and serverless computing.
Google Cloud Functions offer a revolutionary approach to cloud computing. They allow developers to write and run code in the cloud without the need to manage or run a server.
“Run your code in the cloud with no servers or containers to manage with our scalable, pay-as-you-go functions as a service (FaaS) product.”
Leveraging the power of event-driven architecture, Cloud Functions seamlessly integrates with other Google Cloud services, offering a scalable, pay-as-you-go solution for building robust applications. But how exactly can Cloud Functions revolutionize how we interact with cloud infrastructure?
With Cloud Functions, you get:
While Cloud Functions offer a host of benefits, there are some limitations to be aware of:
One of the most exciting use cases for Cloud Functions is the ability to create your REST API for managing cloud resources. By building a custom API, you can interact with your cloud infrastructure using familiar HTTP requests, bypassing the need for clunky web consoles and manual intervention.
To get started:
Now, define your function using your programming language of choice (for this tutorial, we’ll use Python 3.10). With your function defined, you can now start interacting with your cloud infrastructure using simple HTTP requests.
For example, you can create endpoints for starting and stopping Compute Engine instances, which makes managing your cloud infrastructure easy.
functions-framework==3.*
google-api-python-client==1.10.0
We need to add the second line to ensure we have the required pip module to interact with Google Cloud. Once we finish the prerequisites, we can write our code in the main.py file. Here’s what we need to start with:
@functions_framework.http
def hello_http(request):
“””
Driver for cloud function
“””
If request.method == ‘OPTIONS’: # letting CORS know that it’s okay to hit GET/POST on this endpoint
headers = {
‘Access-Control-Allow-Origin’: ‘*’,
‘Access-Control-Allow-Methods’: ‘POST,’
‘Access-Control-Allow-Headers’: ‘Content-Type,’
‘Access-Control-Max-Age’: ‘3600’
}
return (”, 204, headers)
Headers = { # headers to attach to each response we send
‘Access-Control-Allow-Methods’: ‘POST,’
‘Access-Control-Allow-Origin’: ‘*’
}
if request.json[‘operation’]==‘START’:
response = start_vm(request.json[‘id’], headers)
elif request.json[‘operation’]==‘STOP’:
response = stop_vm(request.json[‘id’], headers)
else
response = (‘Invalid Operation,’ 400, headers)
return response
You can modify the code according to your requirements, as this blog is just to provide you with a basic idea of how everything works. In the driver function, we added a few formalities to cater to the “Same origin policy” that our browsers are so fond of (kidding, they’re there for our security).
We can use the Google Cloud Compute SDK Reference to interact with our compute instances. However, here’s the code for starting and stopping a particular instance.
def start_vm(id, headers):
project_id = ‘<YOUR PROJECT ID HERE>’
zone_name = ‘<THE ZONE OF YOUR CHOICE HERE>’
compute = googleapiclient.discovery.build(‘compute’, ‘v1’)
_ = compute.instances().start(project=project_id, zone=zone_name, instance=vm_name).execute()
instance_details = compute.instances().get(project=project_id, zone=zone_name, instance=id).execute()
return (instance details, 200, headers)
def stop_vm(id, headers):
project_id = ‘<YOUR PROJECT ID HERE>’
zone_name = ‘<THE ZONE OF YOUR CHOICE HERE>’
compute = googleapiclient.discovery.build(‘compute’, ‘v1’)
_ = compute.instances().stop(project=project_id, zone=zone_name, instance=vm_name).execute()
return (”, 204, headers)
For the above code to work, you must add your project ID and the zone in which your instances are located. Given that, if you hit the test function and hit the function with the following request, you should see your instance started in the GCP Console.
{
“operation”: “START,”
“id”: <NAME OF YOUR INSTANCE HERE>
}
Now, if you click on DEPLOY and try to run the same request with a cURL command, it won’t work; that’s because we haven’t allowed our function to be accessed publicly. To do that, we need to do the following:
Now you’re good to go! Use the following cURL command to start your cloud instances:
Curl -X POST \
-H “Content-Type: application/json” \
-d ‘{
“operation”: “START,”
“id”: “<NAME OF YOUR INSTANCE HERE>”
}’ \
https://<URL OF YOUR CLOUD FUNCTION>
Cloud Functions offer a powerful solution for managing cloud infrastructure with ease. By leveraging the power of serverless computing, you can interact with your cloud resources using nothing more than a simple cURL command, revolutionizing the way you deploy and manage applications in the cloud.
USA408 365 4638
1301 Shoreway Road, Suite 160,
Belmont, CA 94002
Whether you are a large enterprise looking to augment your teams with experts resources or an SME looking to scale your business or a startup looking to build something.
We are your digital growth partner.
Tel:
+1 408 365 4638
Support:
+1 (408) 512 1812
COMMENTS ()
Tweet