DEV Community

Cover image for Developing cloud agnostic application
Rahul Kashyap
Rahul Kashyap

Posted on

Developing cloud agnostic application

Introduction

Recently, i started working on a personal project where i want to build an application which is cloud agnostic - i.e. it can be deployed on any cloud provider with minimal/no code changes. Primary requirement is to separate business logic with cloud provider specific logic.

In this post, i want to share the approach which was followed.

  • Create an interface which has abstract methods for performing operations in the cloud.
  • Create cloud provider specific classes which are subclasses of this interface and provide implemenation for the abstract methods.
  • Create a separate class/method which will return the cloud provider implementation based on some condition. Factory Pattern
  • The calling class will use the object from the above class/method and call its methods.

Code

Below code uses python

Interface with abstract methods

from abc import ABC, abstractmethod

class IObjectStorage(ABC):

    @abstractmethod
    def upload_object_to_bucket(self, file_name, file_content):
        _raise an error that method is not implemented_

Enter fullscreen mode Exit fullscreen mode

Create cloud provider specific implementation

class AWSObjectStorageConnector(IObjectStorage):

    def __init__(self, bucket_name):
       _Initialize a s3 client using boto3 and initialize a variable using bucket name_

    def upload_object_to_bucket(self, file_name, file_content):
        _Implement the logic to upload the file to AWS S3 bucket_
Enter fullscreen mode Exit fullscreen mode

Create a method to get the specifc cloud provider implementation class object - Factory method

This method takes a cloud provider variable which will be passed from the calling method

def get_object_storage(cloud_provider, bucket_name) -> IObjectStorage:

    if cloud_provider == 'aws':
        return AWSObjectStorageConnector(bucket_name=bucket_name)
    else:
        raise ValueError(f'Unsupported cloud provider: {cloud_provider}')
Enter fullscreen mode Exit fullscreen mode

Call the factory method to get an instance of the object

cloud_provider variable will be read from an environment variable passed as input. This ensures that the same logic works fine with different cloud providers.

object_storage_connector = get_object_storage(cloud_provider=provider, bucket_name=bucket_name)
Enter fullscreen mode Exit fullscreen mode

Please feel free to comment with any suggestions or feedback.

Top comments (0)