Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ This repository interfaces Service Discovery, in this instance CloudMap, in orde


## TODO
- Lambda (`request`).
- SQS (`listen`).
- Http (`request`|`call`).
Comment thread
EwanValentine marked this conversation as resolved.
- Fargate/ECS Task (`run`).

## Note:

## Note

This library requires *Python 3.5 and above*.

## Examples:

## Examples

### Lambda Call

Expand All @@ -43,3 +44,13 @@ from ais_service_discovery import call
response=call('namespace', 'service', 'handler', {<Payload>}, {'InvocationType': 'Event'})
print(response)
```

## Configuration

This library can utilise the following environment variables:

```
BOTO_MAX_ATTEMPTS=10 // Boto3 exponential back-off attemps
BOTO_READ_TIMEOUT=300 // Boto3 timeout
```

18 changes: 14 additions & 4 deletions ais_service_discovery/call_service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from os import environ as env
from boto3 import client
from .services import default_namespace, extract_service_parts, Services, \
CloudmapAdapter
Expand All @@ -6,10 +7,18 @@
from .sqs import SqsAdaptor, Send
from json import dumps

function = client('lambda')
sns = client('sns')
sqs = client('sqs')
service_discovery = client('servicediscovery')
from botocore.config import Config


BOTO_MAX_ATTEMPTS = int(env.get('BOTO_MAX_ATTEMPTS', 10))
BOTO_READ_TIMEOUT = float(env.get('BOTO_READ_TIMEOUT', 300))
Comment thread
EwanValentine marked this conversation as resolved.
Outdated

config = Config(read_timeout=BOTO_READ_TIMEOUT, retries={'max_attempts': BOTO_MAX_ATTEMPTS})
Comment thread
EwanValentine marked this conversation as resolved.
Outdated

function = client('lambda', config=config)
sns = client('sns', config=config)
sqs = client('sqs', config=config)
service_discovery = client('servicediscovery', config=config)

cloudmap_adaptor = CloudmapAdapter(service_discovery)
services = Services(cloudmap_adaptor)
Expand Down Expand Up @@ -58,3 +67,4 @@ def call_service(namespace, service, handler, body, opts={}):
('errorMessage' in payload) and ('errorMessage' in payload)):
raise Exception(payload)
return dumps(payload)

5 changes: 3 additions & 2 deletions ais_service_discovery/services/cloudmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ def __init__(self, client):

def filter_instances(self, response, id):
filter_function = lambda instance : instance['InstanceId'] == id

return {
**response,
'Instances': filter(filter_function, response['Instances'])
'Instances': list(filter(filter_function, response['Instances']))
Comment thread
EwanValentine marked this conversation as resolved.
}

def discover(self, namespace, name, id):
return self.filter_instances(self.client.discover_instances(
NamespaceName=namespace,
ServiceName=name
), id)

13 changes: 13 additions & 0 deletions examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from ais_service_discovery import call

try:
res = call(
'my-namespace',
'my-service',
'my-instance', {
'arg': 'hello-word',
})
print(res)
except Exception as e:
print(e)