Skip to content

Commit

Permalink
Initial import.
Browse files Browse the repository at this point in the history
  • Loading branch information
wmeddie committed Jan 31, 2025
1 parent 683094c commit d406724
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 359 deletions.
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# Xircuits Component Library Template
# Xircuits AWS Components

This section should have a short description on what is does.
This component library provides AWS service integrations for Xircuits, including support for SQS, S3, and DynamoDB operations.

## Prerequisites

A project may have prerequisites such as models that needs to be downloaded or non-python related setup. You may list them down here.
- An AWS account with appropriate credentials configured
- Python's `boto3` library
- Xircuits installed in your environment

## Installation

To use this component library, ensure you have Xircuits installed, then simply run:

```
xircuits install https://github.com/your-organization/your-repository
xircuits install git@github.com:XpressAI/xai-aws.git
```

Alternatively you may manually copy the directory / clone or submodule the repository to your working Xircuits project directory then install the packages using:
Expand All @@ -20,5 +22,22 @@ Alternatively you may manually copy the directory / clone or submodule the repos
pip install -r requirements.txt
```

## Components

### SQS Components
- `SendMessage`: Send a message to an SQS queue
- `ReceiveMessage`: Receive messages from an SQS queue
- `DeleteMessage`: Delete a message from an SQS queue

### S3 Components
- `UploadFile`: Upload a file to an S3 bucket
- `DownloadFile`: Download a file from an S3 bucket
- `ListObjects`: List objects in an S3 bucket

### DynamoDB Components
- `DynamoDBPutItem`: Insert an item into a DynamoDB table
- `DynamoDBGetItem`: Retrieve an item from a DynamoDB table
- `DynamoDBDeleteItem`: Delete an item from a DynamoDB table

## Tests
A github action to test your workflow runs has been provided. Simply add the path of your workflows [here](.github/workflows/run-workflow-tests.yml#L11).
A github action to test your workflow runs has been provided. Simply add the path of your workflows [here](.github/workflows/run-workflow-tests.yml#L11).
213 changes: 213 additions & 0 deletions aws_components.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
from xai_components.base import InArg, OutArg, Component, xai_component
import boto3


@xai_component
class SQSSendMessage(Component):
"""Component to send a message to an SQS queue.
##### inPorts:
- queue_url (str): The URL of the SQS queue.
- message_body (str): The message body to send.
##### outPorts:
- message_id (str): The ID of the sent message.
"""
queue_url: InArg[str]
message_body: InArg[str]
message_id: OutArg[str]

def execute(self, ctx) -> None:
sqs = boto3.client('sqs')
response = sqs.send_message(
QueueUrl=self.queue_url.value,
MessageBody=self.message_body.value
)
self.message_id.value = response['MessageId']


@xai_component
class SQSReceiveMessage(Component):
"""Component to receive messages from an SQS queue.
##### inPorts:
- queue_url (str): The URL of the SQS queue.
##### outPorts:
- messages (list): A list of received messages.
"""
queue_url: InArg[str]
messages: OutArg[list]

def execute(self, ctx) -> None:
sqs = boto3.client('sqs')
response = sqs.receive_message(
QueueUrl=self.queue_url.value,
MaxNumberOfMessages=10,
WaitTimeSeconds=10
)
self.messages.value = response.get('Messages', [])


@xai_component
class SQSDeleteMessage(Component):
"""Component to delete a message from an SQS queue.
##### inPorts:
- queue_url (str): The URL of the SQS queue.
- receipt_handle (str): The receipt handle of the message to delete.
"""
queue_url: InArg[str]
receipt_handle: InArg[str]

def execute(self, ctx) -> None:
sqs = boto3.client('sqs')
sqs.delete_message(
QueueUrl=self.queue_url.value,
ReceiptHandle=self.receipt_handle.value
)


@xai_component
class S3UploadFile(Component):
"""Component to upload a file to an S3 bucket.
##### inPorts:
- bucket_name (str): The name of the S3 bucket.
- file_path (str): The local path of the file to upload.
- object_name (str): The name to assign to the file in S3.
##### outPorts:
- response (dict): The response from the S3 upload operation.
"""
bucket_name: InArg[str]
file_path: InArg[str]
object_name: InArg[str]
response: OutArg[dict]

def execute(self, ctx) -> None:
s3 = boto3.client('s3')
response = s3.upload_file(
Filename=self.file_path.value,
Bucket=self.bucket_name.value,
Key=self.object_name.value
)
self.response.value = response


@xai_component
class S3DownloadFile(Component):
"""Component to download a file from an S3 bucket.
##### inPorts:
- bucket_name (str): The name of the S3 bucket.
- object_name (str): The name of the file in S3.
- file_path (str): The local path to save the downloaded file.
##### outPorts:
- response (dict): The response from the S3 download operation.
"""
bucket_name: InArg[str]
object_name: InArg[str]
file_path: InArg[str]
response: OutArg[dict]

def execute(self, ctx) -> None:
s3 = boto3.client('s3')
response = s3.download_file(
Bucket=self.bucket_name.value,
Key=self.object_name.value,
Filename=self.file_path.value
)
self.response.value = response


@xai_component
class S3ListObjects(Component):
"""Component to list objects in an S3 bucket.
##### inPorts:
- bucket_name (str): The name of the S3 bucket.
##### outPorts:
- objects (list): A list of objects in the specified S3 bucket.
"""
bucket_name: InArg[str]
objects: OutArg[list]

def execute(self, ctx) -> None:
s3 = boto3.client('s3')
response = s3.list_objects_v2(Bucket=self.bucket_name.value)
self.objects.value = response.get('Contents', [])


@xai_component
class DynamoDBPutItem(Component):
"""Component to insert an item into a DynamoDB table.
##### inPorts:
- table_name (str): The name of the table.
- item (dict): The item to insert (e.g., {'id': {'S': '123'}, 'name': {'S': 'John'}}).
##### outPorts:
- response (dict): The response from the PutItem operation.
"""
table_name: InArg[str]
item: InArg[dict]
response: OutArg[dict]

def execute(self, ctx) -> None:
dynamodb = boto3.client('dynamodb')
response = dynamodb.put_item(
TableName=self.table_name.value,
Item=self.item.value
)
self.response.value = response


@xai_component
class DynamoDBGetItem(Component):
"""Component to retrieve an item from a DynamoDB table.
##### inPorts:
- table_name (str): The name of the table.
- key (dict): The key of the item to retrieve (e.g., {'id': {'S': '123'}}).
##### outPorts:
- item (dict): The retrieved item.
"""
table_name: InArg[str]
key: InArg[dict]
item: OutArg[dict]

def execute(self, ctx) -> None:
dynamodb = boto3.client('dynamodb')
response = dynamodb.get_item(
TableName=self.table_name.value,
Key=self.key.value
)
self.item.value = response.get('Item', {})


@xai_component
class DynamoDBDeleteItem(Component):
"""Component to delete an item from a DynamoDB table.
##### inPorts:
- table_name (str): The name of the table.
- key (dict): The key of the item to delete (e.g., {'id': {'S': '123'}}).
##### outPorts:
- response (dict): The response from the DeleteItem operation.
"""
table_name: InArg[str]
key: InArg[dict]
response: OutArg[dict]

def execute(self, ctx) -> None:
dynamodb = boto3.client('dynamodb')
response = dynamodb.delete_item(
TableName=self.table_name.value,
Key=self.key.value
)
self.response.value = response
Loading

0 comments on commit d406724

Please sign in to comment.