Skip to content

Amazon AWS - Lambda

Nothing can be simpler to interact with Lambda Funtions than the BotCity plugin for AWS Lambda.

Manage synchronous and asynchronous invocations of lambda functions.

Installation

pip install botcity-aws-lambda-plugin

Importing the Plugin

After you installed this package, the next step is to import the package into your code and start using the functions.

from botcity.plugins.aws.lambda_functions import BotAWSLambdaPlugin

Setting up connection

Note

There are two different ways to authenticate.

1. Creating the .aws folder in the home directory, you need to create two files.

# ~/.aws/config
[default]
region=<region_code>
# ~/.aws/credentials
[default]
aws_access_key_id=<your_aws_access_key_id>
aws_secret_access_key=<your_aws_secret_access_key>

2. Passing credentials in the class constructor.

# Using the `.aws` folder
lambda_funtion = BotAWSLambdaPlugin()

# Alternative using the credentials as constructor arguments
lambda_funtion = BotAWSLambdaPlugin(
    use_credentials_file=False,
    access_key_id='<your_aws_access_key_id>',
    secret_access_key='<your_aws_secret_access_key>',
    region_name='<region_code>'
)

Lambda operations

List all functions

from botcity.plugins.aws.lambda_functions import BotAWSLambdaPlugin

aws = BotAWSLambdaPlugin()
print(aws.list_functions())

Get function info

Info

The function name parameter can be name-only or name with alias.

my-function (name-only), my-function:v1 (with alias).

from botcity.plugins.aws.lambda_functions import BotAWSLambdaPlugin

aws = BotAWSLambdaPlugin()
print(aws.get_function(function_name='<lambda_name>'))

Lambda invocation

Synchronous invocation

The function name parameter value

The function name parameter can be name-only or name with alias.

my-function (name-only), my-function:v1 (with alias).

InvocationType

You can use the InvocationType='Event' parameter to invoke lambda asynchronously, but you don't receive the response value

from botcity.plugins.aws.lambda_functions import BotAWSLambdaPlugin

aws = BotAWSLambdaPlugin()
result = aws.invoke_function(function_name='<lambda_name>', payload={'name': 'Botcity'})
print(result)

Asynchronous invocation

Return type

The future_invoke_function function returns a concurrent.futures.Future instance.

import time
from botcity.plugins.aws.lambda_functions import BotAWSLambdaPlugin

aws = BotAWSLambdaPlugin()
future_result = aws.future_invoke_function(function_name='<lambda_name>', payload={'name': 'Botcity'})

while not future_result.done():
    print('waiting...')
    time.sleep(1)

print('Invoke (result): ', future_result.result())

Alias operation

List aliases

Info

The function name parameter is name-only.

from botcity.plugins.aws.lambda_functions import BotAWSLambdaPlugin

aws = BotAWSLambdaPlugin()
print(aws.list_aliases(function_name='<lambda_name>'))

Get alias

from botcity.plugins.aws.lambda_functions import BotAWSLambdaPlugin

aws = BotAWSLambdaPlugin()
print(aws.get_alias(function_name='<lambda_name>', alias_name='<alias_name>'))