Skip to content

Tutorial Part 2

Emilien Lancelot edited this page Sep 14, 2022 · 12 revisions

Tutorial - Part 2 - Sending a message to your lambda

Prerequisite

  • Having followed part 1

What will be covered here

Now that we have a working lambda that can be boot using the Gitfaas API we want to give it parameters during runtime.
This way we can replace any given string and the lambda is not static anymore.

Let's go

Updating the Kubernetes Job to take input parameters

Gitfaas uses templates inside your Kubernetes YAML which the value will be replaced during runtime. It has 3 default template variables that you MUST add to your YAML configurations.

Variable name Description
{{RANDOM}} A random string that you should add to the name of your job. This way each new instance of the job will have a unique name.
{{PAYLOAD}} This will be replaced by the content of the message sent to your lambda. Use it in ENV to access the value during runtime.
{{FUNCTION_UID}} This UID represents the unique run of the lambda. This will be covered in PART 3 so you can forget about this for now.

Let's add theses variable to our Job template:
replace_job.yaml

apiVersion: batch/v1
kind: Job
metadata:
  name: replace-job-{{RANDOM}}   # The RANDOM variable will be replaced at runtime by a random value.
spec:
  ttlSecondsAfterFinished: 3600
  backoffLimit: 1
  template:
    spec:
      containers:
      - name: reverse
        image: <URL_OR_ACCOUNT_NAME_OF_REGISTRY>/demo-gitfaas-replace:0.0.1 # Update the the image with your own
        env:
        - name: PAYLOAD
          value: "{{PAYLOAD}}"  # The PAYLOAD variable Will be replaced at runtime by the message sent to this lambda
      restartPolicy: Never

➡️ Commit the updated version of the Job to the Git repo.

To force Gitfaas to pull your commit immediately you can do (still need the port forward though):

$ curl http://127.0.0.1:5000/refresh

Update the python script to use the input parameters

import os
import base64
import json

# Ie: Expected payload format in input message
# {
#    "source": "I like bananas",
#    "str-to-replace": "bananas",
#    "replace-with": "apples"
# }

def main():

    # Retrieving the base64 payload (the input message sent to this function)
    b64_message = os.environ.get('PAYLOAD', None)

    # Decoding base64 JSON payload into usable JSON
    str_message = base64.b64decode(b64_message).decode("utf-8")

    json_message = json.loads(str_message)
    replaced_string = json_message["source"].replace(json_message["str-to-replace"], json_message["replace-with"])

    print("Replaced string is : %s" % replaced_string)

main()

This updated version retrieves the PAYLOAD as env. The PAYALOAD env variable contains the message that we will send to the lamda during runtime.

➡️ Rebuild the application

docker build . -t <URL_OR_ACCOUNT_NAME_OF_REGISTRY>/demo-gitfaas-replace:0.0.1
docker push <URL_OR_ACCOUNT_NAME_OF_REGISTRY>/demo-gitfaas-replace:0.0.1

⚠️ Here we choose to overwrite the tag 0.0.1 instead of making 0.0.2. You can choose to use 0.0.2 but then you must not forget to update the image tag inside the Kubernetes Job and commit !

Start the lamdda

Let's define a payload in out terminal:

$ PAYLOAD=$(cat << EOF
{
  "source": "I like bananas",
  "str-to-replace": "bananas",
  "replace-with": "apples"
}
EOF
)

Then we can send this payload to our new lamda by requesting the topic defined in Part 1.

$ curl -X POST http://127.0.0.1:5000/publish/replace -d "$PAYLOAD" -H 'Content-Type: application/json' 

This will trigger Gitfaas. It will then start your replace Job and give it the content of $PAYLOAD.

Let's check it worked:

$ kubectl get pods -n gitfaas
replace-job-cb97773b-21be-4988-b4d5-ffc9e9abf3f2--1-g2hkf   0/1     Completed   0          3s

ℹ️ Above we can see the pod name is quite long because of the {{RANDOM}} variable that has been appended.

$ kubectl logs -n gitfaas replace-job-xxxxxx
Replaced string is : I like apples

Finaly, let's try with another input message:

$ PAYLOAD=$(cat << EOF
{
  "source": "Kubernetes rocks",
  "str-to-replace": "rocks",
  "replace-with": "is great"
}
EOF
)
$ curl -X POST http://127.0.0.1:5000/publish/replace -d "$PAYLOAD" -H 'Content-Type: application/json' 

This will create another job that will treat the input parameters and display a new line. Go check the logs !

This is all there is for Part 2. jump right in Part 3 !