‫Now, let's talk about how
‫our lambda function integrates
‫with an Application Balancer.
‫So, for now, lambda functions,
‫they can be either invoked with CLI or the SDK,
‫But sometimes if you want to expose them to the internet,
‫you want to allow people to use them
‫through an HTTP or an HTTPS endpoint.
‫And so as such, you have two ways for doing this.
‫The first way is to use an Application Balancer,
‫or an API Gateway, as we'll see
‫in the next lecture, in the next section.
‫So in this lecture, we'll be focusing on the ALB.
‫And then for it to work,
‫you need to register the lambda function in a target group.
‫So your clients will be invoking and sending requests
‫in the form of HTTP or HTTPS to your ALB,
‫which will be invoking synchronously
‫your lambda function in a target group,
‫because synchronously, because we're waiting
‫for the lambda function to get back
‫to the application balancer,
‫which will in turn, return a response to the client.
‫So the question is,
‫"How does the load balancer convert
‫an HTTP request into a lambda invocation?"
‫So from the ALB to lambda,
‫the HTTP gets transformed into a JSON document.
‫So this is an example request payload
‫for your lambda function, and as we can see,
‫in the top of the document,
‫there is the ELB information,
‫so which ELB invokes and what is the target group.
‫Then we get some information around the HTTP method,
‫so it was a GET and the path was /lambda.
‫We get the query string parameters as key/value pairs,
‫so each query string will be appearing in the JSON document.
‫We'll get the headers as key/value pairs,
‫and we'll get the body for POST, PUT,
‫and the value is base 64 encoded,
‫so do you need to decode it or not.
‫And so, for this information,
‫there is a translation of the entire HTTP request to JSON.
‫So we should remember is that the query string parameters,
‫the headers, and the body, they're all converted.
‫And for the query string parameters
‫and the headers, they are key/value pairs.
‫So, similarly,
‫our lambda function should return something, a JSON document
‫and the ALB will convert this back into HTTP.
‫So if we look at the response
‫from the lambda function, it's very simple.
‫It needs to include a status code and a description,
‫as well as the response headers as key/value pairs,
‫and finally the body of the response and the flag,
‫whether or not it is base 64 encoded.
‫Okay, so we know how the lambda,
‫the ALB converts from HTTP to JSON and back,
‫but now let's talk about one last feature
‫of the ALB integration that can come up in the exam,
‫which is the multi-header values.
‫So, if we have our client talking to our ALB,
‫we can enable an ALB setting,
‫which is to have multi-header values.
‫What does that mean?
‫That means that if we pass in
‫multiple headers with the same value
‫or query strings with the same value,
‫so I can represent query strings easily.
‫So, in this example,
‫name=foo&name=bar have the same name, but different values.
‫Then we can enable the setting and both the headers
‫and the query string parameters will be converted
‫as an array into the lambda function.
‫So that means that when my ALB invokes my lambda function
‫for the queryStringParameters JSON,
‫what I will see is name, and instead of one value,
‫I will see an array of values.
‫So, foo and bar.
‫So both values are converted
‫and that's something the exam does test you on.
‫It will ask you,
‫"How do we support multi-header values?"
‫It's just a setting on the ALB
‫and this is what it does.
‫So in the next lecture,
‫we'll go ahead and practice the ALB and lambda.