Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
260 changes: 260 additions & 0 deletions api-gateway-function-nosql-table/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
# API Gateway Function NoSQL Table

This project demonstrates a synchronous CRUD pattern using Oracle Cloud Infrastructure (OCI) services:

- **API Gateway**: Receives HTTP requests and routes them to functions
- **OCI Functions**: Serverless functions for processing requests
- **OCI NoSQL**: Low-latency NoSQL database for storing customer data

## Architecture

The application follows a synchronous request/response pattern:

1. HTTP request arrives at API Gateway → `/customer` endpoint
2. API Gateway routes to **customer-info OCI Function**
3. Function reads or writes customer data to/from **OCI NoSQL Table**
4. Function returns JSON response immediately

```
HTTP Request → API Gateway → customer-info Function → NoSQL Table
↑↓
(Sync Read/Write)
```

## Components

### API Gateway
- Receives HTTP requests at `/customer` endpoint
- Routes GET and POST requests to the `customer-info` function

### Functions
- **customer-info**: OCI Function (Go) that handles customer CRUD operations via API Gateway, reading and writing records to the NoSQL table

### NoSQL Table
- **customer_info**: Stores customer records with fields: `customerId`, `name`, `address`, `email`, `phone`
- Provides low-latency key-value access for serverless workloads

## Directory Structure

```
api-gateway-function-nosql-table/
├── README.md # Project documentation
├── functions/ # Application components
│ └── customer/ # OCI Function for customer CRUD
└── terraform/ # Infrastructure as Code
└── modules/ # Reusable Terraform modules
├── apigateway/ # API Gateway module
├── container_repository/ # OCI Container Registry module
├── functions/ # OCI Functions module
└── nosql/ # OCI NoSQL Table module
```

## Prerequisites

Before you begin, ensure you have the following:

- **OCI Account**: Active Oracle Cloud Infrastructure account with appropriate compartment access
- **Terraform**: Version 1.10.0 or higher installed on your local machine
- **Docker**: Docker Desktop or equivalent for building and pushing container images
- **OCI CLI**: Configured with your OCI credentials for authentication
- **Network**: A public subnet in your VCN for the API Gateway with port 443 opened via security list
- **IAM Permissions**: Your OCI user must have permissions to:
- Create API Gateways
- Create and manage OCI Functions
- Create and manage OCI NoSQL tables
- Create Container Registries
- Manage IAM policies and dynamic groups
- **Authentication**: OCI auth token for Docker Container Registry access

## Getting Started

1. **Configure OCI Variables**:

Update `terraform/terraform.tfvars` with your OCI configuration. Provide your information for `region`, `tenancy_ocid`, `compartment_ocid`, and `subnet_ocid`:
```hcl
region = "us-ashburn-1" # Your OCI region
tenancy_ocid = "ocid1.tenancy.oc1..." # Your tenancy OCID
compartment_ocid = "ocid1.compartment.oc1..." # Your compartment OCID
subnet_ocid = "ocid1.subnet.oc1..." # Your public subnet OCID
...
```

2. **Deploy Infrastructure**:
```bash
cd terraform
terraform init
terraform apply -var-file=terraform.tfvars
```

**Note**: After successful deployment, note the OCIR repository address from the Terraform output:
- `repository_path`: Container registry path for the customer-info function

This will be used in the next step for building and pushing the Docker image.

3. **Build and Deploy Customer-Info Image**:
```bash
cd functions/customer
docker build -t customer-info:1 .
docker tag customer-info:1 <repository_path>:1
docker push <repository_path>:1
```

4. **Update Terraform Variables with Image Tag**:

Update `terraform/terraform.tfvars` to add the function image URI you just pushed:
```hcl
functions = {
"customer_info" = {
source_image = "<repository_path>:1"
path = "/customer"
}
}
```

5. **Re-apply Terraform Configuration**:

Apply the updated configuration with the new image tag:
```bash
cd terraform
terraform apply -var-file=terraform.tfvars
```

This will deploy the OCI Function with the container image you just built and pushed.



## Components

### API Gateway
- Receives HTTP requests at `/customer` endpoint
- Routes GET and POST requests to the `customer-info` function

### Functions
- **customer-info**: OCI Function (Go) that handles customer CRUD operations via API Gateway, reading and writing records to the NoSQL table

