Thursday, January 14, 2021

Notify OCI Compute state change using OCI Events

7:52 PM Posted by Dilli Raj Maharjan No comments

The Oracle Cloud Infrastructure Notifications service is used to broadcast message to subscribers. It can be used to notify when event rules are triggered or alarmed or breached. It uses topics and subscriptions for publishing messages. Topic need to created and should be subscribed by the user before OCI notification service works. Once Topic and subscription is created we will create Event Rule to filter out the events we are interested one.


Create Notification Topics and Subscription.


Click on Navigation menu. Click on Application Integration and then Notifications.



Click on Create Topic.


Provide the name of topic and Description. Click on Create.




Once topic is created, Click on Create Subscription to create subscriptions.


There is an option to create topic using 5 protocols. I am using my email to subscribe this topic.



Provide email address and click on Create.



The Subscription status will be in pending state until you open the confirmation email and click on Confirm Subscription.


Open email and click on Confirm Subscription to complete subscription.

Once you click on confirm subscription, the state will be changed to Active.

Create Event and action.

Click on Navigation menu, Application Integration and Events Service.



Click on Rules and Create Rule button.


Provide the Rule name, Description, Rule conditions and Actions. Click on Create Rule once done.



Now whenever your compute instance state changes, it will send notification email as below. 












Wednesday, January 13, 2021

Deploy event-driven function for converting text file uploaded to bucket to PDF

9:59 PM Posted by Dilli Raj Maharjan No comments


    Before creating application and deploying event-driven function for converting text document that is uploaded to bucket to PDF, fn cli should be installed and configured in a machine. Click here to install and configure fn cli in OCI compute instance linux machine.

Creating OCI object bucket to upload text file and store pdf file.


Create two Object storage buckets: in-bucket to upload text file by the client and out-bucket to upload converted PDF file by the function.



Click on in-bucket, where client will upload text file. Make sure Emit Object Events is enabled. Click on Edit to enable it.


Clone fn code.

Create required directory and use git command to clone fn-text2pdf-events repository. Function fn-text2pdf-events is written in Go programming language and available in git location https://github.com/abhirockzz/fn-text2pdf-events. I am not the author of the code you can get more details about it in the git repo readme page.
cd ~opc
mkdir fn
cd fn
git clone https://github.com/abhirockzz/fn-text2pdf-events.git



Configure fn context

The context specifies Oracle Functions endpoints, the OCID of the compartment to which deployed functions will belong, and the address of the Docker registry to and from which to push and pull images. Create fn context with command below.

fn create context Oracle-cs --provider oracle



Use newly created context with command below.
fn use context Oracle-cs



List all available context.
fn list context



Configure new context with compartment_ocid, api-url and docker registry. API Url must be in format https://functions.<region name>.oci.oraclecloud.com. Docker URL should in format <region-key>.ocir.io/<tenancy-namespace>/<repo-name>




Generate an auth token for the OCI user. Click here for details on generating Auth Token. and login to docker repository. Provide Auth Token password when prompted for password.

docker login -u '<tenancy-namespace>/<OCU username>'  <region-key>.ocir.io



Copy API signing key to the function source directory. In my case deployment failed without copying the key file to the directory 



The function code is written in Go programming languages and use gofpdf for text to PDF conversion. It uses the OCI Go SDK to execute Object Storage read and write operation. During build it will download dependent Go packages. Create required directory and change ownership to OS user opc.


Create app with command below
fn create app text2pdf --annotation oracle.com/oci/subnetIds=<SUBNETS> --config TENANT_OCID=<TENANT_OCID> --config USER_OCID=<USER_OCID> --config FINGERPRINT=<FINGERPRINT> --config PASSPHRASE=<PASSPHRASE> --config REGION=<REGION> --config PRIVATE_KEY_NAME=<PRIVATE_KEY_NAME> --config OUTPUT_BUCKET=<OUTPUT_BUCKET>



Change directory to the function source and deploy function inside the newly created application.



Once function is fully deployed we will notice Successfully created function message at the end of the deployment.



We can check newly create function from the OCI Console too. Click on the application and we can notice the name of the function and its image location.




Create Oracle Cloud Infrastructure Event

Create OCI Event to trigger function as action for file upload in in-bucket Object bucket. Before executing OCI Event we need to add Policy to allow cloudevents and faas service to use required OCI Services

Following are the policy statement Added to the policy.
allow service cloudevents to use ons-topic in tenancy
allow service cloudevents to use function-family in tenancy
allow service cloudevents to read streams in tenancy
allow service cloudevents to use stream-push in tenancy
allow service cloudevents to manage object-family in tenancy
allow service faas to manage object-family in tenancy



Creating OCI Events.


Click on Navigation Menu. Click on Application Integration > Events Service.



On the Event service page Click on Create Rule.



Provide Display Name for the rule, Provide the condition, Service Name, Event Type. In my case I have added first condition to filter out the object storage object create event. I have added addition condition which will filter the name of the bucket and it will check name of the bucket should be in-bucket. Once the condition is met function need to be executed. Name of function Application is app-text2pdf and the function is convert2pdf. Click on Create Rule to create the rule.




Test OCI Event

Click on in-bucket and click on Upload to upload file to the bucket.



Click on select files, choose text file and once files are selected Click on Upload to begin the upload.




Confirm that text file has been successfully uploaded to in-bucket.

We can notice OCI function has converted the text file into pdf.








Install Fn Project CLI for Oracle Cloud Infrastructure Compute Instance

5:28 AM Posted by Dilli Raj Maharjan No comments


    The Fn project is an open-source container-native serverless platform that you can run anywhere, any cloud or on-premise. It’s easy to use, supports every programming language, and is extensible and performant. Fn is an event-driven, open source, Functions-as-a-Service (FaaS) compute platform that you can run anywhere. 

During installation of fn project cli for Oracle Cloud Infrastructure docker, oci-cli should be installed and configured. Following are the step by step guide to install fn project cli in OCI compute instance.

Create Compute instance from OCI Console.


Click on Navigation menu, click on Compute and click on Instances.



Click on Create Instance button in the Instance page.


Provide the name of Instance.



Select required Operating system. In my case I have select Oracle Linux 8.



Provide SSH Public keys. I have provided 2 public key to connect the instance from. Finally click on Create to create instance.



Once instance is created login to the instance via ssh as user opc.



Install docker and git redhat packages. 


For Linux 7:
sudo yum -y install docker git


For Linux 8:
sudo yum -y install git

Install yum utilities.
sudo yum -y install yum-utils




Add Linux 8 yum docker repo.
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo



Execute install docker redhat packages.
sudo yum install docker-ce docker-ce-cli containerd.io



Once docker is installed, enable docker at startup and start docker services.
systemctl stop docker
systemctl start docker


Intall fn project cli


Install fn project with command below. Click here if you want more in fn cli installation. 
curl -LSs https://raw.githubusercontent.com/fnproject/cli/master/install | sh


Once fn project is installed, install and configure oci-cli with command below. Click here if you need more info in oci-cli installation and setup.
bash -c "$(curl -L https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.sh)"


Generate an API Signing Key


An API Signing key should be generated and uploaded or pasted an API Key in console. Click here to learn more about creating API signing key and uploading. In my case I have already generated and uploaded signing key.



Setup oci-cli

Collect OCI informations as tenancy_ocid, user_ocid, FINGERPRINT etc prior to setup oci.


Setup oci-cli with command below. Provide appropriate information.

oci setup config