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.