|
| 1 | +# Automatically Resize VMs |
| 2 | +Automatically resize VMs that exceed memory consumption. The function code is referenced in the OCI documentation at https://docs.cloud.oracle.com/en-us/iaas/Content/Notification/Tasks/scenarioa.htm. |
| 3 | + |
| 4 | +This use case involves writing a function to resize VMs and creating an alarm that sends a message to that function. When the alarm fires, the Notifications service sends the alarm message to the destination topic, which then fans out to the topic's subscriptions. In this scenario, the topic's subscriptions include the function as well as your email. The function is invoked on receipt of the alarm message. |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +As you make your way through this tutorial, look out for this icon . |
| 9 | +Whenever you see it, it's time for you to perform an action. |
| 10 | + |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | +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](https://www.oracle.com/webfolder/technetwork/tutorials/infographics/oci_functions_cloudshell_quickview/functions_quickview_top/functions_quickview/index.html) |
| 14 | +* A - Set up your tenancy |
| 15 | +* B - Create application |
| 16 | +* C - Set up your Cloud Shell dev environment |
| 17 | + |
| 18 | + |
| 19 | +## List Applications |
| 20 | +Assuming your have successfully completed the prerequisites, you should see your |
| 21 | +application in the list of applications. |
| 22 | +``` |
| 23 | +fn ls apps |
| 24 | +``` |
| 25 | + |
| 26 | + |
| 27 | +## Create or Update your Dynamic Group |
| 28 | +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](https://docs.cloud.oracle.com/iaas/Content/Identity/Tasks/managingdynamicgroups.htm#To). |
| 29 | + |
| 30 | +When specifying the *Matching Rules*, we suggest matching all functions in a compartment with: |
| 31 | +``` |
| 32 | +ALL {resource.type = 'fnfunc', resource.compartment.id = 'ocid1.compartment.oc1..aaaaaxxxxx'} |
| 33 | +``` |
| 34 | +Please check the [Accessing Other Oracle Cloud Infrastructure Resources from Running Functions](https://docs.cloud.oracle.com/en-us/iaas/Content/Functions/Tasks/functionsaccessingociresources.htm) for other *Matching Rules* options. |
| 35 | + |
| 36 | + |
| 37 | +## Create or Update IAM Policies |
| 38 | +Create a new policy that allows the dynamic group to *use instances*. |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +Your policy should look something like this: |
| 43 | +``` |
| 44 | +Allow dynamic-group <dynamic-group-name> to use instances in compartment <compartment-name> |
| 45 | +``` |
| 46 | +For more information on how to create policies, check the [documentation](https://docs.cloud.oracle.com/iaas/Content/Identity/Concepts/policysyntax.htm). |
| 47 | + |
| 48 | + |
| 49 | +## Review and customize your function |
| 50 | +Review the following files in the current folder: |
| 51 | +* the code of the function, [func.py](./func.py) |
| 52 | +* its dependencies, [requirements.txt](./requirements.txt) |
| 53 | +* the function metadata, [func.yaml](./func.yaml) |
| 54 | + |
| 55 | +The following piece of code in [func.py](./func.py) should be updated to match your needs: |
| 56 | +``` |
| 57 | + if current_shape == "VM.Standard1.1": |
| 58 | + new_shape = "VM.Standard2.1" |
| 59 | + elif current_shape == "VM.Standard2.1": |
| 60 | + new_shape = "VM.Standard2.2" |
| 61 | +``` |
| 62 | + |
| 63 | +The code in [list_vm_shapes.py](./list_vm_shapes.py) can be used to improve the logic of VM shape selection. |
| 64 | + |
| 65 | + |
| 66 | +## Deploy the function |
| 67 | +In Cloud Shell, run the fn deploy command to build the function and its dependencies as a Docker image, |
| 68 | +push the image to OCIR, and deploy the function to Oracle Functions in your application. |
| 69 | + |
| 70 | + |
| 71 | +``` |
| 72 | +fn -v deploy --app <your app name> |
| 73 | +``` |
| 74 | +e.g. |
| 75 | +``` |
| 76 | +fn -v deploy --app myapp |
| 77 | +``` |
| 78 | + |
| 79 | + |
| 80 | +## Configure Oracle Notification Service |
| 81 | +This section walks through creating an alarm using the Console and then updating the ONS topic created with the alarm. |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +On the OCI console, navigate to *Monitoring* > *Alarm Definitions*. Click *Create Alarm*. |
| 86 | + |
| 87 | +On the Create Alarm page, under Define alarm, set up your threshold: |
| 88 | + |
| 89 | +Metric description: |
| 90 | +* Compartment: (select the compartment that contains your VM) |
| 91 | +* Metric Namespace: oci_computeagent |
| 92 | +* Metric Name: MemoryUtilization |
| 93 | +* Interval: 1m |
| 94 | +* Statistic: Max |
| 95 | + |
| 96 | +Trigger rule: |
| 97 | +* Operator: greater than |
| 98 | +* Value: 90 (or lower for testing purposes) |
| 99 | +* Trigger Delay Minutes: 1 |
| 100 | + |
| 101 | +Select your function under Notifications, Destinations: |
| 102 | +* Destination Service: Notifications Service |
| 103 | +* Compartment: (select the compartment where you want to create the topic and associated subscriptions) |
| 104 | + |
| 105 | +Topic: Click *Create a topic* |
| 106 | +* Topic Name: Alarm Topic |
| 107 | +* Subscription Protocol: Function |
| 108 | +* Function Compartment: (select the compartment that contains your function) |
| 109 | +* Function Application: (select the application that contains your function) |
| 110 | +* Function: (select your function) |
| 111 | +* Click *Create topic and subscription* |
| 112 | + |
| 113 | +Click Save alarm. |
| 114 | + |
| 115 | + |
| 116 | +## Test ONS > Fn |
| 117 | +First, test the function indivudually. |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +Update section "resourceId" in [test-alarm.json](./test-alarm.json) with the OCID of the instance you want to update. |
| 122 | + |
| 123 | +Invoke the function as follows: |
| 124 | +``` |
| 125 | +cat test-alarm.json | fn invoke <your app name> <function name> |
| 126 | +``` |
| 127 | +e.g.: |
| 128 | +``` |
| 129 | +cat test-alarm.json | fn invoke myapp oci-ons-compute-shape-increase-python |
| 130 | +``` |
| 131 | + |
| 132 | +Now, the whole flow can be tested. Connect to an instance in the compartment where the alarm is active, and stress the memory utilization with the *stress* utility for example. |
0 commit comments