Invoke Services from Lambda Functions

This topic describes how to invoke services in the mesh from Lambda functions registered with Consul.

Lambda-to-mesh functionality is currently in beta: Functionality associated with beta features are subject to change. You should never use the beta release in secure environments or production scenarios. Features in beta may have performance issues, scaling issues, and limited support.

Introduction

The following steps describe the process:

  1. Deploy the destination service and mesh gateway.
  2. Deploy the Lambda extension layer.
  3. Deploy the Lambda registrator.
  4. Write the Lambda function code.
  5. Deploy the Lambda function.
  6. Invoke the Lambda function.

You must add the consul-lambda-extension extension as a Lambda layer to enable Lambda functions to send requests to mesh services. Refer to the AWS Lambda documentation for instructions on how to add layers to your Lambda functions.

The layer runs an external Lambda extension that starts a sidecar proxy. The proxy listens on one port for each upstream service and upgrades the outgoing connections to mTLS. It then proxies the requests through to mesh gateways.

Prerequisites

You must deploy the destination services and mesh gateway prior to deploying your Lambda service with the consul-lambda-extension layer.

Deploy the destination service

There are several methods for deploying services to Consul service mesh. The following example configuration deploys a service named static-server with Consul on Kubernetes.

  1. kind: Service
  2. apiVersion: v1
  3. metadata:
  4. # Specifies the service name in Consul.
  5. name: static-server
  6. spec:
  7. selector:
  8. app: static-server
  9. ports:
  10. - protocol: TCP
  11. port: 80
  12. targetPort: 8080
  13. ---
  14. apiVersion: v1
  15. kind: ServiceAccount
  16. metadata:
  17. name: static-server
  18. ---
  19. apiVersion: apps/v1
  20. kind: Deployment
  21. metadata:
  22. name: static-server
  23. spec:
  24. replicas: 1
  25. selector:
  26. matchLabels:
  27. app: static-server
  28. template:
  29. metadata:
  30. name: static-server
  31. labels:
  32. app: static-server
  33. annotations:
  34. 'consul.hashicorp.com/connect-inject': 'true'
  35. spec:
  36. containers:
  37. - name: static-server
  38. image: hashicorp/http-echo:latest
  39. args:
  40. - -text="hello world"
  41. - -listen=:8080
  42. ports:
  43. - containerPort: 8080
  44. name: http
  45. serviceAccountName: static-server

Deploy the mesh gateway

The mesh gateway must be running and registered to the Lambda function’s Consul datacenter. Refer to the following documentation and tutorials for instructions:

Deploy the Lambda extension layer

The consul-lambda-extension extension runs during the Init phase of the Lambda function execution. The extension retrieves the data that the Lambda registrator has been configured to store from AWS Parameter Store and creates a lightweight TCP proxy. The proxy creates a local listener for each upstream defined in the CONSUL_SERVICE_UPSTREAMS environment variable.

The extension periodically retrieves the data from the AWS Parameter Store so that the function can process requests. When the Lambda function receives a shutdown event, the extension also stops.

  1. Download the consul-lambda-extension extension from releases.hashicorp.com:

    1. curl -o consul-lambda-extension_<version>_linux_amd64.zip https://releases.hashicorp.com/consul-lambda/<version>/consul-lambda-extension_<version>_linux_amd64.zip
  2. Create the AWS Lambda layer in the same AWS region as the Lambda function. You can create the layer manually using the AWS CLI or AWS Console, but we recommend using Terraform:

    Invoke Services from Lambda FunctionsBETABETA - 图1

    consul-lambda-extension.tf

    1. resource "aws_lambda_layer_version" "consul_lambda_extension" {
    2. layer_name = "consul-lambda-extension"
    3. filename = "consul-lambda-extension_<version>_linux_amd64.zip"
    4. source_code_hash = filebase64sha256("consul-lambda-extension_<version>_linux_amd64.zip")
    5. description = "Consul service mesh extension for AWS Lambda"
    6. }

Deploy the Lambda registrator

Configure and deploy the Lambda registrator. Refer to the registrator configuration documentation and the registrator deployment documentation for instructions.

Write the Lambda function code

Refer to the AWS Lambda documentation for instructions on how to write a Lambda function. In the following example, the function calls an upstream service on port 2345:

  1. package main
  2. import (
  3. "context"
  4. "io"
  5. "fmt"
  6. "net/http"
  7. "github.com/aws/aws-lambda-go/lambda"
  8. )
  9. type Response struct {
  10. StatusCode int `json:"statusCode"`
  11. Body string `json:"body"`
  12. }
  13. func HandleRequest(ctx context.Context, _ interface{}) (Response, error) {
  14. resp, err := http.Get("http://localhost:2345")
  15. fmt.Println("Got response", resp)
  16. if err != nil {
  17. return Response{StatusCode: 500, Body: "Something bad happened"}, err
  18. }
  19. if resp.StatusCode != 200 {
  20. return Response{StatusCode: resp.StatusCode, Body: resp.Status}, err
  21. }
  22. defer resp.Body.Close()
  23. b, err := io.ReadAll(resp.Body)
  24. if err != nil {
  25. return Response{StatusCode: 500, Body: "Error decoding body"}, err
  26. }
  27. return Response{StatusCode: 200, Body: string(b)}, nil
  28. }
  29. func main() {
  30. lambda.Start(HandleRequest)
  31. }

