|
1 | | -# Oracle Functions Hello World |
| 1 | +# Function Hello World |
2 | 2 |
|
3 | | -Get started with Java, Nodejs, Python, Golang and Ruby functions on Oracle Functions |
| 3 | +This function returns the "Hello World" message or "Hello <name>" when you provide a name in the function call payload. |
4 | 4 |
|
5 | | -- Pre-requisite - [Create Application](create-application.md) |
6 | | -- [Java](java/README.md) |
7 | | -- [Nodejs](node/README.md) |
8 | | -- [Python](python/README.md) |
9 | | -- [Golang](golang/README.md) |
10 | | -- [Ruby](ruby/README.md) |
| 5 | +As you make your way through this tutorial, look out for this icon . |
| 6 | +Whenever you see it, it's time for you to perform an action. |
| 7 | + |
| 8 | + |
| 9 | +## Pre-requisites |
| 10 | +1. Start by making sure all of your policies are correct from this [guide](https://docs.cloud.oracle.com/iaas/Content/Functions/Tasks/functionscreatingpolicies.htm?tocpath=Services%7CFunctions%7CPreparing%20for%20Oracle%20Functions%7CConfiguring%20Your%20Tenancy%20for%20Function%20Development%7C_____4) |
| 11 | + |
| 12 | +2. Have [Fn CLI setup with Oracle Functions](https://docs.cloud.oracle.com/iaas/Content/Functions/Tasks/functionsconfiguringclient.htm?tocpath=Services%7CFunctions%7CPreparing%20for%20Oracle%20Functions%7CConfiguring%20Your%20Client%20Environment%20for%20Function%20Development%7C_____0) |
| 13 | + |
| 14 | + |
| 15 | +## Create an Application to run your function |
| 16 | +You can use an application already created or create a new one using either the CLI or the OCI console. |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +### using the CLI |
| 21 | +``` |
| 22 | +fn create app <app-name> --annotation oracle.com/oci/subnetIds='["<subnet-ocid>"]' |
| 23 | +``` |
| 24 | +Get the OCID of the subnet in your VCN you wish to use. |
| 25 | + |
| 26 | +e.g. |
| 27 | +``` |
| 28 | +fn create app myapp --annotation oracle.com/oci/subnetIds='["ocid1.subnet.oc1.phx.aaaaaaaacnh..."]' |
| 29 | +``` |
| 30 | + |
| 31 | +### using the OCI console |
| 32 | +Log in to the [OCI console](https://console.us-phoenix-1.oraclecloud.com/) with your account, select the same region and compartment you specified when you configured the Fn CLI context. |
| 33 | + |
| 34 | +On the OCI console, navigate to Developer Services > Functions. Click `Create Application` and specify: |
| 35 | +- The name for the new application as *myapp*. |
| 36 | +- The VCN and subnet in which to run the function. |
| 37 | + |
| 38 | +Click `Create`. |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +## Writing the code of function |
| 44 | + |
| 45 | +The [Python folder](./python) contains the files to deploy the HelloWorld function in Python: |
| 46 | +* the code of the function, [func.py](./python/func.py) |
| 47 | +* its dependencies, [requirements.txt](./python/requirements.txt) |
| 48 | +* the function metadata, [func.yaml](./python/func.yaml) |
| 49 | + |
| 50 | +The [Java folder](./java) contains the files to deploy the HelloWorld function in Java: |
| 51 | +* the code of the function, [src/main/java/com/example/fn/HelloFunction.java](./java/src/main/java/com/example/fn/HelloFunction.java) |
| 52 | +* its dependencies, [pom.xml](./java/pom.xml) |
| 53 | +* the function metadata, [func.yaml](./java/func.yaml) |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +You can also generation your own HelloWorld function files by running the following command from your terminal: |
| 58 | + |
| 59 | +``` |
| 60 | +fn init --runtime <runtime-language> helloworld |
| 61 | +``` |
| 62 | + |
| 63 | +where <runtime-language> is one of the supported runtime languages (currently go, java, node, and python are supported). |
| 64 | + |
| 65 | +For example: |
| 66 | +``` |
| 67 | +fn init --runtime python helloworld |
| 68 | +``` |
| 69 | + |
| 70 | +A directory called *helloworld* is created containing the necessary files to deploy the HelloWorld function. |
| 71 | + |
| 72 | + |
| 73 | +## Deploy the function |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +Change directory to the *helloworld* directory created in the previous step: |
| 78 | + |
| 79 | +``` |
| 80 | +cd helloworld |
| 81 | +``` |
| 82 | + |
| 83 | +If you did not generate your own function and you are deploying what is provided in this repo, change to either `Java` or `Python`. |
| 84 | + |
| 85 | +To deploy the function, run the following command: |
| 86 | +``` |
| 87 | +fn -v deploy --app <your app name> |
| 88 | +``` |
| 89 | +e.g. |
| 90 | +``` |
| 91 | +fn -v deploy --app myapp |
| 92 | +``` |
| 93 | + |
| 94 | + |
| 95 | +## Invoke the function |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +Invoke the *helloworld* function by entering: |
| 100 | + |
| 101 | +``` |
| 102 | +fn invoke <your app name> helloworld |
| 103 | +``` |
| 104 | +e.g. |
| 105 | +``` |
| 106 | +fn invoke myapp helloworld |
| 107 | +``` |
| 108 | + |
| 109 | +The Python version displays `{"message":"Hello World!"}` and the Java version displays `Hello World!` |
| 110 | + |
| 111 | +You can also pass in a payload to invoke the function. |
| 112 | +For Java, run: |
| 113 | +``` |
| 114 | +echo -n "Bob" | fn invoke <your app name> helloworld |
| 115 | +``` |
| 116 | +The `Hello Bob!` output is displayed. |
| 117 | +For Python, run: |
| 118 | +``` |
| 119 | +echo -n '{"name":"Bob"}' | fn invoke <your app name> helloworld |
| 120 | +``` |
| 121 | +The `{"message":"Hello Bob!"}` output is displayed. |
| 122 | + |
| 123 | +Congratulations! You've just created, deployed, and invoked the HelloWorld function using Oracle Functions! |
0 commit comments