|
| 1 | +<!-- |
| 2 | +Copyright (c) 2024 Oracle and/or its affiliates. |
| 3 | +
|
| 4 | +The Universal Permissive License (UPL), Version 1.0 |
| 5 | +
|
| 6 | +Subject to the condition set forth below, permission is hereby granted to any |
| 7 | +person obtaining a copy of this software, associated documentation and/or data |
| 8 | +(collectively the "Software"), free of charge and under any and all copyright |
| 9 | +rights in the Software, and any and all patent rights owned or freely |
| 10 | +licensable by each licensor hereunder covering either (i) the unmodified |
| 11 | +Software as contributed to or provided by such licensor, or (ii) the Larger |
| 12 | +Works (as defined below), to deal in both |
| 13 | +
|
| 14 | +(a) the Software, and |
| 15 | +(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 16 | +one is included with the Software (each a "Larger Work" to which the Software |
| 17 | +is contributed by such licensors), |
| 18 | +
|
| 19 | +without restriction, including without limitation the rights to copy, create |
| 20 | +derivative works of, display, perform, and distribute the Software and make, |
| 21 | +use, sell, offer for sale, import, export, have made, and have sold the |
| 22 | +Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 23 | +either these or other terms. |
| 24 | +
|
| 25 | +This license is subject to the following condition: |
| 26 | +The above copyright notice and either this complete permission notice or at |
| 27 | +a minimum a reference to the UPL must be included in all copies or |
| 28 | +substantial portions of the Software. |
| 29 | +
|
| 30 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 31 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 32 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 33 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 34 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 35 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 36 | +SOFTWARE. |
| 37 | +--> |
| 38 | + |
| 39 | +## Functions overview |
| 40 | +[Document functions-overview](files/Fn.pdf) |
| 41 | + |
| 42 | +### Author |
| 43 | +<a href="https://github.com/mikarinneoracle">mikarinneoracle</a> |
| 44 | + |
| 45 | +# A Java Hello World function with local dev and then building and deploying to OCI Functions |
| 46 | + |
| 47 | +This is an example how I'm locally developing and testing OCI Functions on my mac with Apple silicon that uses mainly ARM architecture and then building and deploying the same to Functions in OCI. |
| 48 | + |
| 49 | +### Local Dev |
| 50 | + |
| 51 | +I've installed maven and <code>Fn cli</code> on my mac. This is how to do the <a href="https://docs.oracle.com/en-us/iaas/Content/Functions/Tasks/functionsinstallfncli.htm">Fn cli install</a> or like in here <a href="https://fnproject.io/tutorials/install/">following the Fn tutorial</a>. |
| 52 | + |
| 53 | +<p> |
| 54 | + |
| 55 | +To develop functions locally I'm running the Fn server; to do this I'm using this command with Rancher Desktop instead of doing the usual <code>fn start</code> that works for me: |
| 56 | + |
| 57 | +<pre> |
| 58 | +docker run --rm -i --name fnserver \ |
| 59 | +-v /tmp/iofs:/iofs \ |
| 60 | +-e FN_IOFS_DOCKER_PATH=/tmp/iofs \ |
| 61 | +-e FN_IOFS_PATH=/iofs \ |
| 62 | +-v /tmp/data:/app/data \ |
| 63 | +-v /var/run/docker.sock:/var/run/docker.sock \ |
| 64 | +--privileged \ |
| 65 | +-p 8080:8080 \ |
| 66 | +--entrypoint ./fnserver \ |
| 67 | +-e FN_LOG_LEVEL=DEBUG fnproject/fnserver:latest |
| 68 | +</pre> |
| 69 | + |
| 70 | +<p> |
| 71 | +Now that the Fn server is running create an application for our function using the cli: |
| 72 | + |
| 73 | +<pre> |
| 74 | +fn create app hellofunction |
| 75 | +</pre> |
| 76 | + |
| 77 | +<p> |
| 78 | + |
| 79 | +Then clone this repo and project and cd to the project root directory. Then run Fn cli command to build and deploy the function locally: |
| 80 | + |
| 81 | +<pre> |
| 82 | +fn --verbose deploy --app hellofunction --local |
| 83 | +</pre> |
| 84 | + |
| 85 | +<p> |
| 86 | + |
| 87 | +After this you are ready to call the function to test it: |
| 88 | + |
| 89 | +<pre> |
| 90 | +fn invoke hellofunction hellofunc |
| 91 | +Hello, world! |
| 92 | +</pre> |
| 93 | + |
| 94 | +or |
| 95 | + |
| 96 | +<pre> |
| 97 | +echo 'Mika' | fn invoke hellofunction hellofunc |
| 98 | +Hello, Mika! |
| 99 | +</pre> |
| 100 | + |
| 101 | +### OCI |
| 102 | + |
| 103 | +Now that the function is properly working locally let's build and deploy it to OCI. |
| 104 | + |
| 105 | +<p> |
| 106 | + |
| 107 | +First create a OCIR repo for the container in your OCI tenancy <code>compartment</code> you want push the container into. Unless you do this first the container will be pushed to the <code>root</code> comaprtment that is not a good idea and will usually lead to 404 or 502 errors when the OCI Function pulling the container due to missing OCI policies. You can create the OCIR repo from the Cloud UI or using the <a href="https://docs.oracle.com/en-us/iaas/tools/oci-cli/3.41.0/oci_cli_docs/cmdref/artifacts/container/repository/create.html">oci cli</a>. |
| 108 | + |
| 109 | +<p> |
| 110 | + |
| 111 | +Next, let's build the Docker container for the function using <code>docker build</code>, let's not use Fn this time (that is also possible), using the FRA region: |
| 112 | + |
| 113 | +<pre> |
| 114 | +docker build -t fra.ocir.io/<YOUR OCI TENANCY NAMESPACE>/helloworld-java:1 . |
| 115 | +</pre> |
| 116 | + |
| 117 | +In the docker build command above replace the <code>region</code> if necessary and the <code><YOUR OCI TENANCY NAMESPACE></code> with yours. |
| 118 | + |
| 119 | +<p> |
| 120 | + |
| 121 | +After building let's do <code>docker login</code> and <code>docker push</code> to push the container to the OCIR repo: |
| 122 | + |
| 123 | +<p> |
| 124 | + |
| 125 | +<pre> |
| 126 | +docker login ams.ocir.io -u '<YOUR OCI TENANCY NAMESPACE>/oracleidentitycloudservice/<YOUR USERNAME>' -p '<YOUR ACCESS TOKEN>' |
| 127 | +docker push fra.ocir.io/<YOUR OCI TENANCY NAMESPACE>/helloworld-java:1 |
| 128 | +</pre> |
| 129 | + |
| 130 | +<p> |
| 131 | + |
| 132 | +The last step is to create the Function Application and the function deployment for it. This can be easily done using the Cloud UI. |
| 133 | + |
| 134 | +<p> |
| 135 | + |
| 136 | +To create a mac binary compatible Function Application use the <code>shape GENERIC_ARM</code>. Let's name this <code>hello-arm</code>. |
| 137 | + |
| 138 | +<p> |
| 139 | + |
| 140 | +Using the application create a function for it from the container image that was pushed to the OCIR repo e.g. <code>fra.ocir.io/<YOUR OCI TENANCY NAMESPACE>/helloworld-java:1</code>. Let's name this <code>helloworld-java-arm</code>. |
| 141 | + |
| 142 | +<p> |
| 143 | + |
| 144 | +After deployment the Function can be easily tested in Cloud Shell following the Getting Started guide for the Application and giving: |
| 145 | + |
| 146 | +<pre> |
| 147 | +echo 'Mika' | fn invoke hello-arm helloworld-java-arm |
| 148 | +Hello, Mika! |
| 149 | +</pre> |
| 150 | + |
| 151 | +<p> |
| 152 | + |
| 153 | +Optionally can create API Gateway for the function that will assign a public endpoint to it over HTTPS that can be easily tested with curl: |
| 154 | + |
| 155 | +<pre> |
| 156 | +curl -d "Mika" https://n3yu.....ghhi.apigateway.eu-frankfurt-1.oci.customer-oci.com/ |
| 157 | +Hello, Mika! |
| 158 | +</pre> |
| 159 | + |
| 160 | +## License |
| 161 | + |
| 162 | +Copyright (c) 2024 Oracle and/or its affiliates. |
| 163 | + |
| 164 | +Licensed under the Universal Permissive License (UPL), Version 1.0. |
| 165 | + |
| 166 | +See [LICENSE](LICENSE) for more details. |
| 167 | + |
| 168 | +ORACLE AND ITS AFFILIATES DO NOT PROVIDE ANY WARRANTY WHATSOEVER, EXPRESS OR IMPLIED, FOR ANY SOFTWARE, MATERIAL OR CONTENT OF ANY KIND CONTAINED OR PRODUCED WITHIN THIS REPOSITORY, AND IN PARTICULAR SPECIFICALLY DISCLAIM ANY AND ALL IMPLIED WARRANTIES OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE. FURTHERMORE, ORACLE AND ITS AFFILIATES DO NOT REPRESENT THAT ANY CUSTOMARY SECURITY REVIEW HAS BEEN PERFORMED WITH RESPECT TO ANY SOFTWARE, MATERIAL OR CONTENT CONTAINED OR PRODUCED WITHIN THIS REPOSITORY. IN ADDITION, AND WITHOUT LIMITING THE FOREGOING, THIRD PARTIES MAY HAVE POSTED SOFTWARE, MATERIAL OR CONTENT TO THIS REPOSITORY WITHOUT ANY REVIEW. USE AT YOUR OWN RISK. |
0 commit comments