Automated launching of playbooks based on parameters – Automating Tasks

Automated launching of playbooks based on parameters

Most of the time, even the most basic tasks when automated can become difficult to understand. If you have to automate or trigger multiple tasks, the complexity starts to increase. Not everyone can understand them, and it shouldn’t be the job of everyone to understand them. That is why even a lot of modern servers have user interfaces that make the processing of information easier for many.

However, in many cases, even this level of abstraction isn’t enough. It may be necessary to create a tool in which users can simply enter their inputs and the server handles the creation of complex workflows and resources automatically. In short, you can make playbooks with parameters that will create resources based on an overview given to it by someone who would like the creation of the resource but does not want to bother with the intricacies behind it (in most places, these rather whimsical folk are called customers). Let’s see how to do that:

  1. We will start by making a Google Form (yes, seriously). Go to forms.google.com and click on the big plus (+) button.

Figure 7.1 – Instance selection

It’s a simple Google Form for two different sizes of EC2 instances.

2. Now, we are going to write a Google Apps Script script and an AWS Lambda function:

import boto3
ec2 = boto3.client(‘ec2’)
def lambda_handler(event, context):
    instance_size = event[‘instance_size’]
    response = ec2.run_instances(
        ImageId='<INSERT_AMI_HERE>’,
        InstanceType=instance_size,
        MinCount=1,
        MaxCount=1,
        SecurityGroupIds=[‘<INSERT_SECURITY_GROUP_HERE’],
        SubnetId='<INSERT_SUBNET_HERE>’
    )
    return response

This Lambda function takes an input consisting of the size of the EC2 instance to be created and then creates that instance. We can define an endpoint for it using the Lambda URL or the API gateway.

3. Once this function and this endpoint have been made, you can then call the endpoint from Apps Script and make the trigger and the input from the form. In the form editor, click on the three dots at the top right and click on Script editor:

Figure 7.2 – Accessing Script editor

4. You can now write the API script in what is essentially JavaScript:

function submitForm(e) {
var responses = e.values;
var size = responses[0];
var apiUrl = ‘<YOUR_LAMBDA_URL>’;
var requestData = {
‘instance_size’: size,
};
var requestBody = JSON.stringify(requestData);
var options = {
‘method’: ‘get’,
‘contentType’: ‘application/json’,
‘payload’: requestBody,
};
var response = UrlFetchApp.fetch(apiUrl, options);
}

This will run the Lambda function, though there is a final step to trigger it by adding a trigger. On the left pane of the Apps Script project, click on the Triggers option.

Figure 7.3 – Calling Lambda using Apps Script

5. At the bottom right, click on Add Trigger, which will open the form to create a trigger where you can define all the necessary parameters:

Figure 7.4 – Adding a trigger for when the form is submitted…

Here, we can add the source of the event, and the function, and select the event type.

In doing so, we will create a workflow that will trigger a function when form data is submitted and use the data provided to the function to trigger an API URL.

And there you have it, that’s one way to connect all of the machinations that happen behind the scenes in a Lambda function with a simple Google Form.

Summary

In this chapter, we discussed the beauty of automation along with the means to achieve it. We learned how to automate virtual machine maintenance and container operations. We even learned how to add a layer of automation on top of that that would allow us to get people who are significantly less in the know involved in our process. Automation is a good thing. People will often believe otherwise and fear the automation of a lot of tasks, but the point of automation is to ensure that it is easier for people to live their lives. A life is not meant for boring repetitive tasks, it is meant for exploration. Automation is key to free up time for exploration. You control your life by controlling your time. Automation lets you do that.

In the next chapter, we will discuss the events that drive not only automation but most DevOps infrastructure in general. We will look into event-driven architecture and use cases where it is advantageous, as well as – of course – how Python can help.

Leave a Reply

Your email address will not be published. Required fields are marked *