github.com/argoproj/argo-events@v1.9.1/docs/sensors/triggers/aws-lambda.md (about) 1 # AWS Lambda 2 3 AWS Lambda provides a tremendous value, but the event driven lambda invocation is limited to 4 SNS, SQS and few other event sources. Argo Events makes it easy to integrate lambda with event sources 5 that are not native to AWS. 6 7 <br/> 8 <br/> 9 10 <p align="center"> 11 <img src="https://github.com/argoproj/argo-events/blob/master/docs/assets/aws-lambda-trigger.png?raw=true" alt="AWS Lambda Trigger"/> 12 </p> 13 14 <br/> 15 <br/> 16 17 ## Trigger A Simple Lambda 18 19 1. Make sure to have eventbus deployed in the namespace. 20 21 1. Make sure your AWS account has permissions to execute Lambda. More info on AWS permissions is available [here](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_change-permissions.html). 22 23 1. Fetch your access and secret key for AWS account and base64 encode them. 24 25 1. Create a secret called `aws-secret` as follows. 26 27 apiVersion: v1 28 kind: Secret 29 metadata: 30 name: aws-secret 31 type: Opaque 32 data: 33 accesskey: <base64-access-key> 34 secretkey: <base64-secret-key> 35 36 1. Create a basic lambda function called `hello` either using AWS cli or console. 37 38 exports.handler = async (event, context) => { 39 console.log('name =', event.name); 40 return event.name; 41 }; 42 43 1. Let's set up webhook event-source to invoke the lambda over http requests. 44 45 kubectl apply -n argo-events -f https://raw.githubusercontent.com/argoproj/argo-events/stable/examples/event-sources/webhook.yaml 46 47 1. Let's expose the webhook event-source using `port-forward` so that we can make a request to it. 48 49 kubectl -n argo-events port-forward <name-of-event-source-pod> 12000:12000 50 51 1. Deploy the webhook sensor with AWS Lambda trigger. 52 53 kubectl apply -n argo-events -f https://raw.githubusercontent.com/argoproj/argo-events/stable/examples/sensors/aws-lambda-trigger.yaml 54 55 1. Once the sensor pod is in running state, make a `curl` request to webhook event-source pod, 56 57 curl -d '{"name":"foo"}' -H "Content-Type: application/json" -X POST http://localhost:12000/example 58 59 9. It will trigger the AWS Lambda function `hello`. Look at the CloudWatch logs to verify. 60 61 ## Specification 62 63 The AWS Lambda trigger specification is available [here](https://github.com/argoproj/argo-events/blob/master/api/sensor.md#awslambdatrigger). 64 65 ## Request Payload 66 67 Invoking the AWS Lambda without a request payload would not be very useful. The lambda trigger within a sensor 68 is invoked when sensor receives an event from the eventbus. In order to construct a request payload based on the event data, sensor offers 69 `payload` field as a part of the lambda trigger. 70 71 Let's examine a lambda trigger, 72 73 awsLambda: 74 functionName: hello 75 accessKey: 76 name: aws-secret 77 key: accesskey 78 secretKey: 79 name: aws-secret 80 key: secretkey 81 namespace: argo-events 82 region: us-east-1 83 payload: 84 - src: 85 dependencyName: test-dep 86 dataKey: body.name 87 dest: name 88 89 The `payload` contains the list of `src` which refers to the source event and `dest` which refers to destination key within result request payload. 90 91 The `payload` declared above will generate a request payload like below, 92 93 { 94 "name": "foo" // name field from event data 95 } 96 97 The above payload will be passed in the request to invoke the AWS lambda. You can add however many number of `src` and `dest` under `payload`. 98 99 **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 event data. 100 101 ## Parameterization 102 103 Similar to other type of triggers, sensor offers parameterization for the AWS Lambda trigger. Parameterization is specially useful when 104 you want to define a generic trigger template in the sensor and populate values like function name, payload values on the fly. 105 106 Consider a scenario where you don't want to hard-code the function name and let the event data populate it. 107 108 awsLambda: 109 functionName: hello // this will be replaced. 110 accessKey: 111 name: aws-secret 112 key: accesskey 113 secretKey: 114 name: aws-secret 115 key: secretkey 116 namespace: argo-events 117 region: us-east-1 118 payload: 119 - src: 120 dependencyName: test-dep 121 dataKey: body.message 122 dest: message 123 parameters: 124 - src: 125 dependencyName: test-dep 126 dataKey: body.function_name 127 dest: functionName 128 129 With `parameters` the sensor will replace the function name `hello` with the value of field `function_name` from event data. 130 131 You can learn more about trigger parameterization [here](https://argoproj.github.io/argo-events/tutorials/02-parameterization/). 132 133 ## Policy 134 135 Trigger policy helps you determine the status of the lambda invocation and decide whether to stop or continue sensor. 136 137 To determine whether the lambda was successful or not, Lambda trigger provides a `Status` policy. 138 The `Status` holds a list of response statuses that are considered valid. 139 140 awsLambda: 141 functionName: hello 142 accessKey: 143 name: aws-secret 144 key: accesskey 145 secretKey: 146 name: aws-secret 147 key: secretkey 148 namespace: argo-events 149 region: us-east-1 150 payload: 151 - src: 152 dependencyName: test-dep 153 dataKey: body.message 154 dest: message 155 policy: 156 status: 157 allow: 158 - 200 159 - 201 160 161 The above lambda trigger will be treated successful only if its invocation returns with either 200 or 201 status.