Giter Club home page Giter Club logo

cfn-faunadb-resource's Introduction

FaunaDB CloudFormation custom resource

This project is a Lambda function for creating a Collections and Indexes (in the collection) in FaunaDB as a CloudFormation Custom Resource, as well as a Serverless Template for deploying it to AWS.

Getting Started

Clone this project, then deploy the Lambda and use the ARN for a CloudFormation custom resource's service token. There is an included serverless.yml file to deploy using Serverless using some default options, and can be deployed with the command

npm run deploy

NOTE: This library supports fetching the FaunaDB access key from AWS's SSM Parameter store. If you are using this feature, please ensure the Lambda has permission to access the parameter from SSM, as well as the KMS key to decrypt the parameter if it has been encrypted.

Usage

When deploying a CloudFormation stack, the exported Lambda ARN may be used as the service token for a custom resource. As an alternative, the custom resource can also be declared in the same Template.

Resources:

...

    # Our custom resource
    MyResoruceName:
        Type: Custom::MyCustomResourceName
        Properties:
            ServiceToken:
                # Use the following if creating the custom resource in the same tempate
                Fn::GetAtt:
                    - FaunaLambdaFunction
                    - Arn
                        
                # Otherwise import the function ARN
                Fn::ImportValue: my-exported-resource-name

This resource will create one collection, and any number of indexes on the collection. Here's an example of how to format the template

Properties:
    ServiceToken: ...
    
    # FaunaDB Collection name
    CollectionName: some-name
    
    # FaunaDB Key parameter
    KeyParameter: KeyParam
    
    # Indexes to be created on this Collection
    Indexes:
        nameTerm:
            Name: users_by_name
            Terms:
                - field:
                    - data
                    - name
            Unique: `true`
            
        nameAgeTerm:
            Name: users_by_name_and_age
            Terms:
                - field:
                    - data
                    - name
                  transform: 'casefold'
                  
                - field:
                    - data
                    - age
        
        username:
            Name: usernames
            Values:
                - field:
                    - data
                    - username
                  reverse: `true`
        
        all:
            Name: all_users

Properties

These are the properties used by the lambda

CollectionName

Required
Every collection needs a name.

CollectionName: your-collection-name
Key
KeyParameter
KeyParameterSecure

Required
This lambda expects a FaunaDB Key to be able to connect to FaunaDB. There are three ways to specify this key: in plain text, as an SSM parameter, or a secure SSM parameter.

NOTE: SSM parameters need to exist in the same account as the Lambda for now. Possible future cross account SSM store implementation.

# Plain Text key
Key: 'FaunaDB Key'

# Or a parameter name
KeyParameter: KeyParam

# Or a secure string parameter
KeyParameterSecure: SecureKey

Indexes

While not required, indexes do make collections more useful. Takes an object with index descriptions to create the index on the Collection.

Indexes:
    index1:
        ... # Index description 1
    index2:
        ... # Index description 2
    ...

Each index description needs the following:

Property Name

The property name of each index is not actually used by FaunaDB, but is needed by this package to keep track of which index has updated, and which are added/deleted

Indexes:
    IndexProperty1:
        ...
    IndexProperty2:
        ...
Name

Required
FaunaDB does require each index to have an actual name.

Index1:
    Name: index1
    ...
Terms

Terms used to partition the index. If not specified, will contain all instances in the Collection.
Takes an array terms. Each term contains an object with a field property containing the path to the field to be indexed, and an optional transform property. Please refer to FaunaDB documentation for more information.

Internally this object is passed onto to FaunaDB's JS driver, so it should be formatted according to requirements (i.e. properties are lower case, etc.)

# This creates a term on the `data.name` field
Terms:
    - field:
        - data
        - name

# This creates terms on both `data.name` and `data.id`
Terms:
    - field:
        - data
        - name
    
    - field:
        - data
        - id
      transform: casefold

Note: FaunaDB dictates that indexes cannot change its Terms, Values, or Partition fields. Any updates to these fields will trigger a replacement of the index.

Values

Values describes the data covered by the index, which is what is returned from a query. If not specified, only the instance REF is covered.

Internally this object is passed onto to FaunaDB's JS driver, so it should be formatted according to requirements (i.e. properties are lower case, etc.)

Values:
    - field:
        - data
        - name
    
    - field:
        - ref
    
    - field:
        - ts
      reverse: true

Note: FaunaDB dictates that indexes cannot change its Terms, Values, or Partition fields. Any updates to these fields will trigger a replacement of the index.

Unique

This field will dictate all instances covered by this index be unique (i.e. will not allow duplicated entries to be added to the DB). If this property is updated from false to true, existing duplicate instances will not be removed, but future duplicate entries will be forbidden.

true or True will evaluate to True, all other entries will be treated as False

Unique: true
# Or
Unique: false

TODO

  • Handle errored stack updates better
  • Better documentation

Caveat

This project is a long ways away from implementing all features supported by FaunaDB. My hope is eventually to implement most functionality, but at the moment I have no idea if this is feasible. Comments are always welcome if you have any ideas about the direction this project should take.

Contributing

Pull Requests are always welcome, though I won't guarantee I can get around to reviewing it immediately. Priority at the moment is obviously to incorporate as many of missing FaunaDB features as possible.

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.