Instant Cloud VMs with a cURL Command?!?

COMMENTS ()
Tweet

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?

Advantages of Cloud Functions

With Cloud Functions, you get:

  • Scalability: Cloud Functions automatically scale to handle varying workloads, eliminating the need for manual intervention or complex load-balancing setups.
  • Cost Effectiveness: With Cloud Functions, you only pay for the resources you use, making it a cost-effective solution for applications with unpredictable traffic patterns.

Disadvantages of Cloud Functions:

While Cloud Functions offer a host of benefits, there are some limitations to be aware of:

  • 10 MB limit on both HTTP request and response sizes.
  • Maximum 32 GB memory is available to a single cloud function.
  • 500 MB source size, including modules.
  • Despite these limitations, Cloud Functions offers a powerful solution for many use cases, including efficiently managing cloud infrastructure.

How do you create your REST API with Cloud Functions?

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:

  • Navigate to console.cloud.google.com/functions/list.
  • Click on the Create Function button on the top left.
  • Give your function a name, choose your region, and choose the computational limits you want to apply.
  • Click next!

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:

  • Go to https://console.cloud.google.com/run
  • Select the newly created function
  • Click on Permissions
  • Click on Add Principal and select the “all users” principal
  • Assign it the following role: Cloud Run Invoker

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>

Conclusion

 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.

CALL

USA408 365 4638

VISIT

1301 Shoreway Road, Suite 160,

Belmont, CA 94002

Contact us

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