### NoSQL Table
- **customer_info**: Stores customer records with fields: `customerId`, `name`, `address`, `email`, `phone`
- Provides low-latency key-value access for serverless workloads

## Directory Structure

```
api-gateway-function-nosql-table/
├── README.md # Project documentation
├── functions/ # Application components
│ └── customer/ # OCI Function for customer CRUD
└── terraform/ # Infrastructure as Code
└── modules/ # Reusable Terraform modules
├── apigateway/ # API Gateway module
├── container_repository/ # OCI Container Registry module
├── functions/ # OCI Functions module
└── nosql/ # OCI NoSQL Table module
```

## Prerequisites

Before you begin, ensure you have the following:

- **OCI Account**: Active Oracle Cloud Infrastructure account with appropriate compartment access
- **Terraform**: Version 1.10.0 or higher installed on your local machine
- **Docker**: Docker Desktop or equivalent for building and pushing container images
- **OCI CLI**: Configured with your OCI credentials for authentication
- **Network**: A public subnet in your VPC for the API Gateway
- **IAM Permissions**: Your OCI user must have permissions to:
- Create API Gateways
- Create and manage OCI Functions
- Create and manage OCI NoSQL tables
- Create Container Registries
- Manage IAM policies and dynamic groups
- **Authentication**: OCI auth token for Docker Container Registry access

## Getting Started

1. **Configure OCI Variables**:

Update `terraform/terraform.tfvars` with your OCI configuration:
```hcl
region = "us-ashburn-1" # Your OCI region
compartment_ocid = "ocid1.compartment.oc1..." # Your compartment OCID
subnet_ocid = "ocid1.subnet.oc1..." # Your public subnet OCID
nosql_table_name = "customer_info"
container_repository_name = "customer_info_repo"
application_display_name = "customer_info_app"
functions = {
"customer_info" = {
source_image = null
path = "/customer"
}
}
```

2. **Deploy Infrastructure**:
```bash
cd terraform
terraform init
terraform apply -var-file=terraform.tfvars
```

**Note**: After successful deployment, note the OCIR repository address from the Terraform output:
- `repository_path`: Container registry path for the customer-info function

This will be used in the next step for building and pushing the Docker image.

3. **Build and Deploy Customer-Info Image**:
```bash
cd functions/customer
docker build -t customer-info:1 .
docker tag customer-info:1 <repository_path>:1
docker push <repository_path>:1
```

4. **Update Terraform Variables with Image Tag**:

Update `terraform/terraform.tfvars` to add the function image URI you just pushed:
```hcl
functions = {
"customer_info" = {
source_image = "<repository_path>:1"
path = "/customer"
}
}
```

5. **Re-apply Terraform Configuration**:

Apply the updated configuration with the new image tag:
```bash
cd terraform
terraform apply -var-file=terraform.tfvars
```

This will deploy the OCI Function with the container image you just built and pushed. The `api_gateway_endpoint` will be printed in the Terraform output — save this URL for the next step.

6. **Test the API**:

Create a customer record:
```bash
curl -X POST <api_gateway_endpoint>/customer \
-H "Content-Type: application/json" \
-d '{
"data": {
"id": "CUST-001",
"name": "Jane Doe",
"address": "123 Main St",
"email": "jane@example.com",
"phone": "555-1234"
}
}'
```

Retrieve a customer record:
```bash
curl -X GET "<api_gateway_endpoint>/customer?id=CUST-001"
```

## Benefits

- **Simplicity**: Synchronous request/response pattern is easy to reason about and debug
- **Low Latency**: Direct NoSQL access returns results in the same request cycle
- **Scalability**: Serverless functions scale automatically with incoming traffic
- **Cost Efficiency**: Pay-per-use pricing for both Functions and NoSQL
11 changes: 11 additions & 0 deletions api-gateway-function-nosql-table/functions/customer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM fnproject/go:1.24-dev as build-stage
WORKDIR /function
WORKDIR /go/src/func/
ENV GO111MODULE=on
COPY . .
RUN go mod tidy
RUN go build -o func -v
FROM fnproject/go:1.24
WORKDIR /function
COPY --from=build-stage /go/src/func/func /function/
ENTRYPOINT ["./func"]
Loading