en-aws-synchronizer
en-aws-synchronizer
is a Ruby gem designed to seamlessly synchronize AWS services to your local database within a Rails application. This gem simplifies the process of fetching AWS resource data and mapping it to your ActiveRecord models.
Features
- Seamless Synchronization: Sync AWS resources like EC2 Volumes to your local database effortlessly.
- Custom Mappers: Define your custom mappers using YAML files to map AWS resource attributes to your database fields.
- Extendable: Easily extend the functionality to sync other AWS services by creating custom mappers.
Installation
Add this line to your application's Gemfile:
gem 'en-aws-synchronizer'
And then execute:
bundle install
Usage
- Define Your Mapper Create a YAML file in data/cloud_object_mappers/ directory to map AWS service attributes to your model fields. For example, to map AWS EC2 Volumes, create data/cloud_object_mappers/volume.yml:
name: (obj.tags || []).find { |t| t.key.eql?('Name') }.try(:value) || 'Undefined'
service_id: obj.volume_id
volume_type: obj.volume_type
state: obj.state
tags: obj.tags.to_h
- Update Your Model In your model, include the synchronizer module and reference the mapper YAML file:
require 'eaglenube/synchronizer/aws/volume'
class Volume < ApplicationRecord
include Eaglenube::Synchronizer::AWS::Volume
cloud_obj_mapper_yml "data/cloud_object_mappers/volume.yml"
def self.sync
credential = Aws::Credentials.new(<access_key_id>, <secret_access_key>)
sync_resource(credential, "us-east-1") do |aws_volume|
db_composite_unique_identifers = { region_id: region.id, aws_service_id: aws_volume.volume_id }
extra_attributes = {}
[object_identifying_attributes, extra_attributes]
end
end
end
- Synchronize Data Call the sync method on your model to sync the AWS resources to your local database:
Volume.sync
This will fetch the AWS EC2 Volume data and map the attributes according to the volume.yml mapper file, then save it to your database.
Customization You can extend this gem to sync other AWS resources by creating appropriate mappers and including the respective synchronizer module in your models.
Example To sync another AWS resource, such as Vpc, follow these steps:
Create a new mapper YAML file, e.g., data/cloud_object_mappers/vpc.yml. Include the appropriate synchronizer module in your model. Implement the sync method in your model, similar to the Volume example. Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/your-username/en-aws-synchronizer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.