Coder Social home page Coder Social logo

martinraynov / cerbos-aws-lambda Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cerbos/cerbos-aws-lambda

0.0 0.0 0.0 34 KB

Gateway service implements AWS Lambda runtime and invokes Cerbos server API hosted in the same AWS Lambda instance.

License: Apache License 2.0

Go 82.09% Makefile 14.77% Dockerfile 3.14%

cerbos-aws-lambda's Introduction

Cerbos AWS Lambda Docker Image

Gateway service implements AWS Lambda runtime and invokes Cerbos server API hosted in the same AWS Lambda instance.

Cerbos is the open core, language-agnostic, scalable authorization solution that makes user permissions and authorization simple to implement and manage by writing context-aware access control policies for your application resources.

Description

This project builds a docker image that can be used to run a Cerbos server in AWS Lambda. The images will contain the gateway executable and the Cerbos binary.

The following commands assume you run a Unix-like system with x86_64 or arm64 architectures.

There's also an example of AWS Lambda function based on this image. The function is built using AWS SAM model.

Prerequisites

The following tools are required:

  • Make - build automation tool
  • AWS CLI
  • AWS SAM CLI - if you wish to use the provided AWS Lambda function template
  • Docker
  • jq - If you want the Makefile to automatically detect the version of Cerbos. Otherwise use the CERBOS_RELEASE environment variable to specify the Cerbos version to use.

Build the Docker image

Check out conf.default.yml for Cerbos configuration. The default configuration uses blob storage, e.g. AWS S3 bucket. Cerbos config can read from environment variables. If you choose to do so, your AWS Lambda has to expose them.

By default, the latest release of Cerbos is used. If you want to use a particular Cerbos version, you can specify it in CERBOS_RELEASE environment variable.

Run the following command to build the docker image 'cerbos/aws-lambda-gateway':

make image

Note that the image will be built in whatever architecture you are running on (x86 or arm64) - the AWS region you use must support the architecture you are deploying too also else you will get an exec format error when it tries to start up the lambda.

Publish the Docker image

To publish the image, you will need to have an AWS ECR repository. You can create one in the AWS console or using AWS CLI with the following command (replace <repository-name> with the name of your repository):

You will see repositoryUri in the output of the command. Save it for later use.

aws ecr create-repository --repository-name <repository-name> --image-scanning-configuration scanOnPush=true

Then you will need to get an authentication token for the repository. You can do it with the following command:

export ECR_REPOSITORY_URL=<repositoryUri>
aws ecr get-login-password  | docker login --username AWS --password-stdin $ECR_REPOSITORY_URL

Now you can publish the image with the following command:

make publish-image

Create AWS Lambda function

You can create an AWS Lambda function referencing the published image with any tool. Alternatively, you can use the provided template sam.yml. For the latter option, please visit the template and replace <repositoryUri> with the value you saved in the previous step. The template exposes these environment variables:

  • BUCKET_URL - the URL of the S3 bucket where Cerbos policies are stored.
  • BUCKET_PREFIX - optional prefix for the S3 bucket.
  • CERBOS_LOGGING_LEVEL - Cerbos logging level. It defaults to INFO.

You will need to grant the role access to the S3 bucket you are storing policies in.

To publish the function, run the following command:

make publish-lambda

The command will create an AWS Lambda function as part of the stack called as per CERBOS_STACK_NAME environment variable (if unset, defaults to Cerbos). The stack will also create API Gateway resources and an IAM role for the function. Ensure the role has the necessary permissions to access the S3 bucket (or other policy storage you might use).

Should you change the configuration and rebuild the image, you can update the Lambda via:

make clean
make image
make publish-image
make update-lambda

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.