Skip to content

Latest commit

 

History

History

oci-stop-untagged-instance-python

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Function for Stopping improperly tagged instance during provisioning

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 user input icon. Whenever you see it, it's time for you to perform an action.

Prerequisites

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

List Applications

Assuming your have successfully completed the prerequisites, you should see your application in the list of applications.

fn ls apps

Create or Update your Dynamic Group

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 or Update IAM Policies

Create a new policy that allows the dynamic group to use instance-family.

user input icon

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 and customize your function

Review the following files in the current folder:

Deploy the function

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.

user input icon

fn -v deploy --app <your app name>

e.g.

fn -v deploy --app myapp

Test

Create A Cloud Event

To create a new cloud event rule, click on Application Integration -> Events Service in the sidebar menu:

user input icon

event service image

Click on 'Create Rule' and populate the form:

user input icon

event rule image

In this case, the event is Instance - Launch End and the action to take is to call the function stop-untagged-instance

Launch a Compute instance

user input icon

Launch an instance with no tags

launch instance start

In a few seconds you should see that the instance has stopped because it wasn't tagged.

launch instance stop

Monitoring Functions

Learn how to configure basic observability for your function using metrics, alarms and email alerts: