Skip to content

Commit 2179952

Browse files
committed
Initial version of order-processing-typescript sample
Signed-off-by: Gerhard Poul <gerhard.poul@oracle.com>
1 parent 48f2095 commit 2179952

9 files changed

Lines changed: 4310 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ This repository provides examples demonstrating how to use Oracle Functions.
7373
|------------------------------------------------------|:------:|:----:|
7474
| Serverless SaaS Extensions using Oracle Functions, API Gateway and VBCS | [repo](https://github.com/oracle/cloud-asset-fusion-serverless-vbcs-sample) | [blog](https://www.ateam-oracle.com/the-cloud-native-approach-to-extending-your-saas-applications)
7575
| Function that demonstrates connectivity between Oracle SaaS applications with OIC | [sample](./samples/oci-oic-hcm-object-upload)|
76+
| TypeScript function migrated from AWS Lambda to OCI Functions | [sample](./samples/order-processing-typescript)|
7677

7778
## Documentation
7879

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM fnproject/node:20-dev as build-stage
2+
WORKDIR /function
3+
ADD package.json /function/
4+
ADD func.ts /function/
5+
RUN npm install && chown -R $(id -u):$(id -g) node_modules && npm run build
6+
FROM fnproject/node:20
7+
WORKDIR /function
8+
# Might want to add package lock file also for prod builds
9+
ADD package.json /function/
10+
# Instead of copy node_modules we reinstall without dev dependencies to get a smaller image
11+
RUN npm install --omit=dev && chown -R $(id -u):$(id -g) node_modules && du -hs /function
12+
# Copy the built JavaScript sources from build-stage
13+
COPY --from=build-stage /function/dist/ /function/
14+
RUN chmod -R o+r /function
15+
ENTRYPOINT ["node", "--enable-source-maps", "func.js"]
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Function receives an order JSON and creates an object in a bucket
2+
The JSON document type the function processes is defined in [func.ts](func.ts#L7-L11) and an example can be found in the guide below. The object is written to a target bucket configured through environment variables.
3+
4+
As an alternative to the OCI Object Storage API this function also shows how to use the OCI Object Storage Amazon S3 Compatibility APIs. In production-usage you'd not use both at the same time, but we make it easy for testing purposes to change the used API through environment variables so you can experience both options.
5+
6+
7+
## Prerequisites
8+
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)
9+
* A - Set up your tenancy
10+
* B - Create application
11+
* C - Set up your Cloud Shell dev environment
12+
13+
14+
## List Applications
15+
Assuming you have successfully completed the prerequisites, you should see your application in the list of applications.
16+
```
17+
fn ls apps
18+
```
19+
20+
21+
## Create or Update your Dynamic Group
22+
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).
23+
24+
When specifying the *Matching Rules*, we suggest matching all functions in a compartment with:
25+
```
26+
ALL {resource.type = 'fnfunc', resource.compartment.id = 'ocid1.compartment.oc1..aaaaaxxxxx'}
27+
```
28+
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.
29+
30+
31+
## Create or Update IAM Policies
32+
Create a new policy that allows the dynamic group to write to the target bucket.
33+
34+
Your policy should look something like this:
35+
```
36+
Allow dynamic-group <dynamic-group-name> to use buckets in compartment <compartment-name>
37+
Allow dynamic-group <dynamic-group-name> to manage objects in compartment <compartment-name>
38+
```
39+
For more information on how to create policies, check the [documentation](https://docs.cloud.oracle.com/iaas/Content/Identity/Concepts/policysyntax.htm).
40+
41+
42+
## Review and customize your function
43+
Review the following files in the current folder:
44+
* the code of the function, [func.ts](./func.ts)
45+
* its dependencies, [package.json](./package.json)
46+
* its dockerfile, [Dockerfile](./Dockerfile)
47+
* the function metadata, [func.yaml](./func.yaml)
48+
49+
50+
## Set the function environment values
51+
The function requires the following environment values to be set:
52+
- OS_NAMESPACE, the value should be your tenancy's object storage namespace name
53+
54+
Configure the value before deployment in the `func.yaml` file.
55+
56+
57+
## Deploy the function
58+
In Cloud Shell, run the fn deploy command to build the function and its dependencies as a Docker image,
59+
push the image to OCIR, and deploy the function to Oracle Functions in your application.
60+
61+
```
62+
fn -v deploy --app <your app name>
63+
```
64+
e.g.
65+
```
66+
fn -v deploy --app myapp
67+
```
68+
69+
70+
## Invoke the function
71+
Invoke the function as follows:
72+
73+
```
74+
echo -n '<JSON message>' | fn invoke <your app name> order-processing
75+
```
76+
e.g.:
77+
```
78+
echo -n '{"orderId": "43", "price": 43.99, "product": "Coffee Beans"}' | fn invoke order-processing-app order-processing
79+
```
80+
81+
82+
## Monitoring Functions
83+
84+
Learn how to configure basic observability for your function using metrics, alarms and email alerts:
85+
* [Basic Guidance for Monitoring your Functions](../basic-observability/functions.md)
86+
87+
88+
## Writing to OCI Object Storage using the Amazon S3 Compatibility API
89+
To start using the OCI Object Storage Amazon S3 Compatibility API you need to set the following environment variables of the function:
90+
- TARGET_API, change the value from "OCI" to "S3" to switch the used API
91+
- AWS_ACCESS_KEY_ID, set this to the *Access key* of your created *Customer Secret Key*
92+
- AWS_SECRET_ACCESS_KEY, set this to the *Secret key* of your created *Customer Secret Key*
93+
94+
Configure the value before deployment in the `func.yaml` file or after deployment through the OCI Console.
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
declare module '@fnproject/fdk' {
2+
3+
export function handle(fnfunction: fnHandler, options: object): () => void;
4+
export function streamResult(stream: any): StreamResult;
5+
export function rawResult(res: string | Buffer): RawResult;
6+
/**
7+
* The function handler - This is a user-supplied node function that implements the behaviour of the current fn function
8+
*/
9+
export type fnHandler = (the: any, the: Context) => string | number | Promise<any> | null | Response;
10+
declare class StreamResult extends FnResult {
11+
constructor(stream: any);
12+
_stream: any;
13+
writeResult(ctx: any, resp: any): any;
14+
}
15+
declare class RawResult extends FnResult {
16+
constructor(raw: any);
17+
_raw: any;
18+
}
19+
/**
20+
* Context is the function invocation context - it enables functions to read and write metadata from the request including event headers, config and the underlying payload
21+
*/
22+
declare class Context {
23+
constructor(config: any, payload: any, headers: any);
24+
_config: any;
25+
_body: any;
26+
_headers: any;
27+
_responseHeaders: {};
28+
/**
29+
* Returns the deadline for the function invocation as a Date object
30+
* @returns {Date}
31+
*/
32+
get deadline(): Date;
33+
/**
34+
* returns the Fn Call ID associated with this call
35+
* @returns {string}
36+
*/
37+
get callID(): string;
38+
/**
39+
* Returns the application name associated with this function
40+
* @returns {string}
41+
*/
42+
get appName(): string;
43+
/**
44+
* Returns the application ID associated with this function
45+
* @returns {string}
46+
*/
47+
get appID(): string;
48+
/**
49+
* Returns the function name associated with this function
50+
* @returns {string}
51+
*/
52+
get fnName(): string;
53+
/**
54+
* Returns the function ID associated with this function
55+
* @returns {string}
56+
*/
57+
get fnID(): string;
58+
/**
59+
* Returns the amount of RAM (in MB) allocated to this function
60+
* @returns {int}
61+
*/
62+
get memory(): int;
63+
/**
64+
* Returns the application configuration for this function
65+
* @returns {Object.<string,string>}
66+
*/
67+
get config(): {
68+
[x: string]: string;
69+
};
70+
/**
71+
* Returns the raw body of the input to this function
72+
* @returns {*}
73+
*/
74+
get body(): any;
75+
/**
76+
* Returns the content type of the body (if set)
77+
* @returns {null|string}
78+
*/
79+
get contentType(): null | string;
80+
/**
81+
* returns a map of headers associated with this function this is an object containing key->[string] values
82+
* Header keys are always canonicalized to HTTP first-caps style
83+
84+
* This returns a copy of the underlying headers, changes to the response value will not be reflected in the function response
85+
*
86+
* @returns {Object.<string,Array.<string>>}
87+
*/
88+
get headers(): {
89+
[x: string]: string[];
90+
};
91+
/**
92+
* Create an OCI APM TracingContext for the current invocation.
93+
*/
94+
get tracingContext(): TracingContext;
95+
/**
96+
* returns a object containing the outbound headers associated with this function this is an object containing key->[string] values
97+
*
98+
* Header keys are always canonicalized to HTTP first-caps style
99+
*
100+
* This returns a copy of the underlying headers, changes to the response value will not be reflected in the function response
101+
*
102+
* @returns {Object.<string,Array.<string>>}
103+
*/
104+
get responseHeaders(): {
105+
[x: string]: string[];
106+
};
107+
/**
108+
* returns all header values for a given key
109+
* @param key {string}
110+
* @returns {Array.<string>}
111+
*/
112+
getAllHeaderValues(key: string): Array<string>;
113+
/**
114+
* Returns the first value of a given header or null
115+
* Header keys are compared using case-insensitive matching
116+
* @param key {string}
117+
* @returns {string|null}
118+
*/
119+
getHeader(key: string): string | null;
120+
/**
121+
* Returns a config value for a given key
122+
* @param key {string}
123+
* @returns {string|null}
124+
*/
125+
getConfig(key: string): string | null;
126+
/**
127+
* Returns the first value of a given header or null
128+
* Header keys are compared using case-insensitive matching
129+
* @param key {string}
130+
* @returns {string|null}
131+
*/
132+
getResponseHeader(key: string): string | null;
133+
/**
134+
* Sets a response header to zero or more values
135+
* @param key {string}
136+
* @param values {string}
137+
*/
138+
setResponseHeader(key: string, ...values: string): void;
139+
/**
140+
* Appends a response header to any existing values
141+
* @param key {string}
142+
* @param values {string}
143+
*/
144+
addResponseHeader(key: string, ...values: string): void;
145+
/**
146+
* Sets the response content type
147+
* @param contentType {string}
148+
*/
149+
set responseContentType(contentType: string);
150+
/**
151+
* Gets the response content type
152+
* @returns {string|null}
153+
*/
154+
get responseContentType(): string | null;
155+
/**
156+
* Returns the httpContext associated with this request
157+
* @returns {HTTPGatewayContext}
158+
*/
159+
get httpGateway(): HTTPGatewayContext;
160+
}
161+
/**
162+
* A function result = this causes the handler wrapper to use a specific response writer
163+
*/
164+
declare class FnResult {
165+
writeResult(ctx: any, resp: any): void;
166+
}
167+
/**
168+
* TracingContext defines an OCI APM tracing context for the current invocation.
169+
* Traces are currently defined by the Zipkin standard.
170+
* See: https://zipkin.io/pages/instrumenting
171+
*/
172+
declare class TracingContext {
173+
constructor(ctx: any);
174+
isEnabled: boolean;
175+
traceCollectorUrl: any;
176+
traceId: any;
177+
spanId: any;
178+
parentSpanId: any;
179+
sampled: boolean;
180+
flags: any;
181+
serviceName: string;
182+
}
183+
declare class HTTPGatewayContext {
184+
/**
185+
* Create an HTTP context
186+
* @param ctx {Context}
187+
*/
188+
constructor(ctx: Context);
189+
ctx: Context;
190+
_headers: {};
191+
/**
192+
* returns the HTTP request URL for this event
193+
* @returns {string}
194+
*/
195+
get requestURL(): string;
196+
/**
197+
* Returns the HTTP method for this event
198+
* @returns {string}
199+
*/
200+
get method(): string;
201+
/**
202+
* returns the HTTP headers received by the gateway for this event
203+
* @returns {*}
204+
*/
205+
get headers(): any;
206+
/**
207+
* Retuns a specific header or null if the header is not set - where multiple values are present the first header is returned
208+
* @param key {string} the header key
209+
* @returns {string|null}
210+
*/
211+
getHeader(key: string): string | null;
212+
/**
213+
* returns all header values for a given key
214+
* @param key {string}
215+
* @returns {Array.<string>}
216+
*/
217+
getAllHeaderValues(key: string): Array<string>;
218+
/**
219+
* set the status code of the HTTP response
220+
* @param status {int}
221+
*/
222+
set statusCode(status: int);
223+
/**
224+
* Returns the HTTP status code of the HTTP response
225+
* @returns {string}
226+
*/
227+
get statusCode(): string;
228+
/**
229+
* Sets a response header to zero or more values
230+
* @param key {string}
231+
* @param values {string}
232+
*/
233+
setResponseHeader(key: string, ...values: string): void;
234+
/**
235+
* Appends a response header to any existing values
236+
* @param key {string}
237+
* @param values {string}
238+
*/
239+
addResponseHeader(key: string, ...values: string): void;
240+
}
241+
export {};
242+
}

0 commit comments

Comments
 (0)