Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(tidy): clean up PHP example #71

Merged
merged 3 commits into from
Feb 1, 2024
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
6 changes: 3 additions & 3 deletions functions/php-s3/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ tf-init:

.PHONY: tf-plan
tf-plan:
cd terraform && terraform plan -var-file=vars/main.tfvars
cd terraform && terraform plan

.PHONY: tf-apply
tf-apply:
cd terraform && terraform apply -auto-approve -var-file=vars/main.tfvars
cd terraform && terraform apply -auto-approve

.PHONY: tf-destroy
tf-destroy:
cd terraform && terraform destroy -var-file=vars/main.tfvars
cd terraform && terraform destroy
21 changes: 8 additions & 13 deletions functions/php-s3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,10 @@ This example assumes you are familiar with how serverless functions work. If nee

## Setup

To configure Terraform to use your project, edit `terraform/vars/main.tfvars` to set your project ID:

```
# Replace with your project ID
project_id = "12ef4x91-yh12-1234-g22g-83er2q4z51ec"
```

You then need to export your Scaleway access key and secret key:
First you need to set up your Terraform environment with your Scaleway credentials by exporting some environment variables:

```
export TF_VAR_project_id=<your project id>
export TF_VAR_access_key=<your access key>
export TF_VAR_secret_key=<your secret key>
```
Expand All @@ -36,19 +30,20 @@ You can then set up, plan and apply the Terraform configuration which will creat

```
# Initialise Terraform
make tf-init
cd terraform
terraform init

# Check what changes Terraform will make
make tf-plan
terraform plan

# Apply the changes
make tf-apply
terraform apply
```

This will also output a script, `curl.sh` which you can run to call your function:
Then you can wait for the function to be deployed, and run:

```
./curl.sh
curl https://$(terraform output -raw function_url)
```

You can then check [your `php-s3-example` bucket](https://console.scaleway.com/object-storage/buckets/fr-par/php-s3-example/explorer) to see the key that was written by your function.
Expand Down
2 changes: 1 addition & 1 deletion functions/php-s3/function/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require": {
"aws/aws-sdk-php": "^3.33"
"aws/aws-sdk-php": "3.33"
}
}
13 changes: 0 additions & 13 deletions functions/php-s3/terraform/curl.tftpl

This file was deleted.

44 changes: 27 additions & 17 deletions functions/php-s3/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ variable "secret_key" {
sensitive = true
}

# Randomness for bucket name uniqueness
resource "random_string" "suffix" {
length = 8
special = false
upper = false
}

# Terraform provider
terraform {
required_providers {
Expand All @@ -22,7 +29,9 @@ terraform {
version = ">= 2.8.0"
}
}

required_version = ">= 0.13"

backend "local" {
path = "state"
}
Expand All @@ -31,15 +40,21 @@ terraform {
provider "scaleway" {
zone = "fr-par-1"
region = "fr-par"

access_key = var.access_key
secret_key = var.secret_key
project_id = var.project_id
}

# S3 bucket
resource "scaleway_object_bucket" "example_bucket" {
name = "php-s3-output"
resource "scaleway_object_bucket" "main" {
name = "php-s3-output-${random_string.suffix.result}"
project_id = var.project_id
region = "fr-par"
}

# Function zip
data "archive_file" "func_archive" {
data "archive_file" "main" {
type = "zip"
source_dir = "${path.module}/../function"
excludes = ["composer.lock", "function.zip", "vendor"]
Expand All @@ -48,29 +63,30 @@ data "archive_file" "func_archive" {
}

# Function namespace and function
resource "scaleway_function_namespace" "function_ns" {
resource "scaleway_function_namespace" "main" {
project_id = var.project_id
region = "fr-par"
name = "php-example-namespace"
}

resource "scaleway_function" "php_function" {
resource "scaleway_function" "main" {
name = "php-s3-function"
description = "PHP example working with S3"
namespace_id = scaleway_function_namespace.function_ns.id
namespace_id = scaleway_function_namespace.main.id
runtime = "php82"
handler = "handler.run"
min_scale = 0
max_scale = 2
zip_file = data.archive_file.func_archive.output_path
zip_hash = data.archive_file.func_archive.output_sha
zip_file = data.archive_file.main.output_path
zip_hash = data.archive_file.main.output_sha
privacy = "public"
deploy = true
memory_limit = 512

environment_variables = {
"S3_ENDPOINT" = "https://s3.fr-par.scw.cloud"
"S3_REGION" = "fr-par"
"S3_BUCKET" = scaleway_object_bucket.example_bucket.name
"S3_BUCKET" = scaleway_object_bucket.main.name
}

secret_environment_variables = {
Expand All @@ -84,12 +100,6 @@ resource "scaleway_function" "php_function" {
}
}

# Template script to curl
resource local_file curl_script {
filename = "../curl.sh"
content = templatefile(
"curl.tftpl", {
func_url = "${scaleway_function.php_function.domain_name}",
}
)
output function_url {
value = scaleway_function.main.domain_name
}
1 change: 0 additions & 1 deletion functions/php-s3/terraform/vars/main.tfvars

This file was deleted.

Loading