Deploy the Lambda function

  1. Create and apply an IAM policy that allows the Lambda function’s role to fetch the Lambda extension’s data from the AWS Parameter Store. The following example, creates an IAM role for the Lambda function, creates an IAM policy with the necessary permissions and attaches the policy to the role:

    Invoke Services from Lambda FunctionsBETABETA - 图2

    lambda-iam-policy.tf

    1. resource "aws_iam_role" "lambda" {
    2. name = "lambda-role"
    3. assume_role_policy = <<EOF
    4. {
    5. "Version": "2012-10-17",
    6. "Statement": [
    7. {
    8. "Action": "sts:AssumeRole",
    9. "Principal": {
    10. "Service": "lambda.amazonaws.com"
    11. },
    12. "Effect": "Allow",
    13. "Sid": ""
    14. }
    15. ]
    16. }
    17. EOF
    18. }
    19. resource "aws_iam_policy" "lambda" {
    20. name = "lambda-policy"
    21. path = "/"
    22. description = "IAM policy lambda"
    23. policy = <<EOF
    24. {
    25. "Version": "2012-10-17",
    26. "Statement": [
    27. {
    28. "Effect": "Allow",
    29. "Action": [
    30. "ssm:GetParameter"
    31. ],
    32. "Resource": "arn:aws:ssm:*:*:parameter${local.this_lambdas_extension_data_path}"
    33. }
    34. ]
    35. }
    36. EOF
    37. }
    38. resource "aws_iam_role_policy_attachment" "lambda" {
    39. role = aws_iam_role.lambda.name
    40. policy_arn = aws_iam_policy.lambda.arn
    41. }
  2. Configure and deploy the Lambda function. Refer to the Lambda extension configuration reference for information about all available options. There are several methods for deploying Lambda functions. The following example uses Terraform to deploy a function that can invoke the static-server upstream service using mTLS data stored under the /lambda_extension_data prefix:

    Invoke Services from Lambda FunctionsBETABETA - 图3

    lambda-function.tf

    1. resource "aws_lambda_function" "example" {
    2. function_name = "lambda"
    3. role = aws_iam_role.lambda.arn
    4. tags = {
    5. "serverless.consul.hashicorp.com/v1alpha1/lambda/enabled" = "true"
    6. }
    7. variables = {
    8. environment = {
    9. CONSUL_MESH_GATEWAY_URI = var.mesh_gateway_http_addr
    10. CONSUL_SERVICE_UPSTREAMS = "static-server:2345:dc1"
    11. CONSUL_EXTENSION_DATA_PREFIX = "/lambda_extension_data"
    12. }
    13. }
    14. layers = [aws_lambda_layer_version.consul_lambda_extension.arn]
  3. Run the terraform apply command and Consul automatically configures a service for the Lambda function.

Lambda extension configuration

Define the following environment variables in your Lambda functions to configure the Lambda extension. The variables apply to each Lambda function in your environment:

VariableDescriptionDefault
CONSUL_MESH_GATEWAY_URISpecifies the URI where the mesh gateways that the plugin makes requests are running. The mesh gateway should be registered in the same Consul datacenter and partition that the service is running in. For optimal performance, this mesh gateway should run in the same AWS region.none
CONSUL_EXTENSION_DATA_PREFIXSpecifies the prefix that the plugin pulls configuration data from. The data must be located in the following directory:
“${CONSUL_EXTENSION_DATA_PREFIX}/${CONSUL_SERVICE_PARTITION}/${CONSUL_SERVICE_NAMESPACE}/<lambda-function-name>”
none
CONSUL_SERVICE_NAMESPACESpecifies the Consul namespace the service is registered into.default
CONSUL_SERVICE_PARTITIONSpecifies the Consul partition the service is registered into.default
CONSUL_REFRESH_FREQUENCYSpecifies the amount of time the extension waits before re-pulling data from the Parameter Store. Use Go time.Duration string values, for example, ”30s”.
The time is added to the duration configured in the Lambda registrator sync_frequency_in_minutes configuration. Refer to Lambda registrator configuration options. The combined configurations determine how stale the data may become. Lambda functions can run for up to 14 hours, so we recommend configuring a value that results in acceptable staleness for certificates.
“5m”
CONSUL_SERVICE_UPSTREAMSSpecifies a comma-separated list of upstream services that the Lambda function can call. Specify the value as an unlabelled annotation according to the consul.hashicorp.com/connect-service-upstreams annotation format in Consul on Kubernetes. For example, “[service-name]:[port]:[optional-datacenter]”none

Invoke the Lambda function

If intentions are enabled in the Consul service mesh, you must create an intention that allows the Lambda function’s Consul service to invoke all upstream services prior to invoking the Lambda function. Refer to Service mesh intentions for additional information.

There are several ways to invoke Lambda functions. In the following example, the aws lambda invoke CLI command invokes the function:

  1. $ aws lambda invoke --function-name lambda /dev/stdout | cat