This is an example of a function to check if a compute instance is tagged correctly on provisioning. If not, stop the instance.
This function is trigged by the compute event -- Instance - Launch End which is generated by compute at the completion of instance provisioning whether it succeeds or fails.
This sample uses Oracle cloud infrastructure search to search for compute resources with the freeform tag with a tag key of costcenter and tag value of 1234.
Uses the OCI Python SDK to create a client that receive user information when called in the OCI or a valid config file exists.
As you make your way through this tutorial, look out for this icon . Whenever you see it, it's time for you to perform an action.
Before you deploy this sample function, make sure you have run step A, B and C of the Oracle Functions Quick Start Guide for Cloud Shell
- A - Set up your tenancy
- B - Create application
- C - Set up your Cloud Shell dev environment
Assuming your have successfully completed the prerequisites, you should see your application in the list of applications.
fn ls apps
In order to use other OCI Services, your function must be part of a dynamic group. For information on how to create a dynamic group, refer to the documentation.
When specifying the Matching Rules, we suggest matching all functions in a compartment with:
ALL {resource.type = 'fnfunc', resource.compartment.id = 'ocid1.compartment.oc1..aaaaaxxxxx'}
Please check the Accessing Other Oracle Cloud Infrastructure Resources from Running Functions for other Matching Rules options.
Create a new policy that allows the dynamic group to use instance-family.
Your policy should look something like this:
Allow dynamic-group <dynamic-group-name> to use instance-family in compartment <compartment-name>
For more information on how to create policies, check the documentation.
Review the following files in the current folder:
- the code of the function, func.py
- its dependencies, requirements.txt
- the function metadata, func.yaml
In Cloud Shell, run the fn deploy command to build the function and its dependencies as a Docker image, push the image to OCIR, and deploy the function to Oracle Functions in your application.
fn -v deploy --app <your app name>
e.g.
fn -v deploy --app myapp
To create a new cloud event rule, click on Application Integration -> Events Service in the sidebar menu:
In this case, the event is Instance - Launch End and the action to take is to call the function stop-untagged-instance
Launch an instance with no tags
In a few seconds you should see that the instance has stopped because it wasn't tagged.
Learn how to configure basic observability for your function using metrics, alarms and email alerts: