AWS Lambda With Wasabi

Prev Next

AWS Lambda is validated for use with Wasabi S3 as the backend. Lambda can be used as a virtual function that you can run on demand and they can scale automatically.

You can integrate this service and use Wasabi S3 to store and process your data. You can create a Lambda function to trigger any events in many programming languages. As the Lambda function requires IAM roles to operate, below is a demonstration of how one can create an IAM role and Lambda function to talk to Wasabi.

------------------------------------------------------------------------------------------------------

Inserting a JSON file in Wasabi S3 using Lambda function

  1. Login into Wasabi account and create a bucket on Wasabi (you can also use pre-existing buckets). For this example, we have created a new bucket called ‘lambda-wasabi' in the us-east-2 region. You can choose any name and region.

  2. Create an IAM user in Wasabi that has (at a minimum) s3:PutObject permissions to the bucket (‘lambda-wasabi’ in this example).

  3. Create a Lambda function in AWS that utilizes the Wasabi S3 bucket from Step 1, and the Wasabi IAM user from Step 2.

(Note: If you have not worked with IAM Policies and Roles previously, it is highly recommended that you go through the official documentation and practice on the console.)

  1. We can first create the permission policy in Wasabi

    1. Log into your Wasabi account, click “Policies” on the navigation

    2. Click “Create Policy

      1. Give the policy a name (we will use ‘lambda-user-policy' for this example)

      2. Create a policy that allows s3:PutObject permissions:

        1. {
              "Version": "2012-10-17",
              "Statement": [
                  {
                      "Sid": "VisualEditor0",
                      "Effect": "Allow",
                      "Action": "s3:PutObject",
                      "Resource": "arn:aws:s3:::wasabi-lambda/*"
                  }
              ]
          }
      3. Save the policy

  2. We can then create this user in Wasabi

    1. Log into your Wasabi account, go to Users and click on Create User

      1. Give the user a name (we’ll use ‘wasabi-lambda-user' for this example)

      2. Check “Programmatic” under “Type of access” and click Next

      3. Skip adding to a group

      4. Add the permissions policy created above to the user

      5. Create User and store the access/secret key pair in a safe place (we will be using it for the Lambda function)

  3. Creating the Lambda Function

    1. Go to Lambda service in your AWS account and click on "create functions"

      1. Give a function name, we are naming "lambda-wasabi-test", you can choose any name

      2. Runtime: we are using Python 3.12, you can choose any language are you writing lambda function in

      3. Permissions: Allow AWS to create a new role with basic Lambda permissions

      4. Now, upload your own lambda function code or you can paste your code in the box. Below is a sample code example which we are using:

      5. Actual code to test:

        1. Note that this code example discusses the use of Wasabi's us-east-2 storage region. To use other Wasabi storage regions, please use the appropriate Wasabi service URL as described here. Also, please note that the ‘Wasabi-Access-Key' and ‘Wasabi-Secret-Key' values must be those of your access/secret key we generated in Step 2.

        2. import json
          import boto3
          
          s3 = boto3.client('s3',
                            endpoint_url='https://s3.wasabisys.com',
                            aws_access_key_id='Wasabi-Access-Key',
                            aws_secret_access_key='Wasabi-Secret-Key')
          
          
          def lambda_handler(event, context):
              bucket = 'lambda-wasabi'
              
              Company = {}
              Company['Company Name'] = 'Wasabi Technologies LLC'
              Company['Address'] = '75 Arlington Street Suite 810, Boston, MA 02116'
              Company['Phone'] = '+1 617-307-7912'
              
              fileName = 'Wasabi.json'
              uploadByteStream = bytes(json.dumps(Company).encode('UTF-8'))
              s3.put_object(Bucket=bucket, Key=fileName, Body=uploadByteStream)
          
              print('Put Complete')
      6. Once function code is uploaded/typed/pasted, go ahead and click “Deploy” on the left-hand side to commit the code change.

  4. Once the code is successfully deployed, click on the “Test” tab. Give the Event a name (we are using ‘test’ for this example), and click the orange ’Test” button to execute

  5. Once the test is run you should see that the execution succeeds. If any failures should occur, Lambda will provide a detailed output of the error

  6. This function has successfully triggered an event to Wasabi S3 and inserted a JSON document specified by the code (screenshot below)

Reading from a file stored in Wasabi S3 using Lambda function

Consider a scenario wherein you have a large file with data and you are looking to parse through the body of that file and read-only particular data that you need for your use case through a Lambda function. Here how's you can trigger this event:

1. Start with creating a bucket in the Wasabi account and upload that large data file. 

For this example, we are using the same bucket from earlier called "lambda-wasabi" in the us-east-2 region. Our sample file is shown below, and you can download this file at the bottom of this article for reference

Our goal with this Lambda function is to only get:

  • Total Deleted Storage Bytes

  • Total Raw Storage Bytes

  1. We can then create the permission policy in Wasabi

    1. Log into your Wasabi account, click “Policies” on the navigation

    2. Click “Create Policy”

      1. Give the policy a name (we will use ‘lambda-user-policy' for this example)

      2. Create a policy that allows s3:GetObject permissions:

        1. {
              "Version": "2012-10-17",
              "Statement": [
                  {
                      "Sid": "VisualEditor0",
                      "Effect": "Allow",
                      "Action": "s3:GetObject",
                      "Resource": "arn:aws:s3:::wasabi-lambda/*"
                  }
              ]
          }
        2. Save the policy

  2. We can then create this user in Wasabi

    1. Log into your Wasabi account, go to Users and click on Create User

      1. Give the user a name (we’ll use ‘wasabi-lambda-user' for this example)

      2. Check “Programmatic” under “Type of access” and click Next

      3. Skip adding to a group

      4. Add the permissions policy created above to the user

      5. Create User and store the access/secret key pair in a safe place (we will be using it for the Lambda function)

  3. Create Lambda function:

    1. Go to Lambda service in your AWS account and click on "create functions"

    2. Give this function a name: we are naming it "QueryingWasabiS3"

    3. Runtime: we are using Python 3.12, you can choose any language are you writing lambda function in

    4. Permissions: Allow AWS to create a new role with basic Lambda permissions

    5. Click the ‘Create Function’ button

    6. Now, upload your own lambda function code or you can paste your code in the box

    7. Below is a sample code example which we are using:

      1. Actual code to test:

        1. Note that this code example discusses the use of Wasabi's us-east-2 storage region. To use other Wasabi storage regions, please use the appropriate Wasabi service URL as described here

        2. import json
          import boto3
          
          s3 = boto3.client('s3',
                            endpoint_url='https://s3.us-east-2.wasabisys.com',
                            aws_access_key_id='Wasabi-Access-Key',
                            aws_secret_access_key='Wasabi-Secret-Access-Key')
          
          
          def lambda_handler(event, context):
              bucket = 'Wasabi-Bucket-Name'
              key = 'file-name'
              response = s3.get_object(Bucket=bucket, Key=key)
              content = response['Body']
              jsonObject = json.loads(content.read())
              billing = jsonObject['billing']
          
              for record in billing:
                  print("Total Deleted Storage Bytes are: " +
                        str(record['DeletedStorageSizeBytes']))
                  print("Total Raw Storage Bytes are: " +
                        str(record['RawStorageSizeBytes']))
                  print("---")
  4. Once function code is uploaded/typed/pasted, go ahead and click “Deploy” on the left-hand side to commit the code change.

  5. Once the code is successfully deployed, click on the “Test” tab. Give the Event a name (we are using ‘test’ for this example), and click the orange ’Test” button to execute

    1. Give any Event Name and click the orange “Test” button

  6. Below is the execution results that shows Lambda has fetched the particular data defined in the function: