# AWS Lambda

Lambda functions invoked synchronously or fire-and-forget from Python services.

Zato lets you invoke AWS Lambda functions directly from your Python services. You create an [AWS connection](https://zato.io/docs/dev/examples/cloud/aws/index.html) in the Dashboard and get the Lambda client through `conn.client('lambda')` - the explicit form is needed here because `lambda` is a reserved word in Python and cannot be an attribute name.

## Invoking a function {#invoking-a-function}

```python
# -*- coding: utf-8 -*-

# stdlib
import json

# Zato
from zato.server.service import Service

class ProcessOrder(Service):

    input = 'order_id'

    def handle(self):

        # Get the connection by its Dashboard name
        conn = self.aws['My AWS']

        # The Lambda client - lambda is a reserved word in Python,
        # so the explicit client() form is used.
        aws_lambda = conn.client('lambda')

        # Invoke the function and wait for its result
        payload = json.dumps({'order_id': self.request.input.order_id})

        response = aws_lambda.invoke(
            FunctionName='process-order',
            Payload=payload.encode('utf8'),
        )

        # Read the function's response
        result = response['Payload'].read()
        self.response.payload = json.loads(result)
```

## Asynchronous invocation {#asynchronous-invocation}

To fire and forget, pass `InvocationType='Event'` - Lambda queues the invocation and the call returns immediately, without the function's result.

```python
# -*- coding: utf-8 -*-

# stdlib
import json

# Zato
from zato.server.service import Service

class TriggerReportGeneration(Service):

    input = 'report_id'

    def handle(self):

        conn = self.aws['My AWS']
        aws_lambda = conn.client('lambda')

        payload = json.dumps({'report_id': self.request.input.report_id})

        response = aws_lambda.invoke(
            FunctionName='generate-report',
            InvocationType='Event',
            Payload=payload.encode('utf8'),
        )

        # 202 means the invocation was queued
        self.response.payload = {'status_code': response['StatusCode']}
```

## Listing functions {#listing-functions}

```python
# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class ListFunctions(Service):

    def handle(self):

        conn = self.aws['My AWS']
        aws_lambda = conn.client('lambda')

        response = aws_lambda.list_functions()

        names = []
        for function in response['Functions']:
            names.append(function['FunctionName'])

        self.response.payload = {'functions': names}
```

## More resources {#more-resources}

- [AWS integrations overview](https://zato.io/docs/dev/examples/cloud/aws/index.html)
- [AWS tutorial](https://zato.io/docs/dev/examples/cloud/aws/tutorial.html)
- [boto3 Lambda reference](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/lambda.html)

## Learn more {#learn-more}

- [Development documentation](https://zato.io/docs/dev/) - The complete reference for writing services
- [Requests and responses](https://zato.io/docs/dev/request-response/) - What a service receives, what it returns and how both are structured
- [Integration examples](https://zato.io/docs/dev/examples/) - Ready-made code for the systems you are likely to connect to
- [IDE and debugging](https://zato.io/docs/dev/ide/) - Write services in the Dashboard or in your own editor
- [Data models](https://zato.io/docs/dev/model/) - Declare inputs and outputs and have them validated for you
- [In-depth API tutorial](https://zato.io/tutorials/main/01.html) - The full platform tutorial, from installation to production patterns
