Install the Sqreen Microagent for AWS Lambda Python functions

Closed Alpha

The Sqreen microagent for AWS Lambda Python functions is currently in closed alpha testing. Please email us at contact@sqreen.com for more information.

  1. Sign up to create your Sqreen account.
  2. Fetch your organization token. From the Sqreen Dashboard, access Account Settings > Environments & Tokens. Your token begins with env_org_. Take note of the token.
  3. Set the following environment variable in your application:
    export SQREEN_TOKEN="replace with your own token"
    
  4. Follow the instructions for one of the following options.

a. Insert the following configuration in your template.

Parameters:
  SqreenToken:
    Type: String
    Description: Sqreen organization token

Globals:
  Function:
    Environment:
      Variables:
        SQREEN_TOKEN: !Ref SqreenToken
    Layers:
     - !Sub "arn:aws:lambda:${AWS::Region}:301725247889:layer:sqreen-for-python:4"

b. Deploy your application with a new parameter.

sam build
sam deploy --parameter-overrides "SqreenToken=${SQREEN_TOKEN}"

a. Apply the following command. This command REPLACES your existing function environment variables and layers, so be sure to adapt the command below to add your current function environment variables and layers. Learn more at AWS CLI documentation.

export MY_FUNCTION=my-function
export AWS_REGION=eu-west-1

aws lambda update-function-configuration --function-name "${MY_FUNCTION}" \
    --environment "Variables={SQREEN_TOKEN=${SQREEN_TOKEN}}" \
    --layers "arn:aws:lambda:${AWS_REGION}:301725247889:layer:sqreen-for-python:4"

a. Modify your Terraform Lambda function resources to match the following.

variable "sqreen_token" {}
data "aws_region" "current" {}

resource "aws_lambda_function" "my_function" {
  function_name = "my_function"
  filename      = "my_function_payload.zip"

  runtime = "python3.x"
  handler = "..."
  role    = "..."

  environment {
    variables = {
      SQREEN_TOKEN = var.sqreen_token
    }
  }

  layers = [
    "arn:aws:lambda:${data.aws_region.current.name}:301725247889:layer:sqreen-for-python:4"
  ]
}

b. Apply the changes.

terraform apply -var="sqreen_token=${SQREEN_TOKEN}"

a. Modify your Zappa configuration and adapt the aws_environment_variables and layers fields.

{
    "dev": {
        "project_name": "my-zappa-application",
        "runtime": "python3.x",
        "aws_environment_variables": {
            "SQREEN_TOKEN": "<MY_SQREEN_TOKEN>"
        },
        "layers": [
            "arn:aws:lambda:<MY_AWS_REGION>:301725247889:layer:sqreen-for-python:4"
        ]
    }
}

b. Apply the changes.

zappa update dev