Tuesday, April 18, 2023

OCI API Gateway route to OCI function and test from Postman

Usecase: API Gateway to route to a function and then test it from POSTMAN.

Highlevel:

  1. Create a hello world java function in OCI.
  2. Create an API Gateway and route it to the function.
  3. Add policy to access to the compartment.
  4. Test the API gateway rest service from postman.
Detailed steps:

Step1: Create a java hello-world function
Navigation >> developer services >> Fuctions >> Applications

Follow my below blog : how to create a function:


Here, I have already created the function.


Step2: create an API gateway and route to function as deployment.

Follow my below blog: How to create API gateway and route.

Navigation >> Developer Services >> API Management >> Gateways


I have already created couple of API gatways already. We will use one gateway and add anothet route.

Choose compartment >> click on one gateway


Deployment >> demo-api >>edit



Add another route >> 
Add path: /hello-java, method: POST, Gateway type: Oracle function, 

Choose compartment >> function app name >> function  name >> save


Function route added >> copy the endpoint for testing.


Step3: add a policy to the compartment so that any user can access.

Navigation >> Identity & Security >> Policies >> Create policy.


Here, I have already added the policy.
Policy details:

Allow any-user to use functions-family in compartment dev where ALL { request.principal.type='ApiGateway',request.resource.compartment.id='ocid1.compartment.oc1..aaaaaa.....'}



How to get the compartment id:
Navigation >> Identity & Security >> Compartments >> open your compartment and see the OCID >> just copy it and use it in the Policy.



We are ready now for testing:

Step4: Test Api gateway from postman

Postman >>
Method: post
Test URL : <endpoint of the deployement>/<route path>
Authorization >> basic >> add user and password for OCI console.


Raw >> test >> input:  Sanddy >> output : Hello, Sanddy !


Note: if we dont add the policy to the compartment where function resides, it will fail with 500 Internal server error.

No comments:

Post a Comment

Featured Post

11g to 12c OSB projects migration points

1. Export 11g OSB code and import in 12c Jdeveloper. Steps to import OSB project in Jdeveloper:   File⇾Import⇾Service Bus Resources⇾ Se...