Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Example of Serverless Auth REST-API (API-Gateway-Lambda) #121

Open
csantanapr opened this issue Apr 28, 2023 · 2 comments
Open
Assignees

Comments

@csantanapr
Copy link
Member

Is your feature request related to a problem? Please describe

A few users are looking for an example to implement a REST-API using API-Gateway and Lambda with a Lambda authorizer

Describe the solution you'd like

Using upbound provider to create an example

@giedri
Copy link
Contributor

giedri commented Jan 25, 2024

Here is example:
https://github.com/awslabs/crossplane-on-eks/tree/main/examples/upbound-aws-provider/composite-resources/serverless-examples/microservice

It uses new composition at https://github.com/awslabs/crossplane-on-eks/tree/main/compositions/upbound-aws-provider/apigw

Composition uses Amazon API Gateway REST API and can be extended to use HTTP API or include additional features (such as usage plans, private integration targets, etc.) as needed.

@giedri
Copy link
Contributor

giedri commented Jan 25, 2024

API

API uses API Gateway REST API endpoint type with OpenAPI definition that includes proxy resource. All requests are passed to the integration target (AWS Lambda) for routing and interpretation/response generation. API Gateway does not implement any validation, transformation, path based routing, API management functions. Users would have to update OpenAPI specification to implement those features.

API Gateway uses Lambda Authorizer for authentication/authorization. However, sample implementation at ./src/authorizer/lambda_function.py allows all actions on all resources in the API if the Authorization header value in the request matches the one stored in the AWS Secrets Manager and retrieved by the Lambda Authorizer when it initializes.

Users shall update authorizer Lambda code according to their authentication/authorization needs. For more details on how to implement Lambda Authorizer, they can check out documentation. or blueprints, Lambda Authorizer code at serverless-rest-api for JWT based authorization examples.

Business logic

API Gateway passes all the incoming requests to the Lambda function and returns response back to the API client. Sample implementation code is available at ./src/logic/lambda_function.py. It expects database table name to be specified in the environment variable TABLE_NAME.

For HTTP GET requests to the API items resource it runs Amazon DynamoDB scan operation and returns all items received as a result. For HTTP GET requests for a particular item (the items{id} resource) it performs get_item operation and returns response from the DynamoDB. PUT request to items resource takes incoming payload, adds UUID as a hash key value, adds current timestamp, and performs DynamoDB put_item operation. It returns payload sent to the Dynamo DB as a response body to the API client.

Database

Example uses DynamoDB table to store data. Database definition is hardcoded in the composition and includes just a single required id field that is used as a hash key. Users would need to modify this structure and business logic Lambda code to implement anything more complicated than simple CRUD operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants