├── .deploystack ├── clean ├── deploystack.yaml ├── messages │ ├── description.txt │ └── success.txt ├── test └── test.yaml ├── .github └── dependabot.yaml ├── .gitignore ├── .terraform.lock.hcl ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── architecture.png ├── cloud_shell.png ├── cloudbuild.yaml ├── cloudsql.tf ├── gcp_banner.png ├── main.tf ├── outputs.tf ├── tutorial.md ├── variables.tf └── versions.tf /.deploystack/clean: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # DON'T REMOVE FROM test script. 16 | CYAN='\033[0;36m' 17 | BCYAN='\033[1;36m' 18 | NC='\033[0m' # No Color 19 | DIVIDER=$(printf %"$(tput cols)"s | tr " " "*") 20 | DIVIDER+="\n" 21 | 22 | function get_project_id() { 23 | local __resultvar=$1 24 | VALUE=$(gcloud config get-value project | xargs) 25 | eval $__resultvar="'$VALUE'" 26 | } 27 | 28 | function get_project_number() { 29 | local __resultvar=$1 30 | local PRO=$2 31 | VALUE=$(gcloud projects list --filter="project_id=$PRO" --format="value(PROJECT_NUMBER)" | xargs) 32 | eval $__resultvar="'$VALUE'" 33 | } 34 | 35 | # DISPLAY HELPERS 36 | function section_open() { 37 | section_description=$1 38 | printf "$DIVIDER" 39 | printf "${CYAN}$section_description${NC} \n" 40 | printf "$DIVIDER" 41 | } 42 | 43 | function section_close() { 44 | printf "$DIVIDER" 45 | printf "${CYAN}$section_description ${BCYAN}- done${NC}\n" 46 | printf "\n\n" 47 | } 48 | 49 | function evalTest() { 50 | local command=$1 51 | local expected=$2 52 | 53 | local ERR="" 54 | got=$(eval $command 2>errFile) 55 | ERR=$(errFile) 55 | ERR=$( to see 12 | your current agreements on file or to sign a new one. 13 | 14 | You generally only need to submit a CLA once, so if you've already submitted one 15 | (even if it was for a different project), you probably don't need to do it 16 | again. 17 | 18 | ## Code Reviews 19 | 20 | All submissions, including submissions by project members, require review. We 21 | use GitHub pull requests for this purpose. Consult 22 | [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more 23 | information on using pull requests. 24 | 25 | ## Community Guidelines 26 | 27 | This project follows [Google's Open Source Community 28 | Guidelines](https://opensource.google/conduct/). 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![GC Start](gcp_banner.png)](https://cloud.google.com/?utm_source=github&utm_medium=referral&utm_campaign=GCP&utm_content=packages_repository_banner) 2 | # Host a Serverless Wordpress Site with Cloud Run 3 | 4 | ## Introduction 5 | 6 | _This architecture uses click-to-deploy so you can spin up infrastructure in minutes using terraform!_ 7 | 8 | 43% of the Web is built on Wordpress. Because of its simplicity and versatility, Wordpress can be used for internal websites and customer facing e-commerce platforms in small to large businesses, while still offering security. **Cloud Run automatically scales your WordPress application to handle any amount of traffic**, without requiring manual intervention. This means that your website can handle large traffic spikes, without worrying about capacity. 9 | 10 | Cloud Run allows you to deploy your WordPress application quickly and easily, with a few clicks or commands. You can deploy your application from a container image stored in a container registry or directly from a Git repository. This click-to-deploy architecture leverages the serverless and scalability benefits of using Cloud Run to host a Wordpress Application connected to a PostgreSQL instance. 11 | 12 | This repo is based on the Cloud Foundation Fabric blueprint available [here](https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/tree/master/blueprints/third-party-solutions/wordpress/cloudrun). 13 | 14 | ## Use cases 15 | 16 | These are some of the use cases you can build on top of this infrastructure: 17 | * Business websites 18 | * E-commerce websites 19 | * Portfolios 20 | * Non-profit websites 21 | * Educational Websites 22 | 23 | ## Architecture 24 | 25 | ![Wordpress on Cloud Run](architecture.png "Wordpress on Cloud Run") 26 | 27 | The main components that are deployed in this architecture are the following (you can learn about them by following the hyperlinks): 28 | 29 | * [Cloud Run](https://cloud.google.com/run): serverless PaaS offering to host containers for web-oriented applications, while offering security, scalability and easy versioning 30 | * [Cloud SQL](https://cloud.google.com/sql): Managed solution for SQL databases 31 | * [VPC Serverless Connector](https://cloud.google.com/vpc/docs/serverless-vpc-access): Solution to access the CloudSQL VPC from Cloud Run, using only internal IP addresses 32 | 33 | ## Costs 34 | 35 | Pricing Estimates - We have created a sample estimate based on some usage we see from new startups looking to scale. This estimate would give you an idea of how much this deployment would essentially cost per month at this scale and you extend it to the scale you further prefer. Here's the [link](https://cloud.google.com/products/calculator#id=8a7471c9-98df-4b71-97de-6222d22484c8). 36 | 37 | 38 | ## Setting up the project for the deployment 39 | 40 | This example will deploy all its resources into the project defined by the `project_id` variable. Please note that we assume this project already exists. However, if you provide the appropriate values to the `project_create` variable, the project will be created as part of the deployment. 41 | 42 | If `project_create` is set to null, the identity performing the deployment needs the `owner` role on the project defined by the `project_id` variable. Otherwise, the identity performing the deployment needs `resourcemanager.projectCreator` on the resource hierarchy node specified by `project_create.parent` and `billing.user` on the billing account specified by `project_create.billing_account_id`. 43 | 44 | ## Deploy the architecture 45 | 46 | Before we deploy the architecture, you will need the following information: 47 | 48 | * The __service project ID__. 49 | * A __unique prefix__ that you want all the deployed resources to have (for example: awesomestartup). This must be a string with no spaces or tabs. 50 | * A __Wordpress image__ if you want to use your own, otherwise you can use the provided standard image. 51 | * A __list of Groups or Users__ with Service Account Token creator role on Service Accounts in IAM format, eg 'group:group@domain.com'. 52 | 53 | **Notes**: 54 | 55 | 1. If you want to change your admin password later on, please note that you can only do so via the Wordpress user interface. 56 | 2. If you have the [domain restriction org. policy](https://cloud.google.com/resource-manager/docs/organization-policy/restricting-domains) on your organization, you have to edit the `cloud_run_invoker` variable and give it a value that will be accepted in accordance to your policy. 57 | 58 | Click on the button below, sign in if required and when the prompt appears, click on “confirm”. It will walk you through setting up your architecture. 59 | 60 | [![Open Cloud Shell](https://gstatic.com/cloudssh/images/open-btn.svg)](https://shell.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fdeploystack-wordpress-on-cloudrun&cloudshell_image=gcr.io%2Fds-artifacts-cloudshell%2Fdeploystack_custom_image&cloudshell_git_branch=main&cloudshell_tutorial=tutorial.md) 61 | 62 | 63 | This is the startup screen that appears after clicking the button and confirming: 64 | 65 | ![cloud_shell](cloud_shell.png) 66 | 67 | During the process, you will be asked for some user input. All necessary variables are explained at the bottom of this ReadMe file. In case of failure, you can simply click the button again. 68 | 69 |
70 |

