github.com/argoproj/argo-events@v1.9.1/docs/sensors/triggers/http-trigger.md (about)

     1  # HTTP Trigger
     2  
     3  Argo Events offers HTTP trigger which can easily invoke serverless functions like OpenFaaS, Kubeless, Knative, Nuclio and make REST API calls.
     4  
     5  <br/>
     6  <br/>
     7  
     8  <p align="center">
     9    <img src="https://github.com/argoproj/argo-events/blob/master/docs/assets/http-trigger.png?raw=true" alt="HTTP Trigger"/>
    10  </p>
    11  
    12  <br/>
    13  <br/>
    14  
    15  ## Specification
    16  
    17  The HTTP trigger specification is available [here](https://github.com/argoproj/argo-events/blob/master/api/sensor.md#httptrigger).
    18  
    19  ## REST API Calls
    20  
    21  Consider a scenario where your REST API server needs to consume events from event-sources S3, GitHub, SQS etc. Usually, you'd end up writing
    22  the integration yourself in the server code, although server logic has nothing to do any of the event-sources. This is where Argo Events HTTP trigger
    23  can help. The HTTP trigger takes the task of consuming events from event-sources away from API server and seamlessly integrates these events via REST API calls.
    24  
    25  We will set up a basic go http server and connect it with the Minio events.
    26  
    27  1. The HTTP server simply prints the request body as follows.
    28  
    29          package main
    30  
    31          import (
    32           "fmt"
    33           "io"
    34           "net/http"
    35          )
    36  
    37          func hello(w http.ResponseWriter, req *http.Request) {
    38           body, err := io.ReadAll(req.Body)
    39           if err != nil {
    40            fmt.Printf("%+v\n", err)
    41            return
    42           }
    43           fmt.Println(string(body))
    44           fmt.Fprintf(w, "hello\n")
    45          }
    46  
    47          func main() {
    48           http.HandleFunc("/hello", hello)
    49           fmt.Println("server is listening on 8090")
    50           http.ListenAndServe(":8090", nil)
    51          }
    52  
    53  2. Deploy the HTTP server.
    54  
    55          kubectl -n argo-events apply -f https://raw.githubusercontent.com/argoproj/argo-events/stable/examples/tutorials/09-http-trigger/http-server.yaml
    56  
    57  3. Create a service to expose the http server.
    58  
    59          kubectl -n argo-events apply -f https://raw.githubusercontent.com/argoproj/argo-events/stable/examples/tutorials/09-http-trigger/http-server-svc.yaml
    60  
    61  4. Either use Ingress, OpenShift Route or port-forwarding to expose the http server.
    62  
    63          kubectl -n argo-events port-forward <http-server-pod-name> 8090:8090
    64  
    65  5. Our goals is to seamlessly integrate Minio S3 bucket notifications with REST API server created in previous step. So,
    66      lets set up the Minio event-source available [here](https://argoproj.github.io/argo-events/setup/minio/).
    67      Don't create the sensor as we will be deploying it in next step.
    68  
    69  6. Create a sensor as follows.
    70  
    71          kubectl apply -n argo-events -f https://raw.githubusercontent.com/argoproj/argo-events/stable/examples/sensors/http-trigger.yaml
    72  
    73  7. Now, drop a file onto `input` bucket in Minio server.
    74  
    75  8. The sensor has triggered a http request to the http server. Take a look at the logs.
    76  
    77          server is listening on 8090
    78          {"type":"minio","bucket":"input"}
    79  
    80  9. Great!!!
    81  
    82  ### Request Payload
    83  
    84  In order to construct a request payload based on the event data, sensor offers
    85  `payload` field as a part of the HTTP trigger.
    86  
    87  Let's examine a HTTP trigger,
    88  
    89          http:
    90            url: http://http-server.argo-events.svc:8090/hello
    91            payload:
    92              - src:
    93                  dependencyName: test-dep
    94                  dataKey: notification.0.s3.bucket.name
    95                dest: bucket
    96              - src:
    97                  dependencyName: test-dep
    98                  contextKey: type
    99                dest: type
   100            method: POST  // GET, DELETE, POST, PUT, HEAD, etc.
   101  
   102  The `payload` contains the list of `src` which refers to the source event and `dest` which refers to destination key within result request payload.
   103  
   104  The `payload` declared above will generate a request payload like below,
   105  
   106          {
   107            "type": "type of event from event's context"
   108            "bucket": "bucket name from event data"
   109          }
   110  
   111  The above payload will be passed in the HTTP request. You can add however many number of `src` and `dest` under `payload`.
   112  
   113  **Note**: Take a look at [Parameterization](https://argoproj.github.io/argo-events/tutorials/02-parameterization/) in order to understand how to extract particular key-value from
   114  event data.
   115  
   116  ### Parameterization
   117  
   118  Similar to other type of triggers, sensor offers parameterization for the HTTP trigger. Parameterization is specially useful when
   119  you want to define a generic trigger template in the sensor and populate values like URL, payload values on the fly.
   120  
   121  You can learn more about trigger parameterization [here](https://argoproj.github.io/argo-events/tutorials/02-parameterization/).
   122  
   123  ### Policy
   124  
   125  Trigger policy helps you determine the status of the HTTP request and decide whether to stop or continue sensor.
   126  
   127  To determine whether the HTTP request was successful or not, the HTTP trigger provides a `Status` policy.
   128  The `Status` holds a list of response statuses that are considered valid.
   129  
   130          http:
   131            url: http://http-server.argo-events.svc:8090/hello
   132            payload:
   133              - src:
   134                  dependencyName: test-dep
   135                  dataKey: notification.0s3.bucket.name
   136                dest: bucket
   137              - src:
   138                  dependencyName: test-dep
   139                  contextKey: type
   140                dest: type
   141            method: POST  // GET, DELETE, POST, PUT, HEAD, etc.
   142        retryStrategy:
   143          steps: 3
   144          duration: 3s
   145        policy:
   146          status:
   147            allow:
   148              - 200
   149              - 201
   150  
   151  The above HTTP trigger will be treated successful only if the HTTP request returns with either 200 or 201 status.
   152  
   153  ## OpenFaaS
   154  
   155  OpenFaaS offers a simple way to spin up serverless functions. Lets see how we can leverage Argo Events HTTP trigger
   156  to invoke OpenFaaS function.
   157  
   158  1. If you don't have OpenFaaS installed, follow the [instructions](https://docs.openfaas.com/deployment/kubernetes/).
   159  
   160  2. Let's create a basic function. You can follow the [steps](https://blog.alexellis.io/serverless-golang-with-openfaas/).
   161      to set up the function.
   162  
   163           package function
   164  
   165           import (
   166            "fmt"
   167           )
   168  
   169           // Handle a serverless request
   170           func Handle(req []byte) string {
   171            return fmt.Sprintf("Hello, Go. You said: %s", string(req))
   172           }
   173  
   174  3. Make sure the function pod is up and running.
   175  
   176  4. We are going to invoke OpenFaaS function on a message on Redis Subscriber.
   177  
   178  5. Let's set up the Redis Database, Redis PubSub event-source as specified [here](https://argoproj.github.io/argo-events/setup/redis/).
   179      Do not create the Redis sensor, we are going to create it in next step.
   180  
   181  6. Let's create the sensor with OpenFaaS trigger.
   182  
   183          apiVersion: argoproj.io/v1alpha1
   184          kind: Sensor
   185          metadata:
   186            name: redis-sensor
   187          spec:
   188            dependencies:
   189              - name: test-dep
   190                eventSourceName: redis
   191                eventName: example
   192            triggers:
   193              - template:
   194                  name: openfaas-trigger
   195                  http:
   196                    url: http://gateway.openfaas.svc.cluster.local:8080/function/gohash
   197                    payload:
   198                      - src:
   199                          dependencyName: test-dep
   200                        dest: bucket
   201                    method: POST
   202  
   203  7. Publish a message on `FOO` channel using `redis-cli`.
   204  
   205          PUBLISH FOO hello
   206  
   207  8. As soon as you publish the message, the sensor will invoke the OpenFaaS function `gohash`.
   208  
   209  ## Kubeless
   210  
   211  Similar to REST API calls, you can easily invoke Kubeless functions using HTTP trigger.
   212  
   213  1. If you don't have Kubeless installed, follow the [installation](https://kubeless.io/docs/quick-start/).
   214  
   215  2. Lets create a basic function.
   216  
   217          def hello(event, context):
   218            print event
   219            return event['data']
   220  
   221  3. Make sure the function pod and service is created.
   222  
   223  4. Now, we are going to invoke the Kubeless function when a message is placed on a NATS queue.
   224  
   225  5. Let's set up the NATS event-source. Follow [instructions](https://argoproj.github.io/argo-events/setup/nats/#setup) for details.
   226      Do not create the NATS sensor, we are going to create it in next step.
   227  
   228  6. Let's create NATS sensor with HTTP trigger.
   229  
   230          apiVersion: argoproj.io/v1alpha1
   231          kind: Sensor
   232          metadata:
   233            name: nats-sensor
   234          spec:
   235            dependencies:
   236              - name: test-dep
   237                eventSourceName: nats
   238                eventName: example
   239            triggers:
   240              - template:
   241                  name: kubeless-trigger
   242                  http:
   243                    serverURL: http://hello.kubeless.svc.cluster.local:8080
   244                    payload:
   245                      - src:
   246                          dependencyName: test-dep
   247                          dataKey: body.first_name
   248                        dest: first_name
   249                      - src:
   250                          dependencyName: test-dep
   251                          dataKey: body.last_name
   252                        dest: last_name
   253                    method: POST
   254  
   255  7. Once event-source and sensor pod are up and running, dispatch a message on `foo` subject using nats client.
   256  
   257          go run main.go -s localhost foo '{"first_name": "foo", "last_name": "bar"}'
   258  
   259  8. It will invoke Kubeless function `hello`.
   260  
   261          {'event-time': None, 'extensions': {'request': <LocalRequest: POST http://hello.kubeless.svc.cluster.local:8080/> }, 'event-type': None, 'event-namespace': None, 'data': '{"first_name":"foo","last_name":"bar"}', 'event-id': None}
   262  
   263  # Other serverless frameworks
   264  
   265  Similar to OpenFaaS and Kubeless invocation demonstrated above, you can easily trigger KNative, Nuclio, Fission functions using HTTP trigger.