AWS Lambda SDK Integration

It's Lambdas all the way down

This section illustrates how to use the aws_lambda.invoke and aws_lambda.invoke_async Sym SDK methods in your impl.py.

📘

Prerequisites

This page assumes you:

  1. Have completed Getting Started, and/or already have some basic Sym resources created. If you have not created any Sym resources yet, please visit the Getting Started pages first.

  2. Have completed AWS Runtime Setup to set up your runtime_connector module.

  3. Have an AWS Lambda you wish to invoke. For instructions on how to create an AWS Lambda, visit the AWS Tutorials or create one with the terraform-aws-lambda module.

For the remainder of this tutorial, we will refer to your Lambda in code samples as a data.aws_lambda_function resource, as below.

data "aws_lambda_function" "your_lambda" {
  function_name = "your-lambda-function"
}

Tutorial

Give the runtime_context Lambda Execution Permissions

To be able to invoke your Lambda, you will need to give the IAM Role created by the runtime_module lambda execution permissions. In this section, you will add the following resources to your Terraform:

  • The Lambda Connector Module
  • A sym_integration that enables these permissions in your sym_environment
# The AWS IAM Resources that allow the Sym Runtime to invoke your Lambda functions
module "lambda_connector" {
  source  = "symopsio/lambda-connector/aws"
  version = ">= 1.0.0"

  environment       = var.environment_name

  # Update this list to include all the lambdas you wish to allow Sym to invoke
  lambda_arns       = [data.aws_lambda_function.your_lambda.arn]

  # The IAM Role(s) to give these permissions to
  # Here we are using the IAM Role provisioned in runtime.tf
  runtime_role_arns = [module.runtime_connector.sym_runtime_connector_role.arn]
}

# This will be added to your sym_environment integrations to enable the aws_lambda SDK methods
resource "sym_integration" "lambda_context" {
  type = "permission_context"
  name = "lambda-context-${var.environment_name}"

  external_id = module.lambda_connector.settings.account_id
  settings    = module.lambda_connector.settings
}

Enable the Lambda Integration in your sym_environment

When executing methods from the Sym SDK integrations package, the Sym Runtime needs to know which credentials to assume.

To enable the aws_lambda SDK integration in your impl.py, add the lambda_context from the previous section to your sym_environment.integrations block.

resource "sym_environment" "this" {
  name            = var.environment_name
  runtime_id      = sym_runtime.this.id
  error_logger_id = sym_error_logger.slack.id

  integrations = {
		slack_id = sym_integration.slack.id

		# This `aws_lambda_id` is required to be able to use the `aws_lambda` SDK methods
		# It tells the Sym Runtime to use the permissions defined by sym_integration.lambda_context
		aws_lambda_id = sym_integration.lambda_context.id
  }
}

Invoke your Lambda from impl.py

With these Terraform pieces, you can now utilize the sym.sdk.integrations.aws_lambda methods.

from sym.sdk.annotations import hook
from sym.sdk.integrations import aws_lambda

@hook
def on_request(event):
    # The lambda to invoke
    lambda_arn = "arn:aws:lambda:us-east-1:account-id:function:your-lambda-function"
    # The payload to send to your lambda
    payload = {"email": event.user.email}
    
    # `aws_lambda.invoke` is a synchronous invocation, and returns the lambda's response
    response = aws_lambda.invoke(lambda_arn, payload)
    print(response)
    
    # `aws_lambda.invoke_async` is an asynchronous invocatio, and is fire-and-forget
    aws_lambda.invoke_async(lambda_arn, payload)

(Optional) Pass in your Lambda ARN from Terraform

In the above on_request hook, you'll notice that the lambda_arn we invoke is the ARN from the data.aws_lambda_function resource. If you already have your lambda defined in Terraform, then you can auto-magically access this ARN by utilizing the event.flow.vars functionality, where you define variables in Terraform to be available in your impl.py.

In your sym_flow.vars definition, add your lambda function's ARN as a variable lambda_arn (or your preferred variable name)

resource "sym_flow" "this" {
  # ... truncated

  # If you already have vars = var.flow_variables, then you can append new variables
  # with Terraform's `merge` function.
  vars = merge(
    var.flow_variables,
    {
      # This will be available as event.flow.vars["lambda_arn"]
      lambda_arn = data.aws_lambda_function.your_lambda.arn
    }
  )

  params { ... } # truncated
}

You can now access these variables with event.flow.vars in your hooks and reducers.

from sym.sdk.annotations import hook
from sym.sdk.integrations import aws_lambda

@hook
def on_request(event):
    lambda_arn = event.flow.vars["lambda_arn"]
    response = aws_lambda.invoke(lambda_arn, {"email": event.user.email})
    print(response)

Full Example

You can find the complete code for this example in our AWS Lambda SDK example.