Skip to content

Commit 400fa5f

Browse files
authored
chore(tidy): clean up PHP example (#71)
* Clean up PHP S3 example * Pin version of aws-sdk-php * No uppercase letters
1 parent 4680b33 commit 400fa5f

File tree

6 files changed

+39
-48
lines changed

6 files changed

+39
-48
lines changed

functions/php-s3/Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ tf-init:
55

66
.PHONY: tf-plan
77
tf-plan:
8-
cd terraform && terraform plan -var-file=vars/main.tfvars
8+
cd terraform && terraform plan
99

1010
.PHONY: tf-apply
1111
tf-apply:
12-
cd terraform && terraform apply -auto-approve -var-file=vars/main.tfvars
12+
cd terraform && terraform apply -auto-approve
1313

1414
.PHONY: tf-destroy
1515
tf-destroy:
16-
cd terraform && terraform destroy -var-file=vars/main.tfvars
16+
cd terraform && terraform destroy

functions/php-s3/README.md

+8-13
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,10 @@ This example assumes you are familiar with how serverless functions work. If nee
1818

1919
## Setup
2020

21-
To configure Terraform to use your project, edit `terraform/vars/main.tfvars` to set your project ID:
22-
23-
```
24-
# Replace with your project ID
25-
project_id = "12ef4x91-yh12-1234-g22g-83er2q4z51ec"
26-
```
27-
28-
You then need to export your Scaleway access key and secret key:
21+
First you need to set up your Terraform environment with your Scaleway credentials by exporting some environment variables:
2922

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

3731
```
3832
# Initialise Terraform
39-
make tf-init
33+
cd terraform
34+
terraform init
4035
4136
# Check what changes Terraform will make
42-
make tf-plan
37+
terraform plan
4338
4439
# Apply the changes
45-
make tf-apply
40+
terraform apply
4641
```
4742

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

5045
```
51-
./curl.sh
46+
curl https://$(terraform output -raw function_url)
5247
```
5348

5449
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.
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"require": {
3-
"aws/aws-sdk-php": "^3.33"
3+
"aws/aws-sdk-php": "3.33"
44
}
55
}

functions/php-s3/terraform/curl.tftpl

-13
This file was deleted.

functions/php-s3/terraform/main.tf

+27-17
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ variable "secret_key" {
1414
sensitive = true
1515
}
1616

17+
# Randomness for bucket name uniqueness
18+
resource "random_string" "suffix" {
19+
length = 8
20+
special = false
21+
upper = false
22+
}
23+
1724
# Terraform provider
1825
terraform {
1926
required_providers {
@@ -22,7 +29,9 @@ terraform {
2229
version = ">= 2.8.0"
2330
}
2431
}
32+
2533
required_version = ">= 0.13"
34+
2635
backend "local" {
2736
path = "state"
2837
}
@@ -31,15 +40,21 @@ terraform {
3140
provider "scaleway" {
3241
zone = "fr-par-1"
3342
region = "fr-par"
43+
44+
access_key = var.access_key
45+
secret_key = var.secret_key
46+
project_id = var.project_id
3447
}
3548

3649
# S3 bucket
37-
resource "scaleway_object_bucket" "example_bucket" {
38-
name = "php-s3-output"
50+
resource "scaleway_object_bucket" "main" {
51+
name = "php-s3-output-${random_string.suffix.result}"
52+
project_id = var.project_id
53+
region = "fr-par"
3954
}
4055

4156
# Function zip
42-
data "archive_file" "func_archive" {
57+
data "archive_file" "main" {
4358
type = "zip"
4459
source_dir = "${path.module}/../function"
4560
excludes = ["composer.lock", "function.zip", "vendor"]
@@ -48,29 +63,30 @@ data "archive_file" "func_archive" {
4863
}
4964

5065
# Function namespace and function
51-
resource "scaleway_function_namespace" "function_ns" {
66+
resource "scaleway_function_namespace" "main" {
5267
project_id = var.project_id
5368
region = "fr-par"
5469
name = "php-example-namespace"
5570
}
5671

57-
resource "scaleway_function" "php_function" {
72+
resource "scaleway_function" "main" {
5873
name = "php-s3-function"
5974
description = "PHP example working with S3"
60-
namespace_id = scaleway_function_namespace.function_ns.id
75+
namespace_id = scaleway_function_namespace.main.id
6176
runtime = "php82"
6277
handler = "handler.run"
6378
min_scale = 0
6479
max_scale = 2
65-
zip_file = data.archive_file.func_archive.output_path
66-
zip_hash = data.archive_file.func_archive.output_sha
80+
zip_file = data.archive_file.main.output_path
81+
zip_hash = data.archive_file.main.output_sha
6782
privacy = "public"
6883
deploy = true
84+
memory_limit = 512
6985

7086
environment_variables = {
7187
"S3_ENDPOINT" = "https://s3.fr-par.scw.cloud"
7288
"S3_REGION" = "fr-par"
73-
"S3_BUCKET" = scaleway_object_bucket.example_bucket.name
89+
"S3_BUCKET" = scaleway_object_bucket.main.name
7490
}
7591

7692
secret_environment_variables = {
@@ -84,12 +100,6 @@ resource "scaleway_function" "php_function" {
84100
}
85101
}
86102

87-
# Template script to curl
88-
resource local_file curl_script {
89-
filename = "../curl.sh"
90-
content = templatefile(
91-
"curl.tftpl", {
92-
func_url = "${scaleway_function.php_function.domain_name}",
93-
}
94-
)
103+
output function_url {
104+
value = scaleway_function.main.domain_name
95105
}

functions/php-s3/terraform/vars/main.tfvars

-1
This file was deleted.

0 commit comments

Comments
 (0)