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:
    export SQREEN_TOKEN="replace with your own token"
    
  4. Follow the instructions for one of the following options.

a. Retrieve the install script by downloading it.

curl -O https://sqreen-for-lambda.s3-eu-west-1.amazonaws.com/install.sh

b. Install Sqreen as an AWS Lambda Layer with the following command:

sh install.sh layers

Note, you can use the desired AWS region or AWS profile using the environment variables AWS_REGION and AWS_PROFILE.

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:12"

b. Deploy your application with a new parameter.

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

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:12"
  ]
}

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:12"
        ]
    }
}

b. Apply the changes.

zappa update dev