🎉 Congratulations! 🎉
71 | You have successfully deployed the foundation for running Wordpress using CloudRun on Google Cloud.

72 | 73 | 74 | **Note**: 75 | You might get the following error (or a similar one): 76 | 77 | ``` {shell} 78 | │ Error: resource is in failed state "Ready:False", message: Revision '...' is not ready and cannot serve traffic.│ 79 | ``` 80 | 81 | In case this happens, manually run 82 | ``` {shell} 83 | deploystack install 84 | ``` 85 | to run the installation again. 86 | 87 | ## Using the Wordpress Installation 88 | 89 | Upon completion, you will see the output with the values for the Cloud Run service and the user and password to access the `/admin` part of the website. You can also view it later with: 90 | 91 | ``` {shell} 92 | terraform output 93 | # or for the concrete variable: 94 | terraform output cloud_run_service 95 | ``` 96 | 97 | When clicking on the Wordpress link, it will immediately prompt you to register as an administrator. The password will be pre-filled and can be changed after registration. 98 | 99 | ## Cleaning Up Your Environment 100 | 101 | The easiest way to remove all deployed resources is to run the following command in Cloud Shell: 102 | 103 | ``` {shell} 104 | deploystack uninstall 105 | ``` 106 | 107 | The above command will delete the associated resources so there will be no billable charges made afterwards. 108 | 109 | 110 | ## Standalone deployment 111 | 112 | Since the scripts are based on Terraform, it is possible to deploy this solution through `Terraform` directly, without having to go through `Deploystack`. 113 | Just clone this repo, create a `terraform.tfvars` file with your own values and call the following commands: 114 | ``` {shell} 115 | terraform init 116 | terraform plan -out=execplan.out 117 | terraform apply execplan.out 118 | ``` 119 | 120 | ## Special Thanks 121 | A special thank you goes out to Natalia Strelkova (skalolazka) and Grigory Movsesyan, the original developers of this example architecture. 122 | 123 | ## Variables 124 | 125 | | name | description | type | default | 126 | |---|---|:---:|:---:| 127 | | [project_id](variables.tf#L78) | Project id, references existing project if `project_create` is null. | string | | 128 | | [wordpress_image](variables.tf#L89) | Image to run with Cloud Run, starts with \"gcr.io\" | string | mirror.gcr.io/library/wordpress | 129 | | [cloud_run_invoker](variables.tf#L18) | IAM member authorized to access the end-point (for example, 'user:YOUR_IAM_USER' for only you or 'allUsers' for everyone) | string | "allUsers" | 130 | | [cloudsql_password](variables.tf#L24) | CloudSQL password (will be randomly generated by default) | string | null | 131 | | [connector](variables.tf#L30) | Existing VPC serverless connector to use if not creating a new one | string | null | 132 | | [create_connector](variables.tf#L36) | Should a VPC serverless connector be created or not | bool | true | 133 | | [ip_ranges](variables.tf#L43) | CIDR blocks: VPC serverless connector, Private Service Access(PSA) for CloudSQL, CloudSQL VPC | object({…}) | {…} | 134 | | [prefix](variables.tf#L57) | Unique prefix used for resource names. Not used for project if 'project_create' is null. | string | "" | 135 | | [principals](variables.tf#L63) | List of users to give rights to (CloudSQL admin, client and instanceUser, Logging admin, Service Account User and TokenCreator), eg 'user@domain.com'. | list(string) | [] | 136 | | [project_create](variables.tf#L69) | Provide values if project creation is needed, uses existing project if null. Parent is in 'folders/nnn' or 'organizations/nnn' format. | object({…}) | null | 137 | | [region](variables.tf#L83) | Region for the created resources | string | | "europe-west4" | 138 | | [wordpress_password](variables.tf#L94) | Password for the Wordpress user (will be randomly generated by default) | string | null | 139 | | [wordpress_port](variables.tf#L100) | Port for the Wordpress image | number | 8080 | 140 | 141 | ## Outputs 142 | 143 | | name | description | sensitive | 144 | |---|---|:---:| 145 | | [cloud_run_service](outputs.tf#L17) | CloudRun service URL | ✓ | 146 | | [cloudsql_password](outputs.tf#L23) | CloudSQL password | ✓ | 147 | | [wp_password](outputs.tf#L29) | Wordpress user password | ✓ | 148 | | [wp_user](outputs.tf#L35) | Wordpress username | | 149 | 150 | 151 | -------------------------------------------------------------------------------- /architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/deploystack-wordpress-on-cloudrun/1bb9d7e2fd85d24947ba8ea8eb2b2531e46c8762/architecture.png -------------------------------------------------------------------------------- /cloud_shell.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/deploystack-wordpress-on-cloudrun/1bb9d7e2fd85d24947ba8ea8eb2b2531e46c8762/cloud_shell.png -------------------------------------------------------------------------------- /cloudbuild.yaml: -------------------------------------------------------------------------------- 1 | steps: 2 | - name: 'hashicorp/terraform' 3 | args: 4 | - 'init' 5 | - name: 'hashicorp/terraform' 6 | args: ['plan', '-var', 'project_id=$PROJECT_ID', '-var', 'prefix=deploystack'] 7 | - name: 'hashicorp/terraform' 8 | args: ['apply', '-var', 'project_id=$PROJECT_ID', '-var', 'prefix=deploystack', '-auto-approve'] 9 | - name: 'hashicorp/terraform' 10 | args: ['destroy', '-var', 'project_id=$PROJECT_ID', '-var', 'prefix=deploystack', '-auto-approve'] 11 | -------------------------------------------------------------------------------- /cloudsql.tf: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2022 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | resource "random_password" "cloudsql_password" { 18 | length = 8 19 | } 20 | 21 | # create a VPC for CloudSQL 22 | module "vpc" { 23 | source = "github.com/GoogleCloudPlatform/cloud-foundation-fabric//modules/net-vpc?ref=v34.1.0" 24 | project_id = module.project.project_id 25 | name = "${local.prefix}sql-vpc" 26 | subnets = [ 27 | { 28 | ip_cidr_range = var.ip_ranges.sql_vpc 29 | name = "subnet" 30 | region = var.region 31 | } 32 | ] 33 | psa_configs = [{ 34 | ranges = { 35 | cloud-sql = var.ip_ranges.psa 36 | } 37 | }] 38 | } 39 | 40 | # create a VPC connector for the ClouSQL VPC 41 | resource "google_vpc_access_connector" "connector" { 42 | count = var.create_connector ? 1 : 0 43 | project = module.project.project_id 44 | name = "${local.prefix}wp-connector" 45 | region = var.region 46 | ip_cidr_range = var.ip_ranges.connector 47 | network = module.vpc.self_link 48 | } 49 | 50 | # Set up CloudSQL 51 | module "cloudsql" { 52 | source = "github.com/GoogleCloudPlatform/cloud-foundation-fabric//modules/cloudsql-instance?ref=v34.1.0" 53 | project_id = module.project.project_id 54 | network_config = { 55 | connectivity = { 56 | psa_config = { 57 | private_network = module.vpc.self_link 58 | } 59 | } 60 | } 61 | name = "${local.prefix}mysql" 62 | region = var.region 63 | database_version = local.cloudsql_conf.database_version 64 | tier = local.cloudsql_conf.tier 65 | databases = [local.cloudsql_conf.db] 66 | gcp_deletion_protection = false 67 | users = { 68 | "${local.cloudsql_conf.user}" = { 69 | password = var.cloudsql_password 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /gcp_banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/deploystack-wordpress-on-cloudrun/1bb9d7e2fd85d24947ba8ea8eb2b2531e46c8762/gcp_banner.png -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2022 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | locals { 18 | all_principals_iam = [for k in var.principals : "user:${k}"] 19 | cloudsql_conf = { 20 | database_version = "MYSQL_8_4" 21 | tier = "db-g1-small" 22 | db = "wp-mysql" 23 | user = "admin" 24 | } 25 | iam = { 26 | # CloudSQL 27 | "roles/cloudsql.admin" = local.all_principals_iam 28 | "roles/cloudsql.client" = local.all_principals_iam 29 | "roles/cloudsql.instanceUser" = local.all_principals_iam 30 | # common roles 31 | "roles/logging.admin" = local.all_principals_iam 32 | "roles/iam.serviceAccountUser" = local.all_principals_iam 33 | "roles/iam.serviceAccountTokenCreator" = local.all_principals_iam 34 | } 35 | connector = var.connector == null ? google_vpc_access_connector.connector.0.self_link : var.connector 36 | prefix = var.prefix == null ? "" : "${var.prefix}-" 37 | } 38 | 39 | # either create a project or set up the given one 40 | module "project" { 41 | source = "github.com/GoogleCloudPlatform/cloud-foundation-fabric//modules/project?ref=v34.1.0" 42 | name = var.project_id 43 | parent = try(var.project_create.parent, null) 44 | billing_account = try(var.project_create.billing_account_id, null) 45 | project_create = var.project_create != null 46 | prefix = var.project_create == null ? null : var.prefix 47 | iam = var.project_create != null ? local.iam : {} 48 | services = [ 49 | "run.googleapis.com", 50 | "logging.googleapis.com", 51 | "monitoring.googleapis.com", 52 | "sqladmin.googleapis.com", 53 | "sql-component.googleapis.com", 54 | "vpcaccess.googleapis.com", 55 | "servicenetworking.googleapis.com" 56 | ] 57 | } 58 | 59 | resource "random_password" "wp_password" { 60 | length = 8 61 | } 62 | 63 | # create the Cloud Run service 64 | module "cloud_run" { 65 | source = "github.com/GoogleCloudPlatform/cloud-foundation-fabric//modules/cloud-run?ref=v34.1.0" 66 | project_id = module.project.project_id 67 | name = "${local.prefix}cr-wordpress" 68 | region = var.region 69 | 70 | containers = { 71 | wordpress = { 72 | image = var.wordpress_image 73 | ports = { 74 | http = { 75 | container_port = var.wordpress_port 76 | name = "http1" 77 | protocol = null 78 | } 79 | } 80 | # set up the database connection 81 | env = { 82 | "WORDPRESS_DB_HOST" : module.cloudsql.ip 83 | "WORDPRESS_DB_NAME" : local.cloudsql_conf.db 84 | "WORDPRESS_DB_USER" : local.cloudsql_conf.user 85 | "WORDPRESS_DB_PASSWORD" : var.cloudsql_password == null ? module.cloudsql.user_passwords[local.cloudsql_conf.user] : var.cloudsql_password 86 | } 87 | } 88 | } 89 | 90 | iam = { 91 | "roles/run.invoker" : [var.cloud_run_invoker] 92 | } 93 | 94 | revision_annotations = { 95 | autoscaling = { 96 | min_scale = 1 97 | max_scale = 2 98 | } 99 | # connect to CloudSQL 100 | cloudsql_instances = [module.cloudsql.connection_name] 101 | vpcaccess_connector = null 102 | # allow all traffic 103 | vpcaccess_egress = "all-traffic" 104 | vpcaccess_connector = local.connector 105 | } 106 | ingress_settings = "all" 107 | } 108 | -------------------------------------------------------------------------------- /outputs.tf: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2022 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | output "cloud_run_service" { 18 | description = "CloudRun service URL" 19 | value = module.cloud_run.service.status[0].url 20 | } 21 | 22 | output "cloudsql_password" { 23 | description = "CloudSQL password" 24 | value = var.cloudsql_password == null ? module.cloudsql.user_passwords[local.cloudsql_conf.user] : var.cloudsql_password 25 | sensitive = true 26 | } 27 | -------------------------------------------------------------------------------- /tutorial.md: -------------------------------------------------------------------------------- 1 | # Host a Serverless Wordpress Site with Cloud Run 2 | 3 | ## Let's get started! 4 | 5 | This example will deploy all its resources into the project defined by the `project_id` variable. Please note that we assume this project already exists. 6 | 7 | However, if you provide the appropriate values to the `project_create` variable, the project will be created as part of the deployment. 8 | 9 | If `project_create` is left to null, the identity performing the deployment needs the owner role on the project defined by the project_id variable. 10 | 11 | Otherwise, the identity performing the deployment needs `resourcemanager.projectCreator` on the resource hierarchy node specified by `project_create.parent` and `billing.user` on the billing account specified by `project_create.billing_account_id`. 12 | 13 | **Time to complete**: About 10 minutes 14 | 15 | Click the **Start** button to move to the next step. 16 | 17 | ## Information required 18 | 19 | Before we deploy the architecture, you will need the following information: 20 | 21 | * __The service project ID__. 22 | * A __unique prefix__ that you want all the deployed resources to have (for example: awesomestartup). This must be a string with no spaces or tabs. 23 | * A __Wordpress image__ if you want to use your own, otherwise you can use the provided standard image. 24 | * A __list of Groups or Users__ with Service Account Token creator role on Service Accounts in IAM format, eg 'group:group@domain.com'. 25 | 26 | ### Notes: 27 | 28 | 1. If you want to change your admin password later on, please note that you can only do so via the Wordpress user interface. 29 | 2. If you have the domain restriction org. policy on your organization, you have to edit the cloud_run_invoker variable and give it a value that will be accepted in accordance to your policy. 30 | 31 | ## Deploy the architecture 32 | Congratulations! At this point you should have successfully deployed the foundations for running Wordpress using CloudRun on Google Cloud. 33 | 34 | ### Note: 35 | *** You might get the following error (or a similar one): *** 36 | 37 | ``` 38 | Error: resource is in failed state "Ready:False", message: Revision '...' is not ready and cannot serve traffic. 39 | ``` 40 | 41 | In case this happens, manually run the following command to run the installation again 42 | 43 | ```bash 44 | deploystack install 45 | ``` 46 | 47 | ## Using the Wordpress Installation 48 | 49 | Upon completion, you will see the output with the values for the Cloud Run service and the user and password to access the /admin part of the website. You can also view it later with: 50 | 51 | ``` bash 52 | terraform output 53 | ``` 54 | 55 | or for the concrete variable: 56 | 57 | ``` bash 58 | terraform output cloud_run_service 59 | ``` 60 | 61 | When clicking on the Wordpress link, it will immediately prompt you to register as an administrator. 62 | 63 | The password will be pre-filled and can be changed after registration. 64 | 65 | ## Cleaning Up Your Environment 66 | The easiest way to remove all deployed resources is to run the following command in Cloud Shell: 67 | 68 | ```bash 69 | deploystack uninstall 70 | ``` 71 | 72 | The above command will delete the associated resources so there will be no billable charges made afterwards. 73 | 74 | ## Variables & Outputs 75 | 76 | For full information on variables and outputs please refer to the [README](https://github.com/GoogleCloudPlatform/deploystack-wordpress-on-cloudrun/blob/main/README.md#variables) file 77 | 78 | ## Congratulations 79 | 80 | 81 | 82 | You’re all set! 83 | -------------------------------------------------------------------------------- /variables.tf: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2022 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | # Documentation: https://cloud.google.com/run/docs/securing/managing-access#making_a_service_public 18 | variable "cloud_run_invoker" { 19 | type = string 20 | description = "IAM member authorized to access the end-point (for example, 'user:YOUR_IAM_USER' for only you or 'allUsers' for everyone)" 21 | default = "allUsers" 22 | } 23 | 24 | variable "cloudsql_password" { 25 | type = string 26 | description = "CloudSQL password (will be randomly generated by default)" 27 | default = null 28 | } 29 | 30 | variable "connector" { 31 | type = string 32 | description = "Existing VPC serverless connector to use if not creating a new one" 33 | default = null 34 | } 35 | 36 | variable "create_connector" { 37 | type = bool 38 | description = "Should a VPC serverless connector be created or not" 39 | default = true 40 | } 41 | 42 | # PSA: documentation: https://cloud.google.com/vpc/docs/configure-private-services-access#allocating-range 43 | variable "ip_ranges" { 44 | description = "CIDR blocks: VPC serverless connector, Private Service Access(PSA) for CloudSQL, CloudSQL VPC" 45 | type = object({ 46 | connector = string 47 | psa = string 48 | sql_vpc = string 49 | }) 50 | default = { 51 | connector = "10.8.0.0/28" 52 | psa = "10.60.0.0/24" 53 | sql_vpc = "10.0.0.0/20" 54 | } 55 | } 56 | 57 | variable "prefix" { 58 | description = "Unique prefix used for resource names. Not used for project if 'project_create' is null." 59 | type = string 60 | default = "" 61 | } 62 | 63 | variable "principals" { 64 | description = "List of users to give rights to (CloudSQL admin, client and instanceUser, Logging admin, Service Account User and TokenCreator), eg 'user@domain.com'." 65 | type = list(string) 66 | default = [] 67 | } 68 | 69 | variable "project_create" { 70 | description = "Provide values if project creation is needed, uses existing project if null. Parent is in 'folders/nnn' or 'organizations/nnn' format." 71 | type = object({ 72 | billing_account_id = string 73 | parent = string 74 | }) 75 | default = null 76 | } 77 | 78 | variable "project_id" { 79 | description = "Project id, references existing project if `project_create` is null." 80 | type = string 81 | } 82 | 83 | variable "region" { 84 | type = string 85 | description = "The region where resources will be deployed." 86 | default = "europe-west1" 87 | } 88 | 89 | variable "wordpress_image" { 90 | type = string 91 | description = "Image to run with Cloud Run, starts with \"gcr.io\" or \"REGION-docker.pkg.dev\"" 92 | default = "mirror.gcr.io/library/wordpress" 93 | } 94 | 95 | variable "wordpress_port" { 96 | type = number 97 | description = "Port for the Wordpress image" 98 | default = 80 99 | } 100 | -------------------------------------------------------------------------------- /versions.tf: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | terraform { 16 | required_version = ">= 1.4.4" 17 | required_providers { 18 | google = { 19 | source = "hashicorp/google" 20 | version = ">= 4.69.1" # tftest 21 | } 22 | google-beta = { 23 | source = "hashicorp/google-beta" 24 | version = ">= 4.69.1" # tftest 25 | } 26 | } 27 | 28 | provider_meta "google" { 29 | module_name = "cloud-solutions/wordpress-on-cloudrun-v1.0" 30 | } 31 | } 32 | --------------------------------------------------------------------------------