06 Oct

AWS Step Functions for networkers – Lambda functions

Now it is time to create Lambda functions for our small state machine. The first one will fetch version stored in S3 bucket object; the second function will get it from firewall using REST API. Then we will pass the collected values to the third function. With AWS Step Functions it is very easy to achieve!

Reading object from S3 bucket

This python script will be straightforward

<br />
import boto3<br />
from botocore.exceptions import ClientError</p>
<p>def main(event, context):<br />
    s3 = boto3.resource('s3')</p>
<p>    try:<br />
        bucketObject = s3.Object('stepfunctiondemobucket','ASAVersion.txt')<br />
    except ClientError as e:<br />
<p>    print(bucketObject.get()['Body'].read().decode('utf-8'))</p>
<p>    return bucketObject.get()['Body'].read().decode('utf-8')<br />

All we do here is accessing the AWS S3 bucket and reading the object content. We use the boto3 library for accessing AWS services. I wrote more about this in my post AWS Lambda guide part II – Access to S3 service from Lambda function.

Reading from ASAv

The second script will get firmware version directly from firewall via REST API. If you do not know how to enable REST API on Cisco ASA, please read my old post: Cisco ASA REST API – Part I: Getting started.

This script is a modified version of what you can find in online Cisco ASA REST API documentation as an example. I changed it by putting it inside of main() function so we can run it in Lambda, I removed unnecessary variables, so everything is static, I enabled the support of self-signed certificates, and instead of printing the whole JSON structure the function just print and return value of the asaVersion key.

<br />
import base64<br />
import json<br />
import urllib2<br />
import ssl</p>
<p>def main(event, context):<br />
    ssl._create_default_https_context = ssl._create_unverified_context<br />
    server = &quot;https://asav-1.virl.lan&quot;<br />
    username = &quot;cisco&quot;<br />
    password = &quot;cisco&quot;<br />
    headers = {'Content-Type': 'application/json'}<br />
    api_path = &quot;/api/monitoring/device/components/version&quot;<br />
    url = server + api_path<br />
    f = None</p>
<p>    req = urllib2.Request(url, None, headers)<br />
    base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')<br />
    req.add_header(&quot;Authorization&quot;, &quot;Basic %s&quot; % base64string)<br />
    try:<br />
        f = urllib2.urlopen(req)<br />
        status_code = f.getcode()<br />
        if (status_code != 200):<br />
            print 'Error in get. Got status code: ' + status_code<br />
        resp = f.read()<br />
        json_resp = json.loads(resp)<br />
        print json_resp['asaVersion']<br />
    finally:<br />
        if f:  f.close()</p>
<p>    return json_resp['asaVersion']<br />

Passing data between states in Step Functions machine

In previous examples the steps in state machines pretty much did nothing. What we want now is to run the Lambda instead of Wait timer. However, first, let’s add the third function that will now just print the parameters provided during the execution. Save it as CompareFirmwareVersion function with the following content

<br />
def main(event, context):<br />
 print event<br />

Now it is time to modify the state machine definition. If we want to run Lambda function as a state, we need to change state type to Task and provide ARN of the Lambda function as Resource. The new state machine definition:

<br />
{<br />
  &quot;Comment&quot;: &quot;Example of simple AWS Step Function from Piotr Wojciechowski&quot;,<br />
  &quot;StartAt&quot;: &quot;GetConfiguration&quot;,<br />
  &quot;States&quot;: {<br />
    &quot;GetConfiguration&quot;: {<br />
      &quot;Type&quot;: &quot;Parallel&quot;,<br />
      &quot;Next&quot;: &quot;CompareFirmwareVersion&quot;,<br />
      &quot;Branches&quot;: [<br />
        {<br />
          &quot;StartAt&quot;: &quot;GetVersionFromS3&quot;,<br />
          &quot;States&quot;: {<br />
            &quot;GetVersionFromS3&quot;: {<br />
              &quot;Type&quot;: &quot;Task&quot;,<br />
              &quot;Resource&quot;: &quot;arn:aws:lambda:eu-west-1:0000000000000:function:GetVersionFromS3&quot;,<br />
              &quot;End&quot;: true<br />
            }<br />
          }<br />
        },<br />
        {<br />
          &quot;StartAt&quot;: &quot;GetVersionFromASA&quot;,<br />
          &quot;States&quot;: {<br />
            &quot;GetVersionFromASA&quot;: {<br />
              &quot;Type&quot;: &quot;Task&quot;,<br />
              &quot;Resource&quot;: &quot;arn:aws:lambda:eu-west-1:0000000000000:function:GetVersionFromASA&quot;,<br />
              &quot;End&quot;: true<br />
            }<br />
          }<br />
        }<br />
      ]<br />
    },<br />
    &quot;CompareFirmwareVersion&quot;: {<br />
      &quot;Type&quot;: &quot;Task&quot;,<br />
      &quot;Resource&quot;: &quot;arn:aws:lambda:eu-west-1:0000000000000:function:CompareFirmwareVersion&quot;,<br />
      &quot;End&quot;: true<br />
    }<br />
  }<br />
}<br />

Ok, so we defined state machine with three states. What about the parameters? How we pass output from first two functions to the third one? The answer is easy – it will be passed automatically! The AWS Step Function engine is responsible for sending returned values from the previous step to the next step. If, like in our example, preceding phase consists of two independent steps then results from both of them are merged.

It is easy to track the input and output values using the web interface. When you execute the state machine for each step you can see its details in Step Details window. In the Input tab, we will find the value provided to this step (if this is Lambda function this value is accessible via event variable), the Output tab contains value returned by the step.

The input of CompareFirmwareVersion is combined output of two Lambdas executed in previous steps

The input of CompareFirmwareVersion is combined output of two Lambdas executed in previous steps

In this example, each Lambda returns “raw” value – it is not embedded into JSON object. In real Step Functions machines, you should always use JSON even if you are not returning multiple values.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: