|
| 1 | +# Connect to Cloud Memorystore from Google App Engine |
| 2 | + |
| 3 | +This sample application demonstrates how to use |
| 4 | +[Cloud Memorystore for Redis](https://cloud.google.com/memorystore/docs/) |
| 5 | +with Google App Engine for PHP 7.2. |
| 6 | + |
| 7 | +**Prerequisites** |
| 8 | + |
| 9 | +- Install the [Google Cloud SDK](https://developers.google.com/cloud/sdk/). |
| 10 | + |
| 11 | +## Setup |
| 12 | + |
| 13 | +Before you can run or deploy the sample, you will need to do the following: |
| 14 | + |
| 15 | +1. Create a [Memorystore instance][memorystore_create]. You can do this from the |
| 16 | + [Cloud Console](https://console.developers.google.com) or via the |
| 17 | + [Cloud SDK](https://cloud.google.com/sdk). To create it via the SDK use the |
| 18 | + following command: |
| 19 | + |
| 20 | + $ gcloud beta redis instances create YOUR_INSTANCE_NAME --region=REGION_ID |
| 21 | + |
| 22 | +1. Update the environment variables `REDIS_HOST` and `REDIS_PORT` in `app.yaml` |
| 23 | + with your configuration values. These values are used when the application is |
| 24 | + deployed. Run the following command to get the values for your isntance: |
| 25 | + |
| 26 | + $ gcloud beta redis instances describe YOUR_INSTANCE_NAME --region=REGION_ID |
| 27 | + |
| 28 | +[memorystore_create]: https://cloud.google.com/memorystore/docs/redis/creating-managing-instances |
| 29 | + |
| 30 | +## Run locally |
| 31 | + |
| 32 | +You can connect to a local database instance by setting the `REDIS_` environment |
| 33 | +variables to your local instance. Alternatively, you can set them to your Cloud |
| 34 | +Memorystore instance, but you will need to create a firewall rule for this, |
| 35 | +which may be a safety concern. |
| 36 | + |
| 37 | +```sh |
| 38 | +cd php-docs-samples/appengine/php72/memorystore |
| 39 | + |
| 40 | +# set local connection parameters |
| 41 | +export REDIS_HOST=127.0.0.1 |
| 42 | +export REDIS_PORT=6379 |
| 43 | + |
| 44 | +php -S localhost:8080 |
| 45 | +``` |
| 46 | + |
| 47 | +> be sure the `REDIS_` environment variables are appropriate for your Redis |
| 48 | + instance. |
| 49 | + |
| 50 | +Now you can view the app running at [http://localhost:8080](http://localhost:8080) |
| 51 | +in your browser. |
| 52 | + |
| 53 | +## Set up Serverless VPC Access |
| 54 | + |
| 55 | +**IMPORTANT** App Engine requires a [Serverless VPC Access][vpc-access] |
| 56 | +connector to connect to Memorystore. |
| 57 | + |
| 58 | +In order for App Engine to connect to Memorystore, you must first |
| 59 | +[configure a Serverless VPC Access connector][configure-vpc]. For example: |
| 60 | + |
| 61 | +``` |
| 62 | +# Example command to create a VPC connector. See the docs for more details. |
| 63 | +$ gcloud beta compute networks vpc-access connectors create CONNECTOR_NAME \ |
| 64 | + --region=REGION_ID \ |
| 65 | + --range="10.8.0.0/28" |
| 66 | + --project=PROJECT_ID |
| 67 | +``` |
| 68 | + |
| 69 | +Next, you neded to [configure App Engine to connect to your VPC network][connecting-appengine]. |
| 70 | +This is done by modifying [`app.yaml`](app.yaml) and setting the full name of |
| 71 | +the connector you just created under `vpc_access_connector`. |
| 72 | + |
| 73 | +``` |
| 74 | +vpc_access_connector: |
| 75 | + name: "projects/PROJECT_ID/locations/REGION_ID/connectors/CONNECTOR_NAME" |
| 76 | +``` |
| 77 | + |
| 78 | +**Note**: Serverless VPC Access incurs additional billing. See |
| 79 | +[pricing][vpc-pricing] for details. |
| 80 | + |
| 81 | +[vpc-access]: https://cloud.google.com/vpc |
| 82 | +[configure-vpc]: https://cloud.google.com/vpc/docs/configure-serverless-vpc-access |
| 83 | +[connecting-appengine]: https://cloud.google.com/appengine/docs/standard/python/connecting-vpc#configuring |
| 84 | +[vpc-pricing]: https://cloud.google.com/compute/pricing#network |
| 85 | + |
| 86 | +## Deploy to App Engine |
| 87 | + |
| 88 | +**IMPORTANT** Because Serverless VPC Connector is in *beta*, you must deploy to App Engine |
| 89 | +using the `gcloud beta app deploy` command. Without this, the connection to |
| 90 | +Memorystore *will not work*. |
| 91 | + |
| 92 | +``` |
| 93 | +$ gcloud beta app deploy --project PROJECT_ID |
| 94 | +``` |
| 95 | + |
| 96 | +Now open `https://{YOUR_PROJECT_ID}.appspot.com/` in your browser to see the running |
| 97 | +app. |
| 98 | + |
| 99 | +**Note**: This example requires the `redis.so` extension to be enabled on your App Engine |
| 100 | +instance. This is done by including [`php.ini`](php.ini) in your project root. |
| 101 | + |
| 102 | +## Troubleshooting |
| 103 | + |
| 104 | +### Connection timed out |
| 105 | + |
| 106 | +If you receive the error "Error: Connection timed out", it's possible your VPC Connector |
| 107 | +is not fully set up. Run the following and check the property `state` is set to READY: |
| 108 | + |
| 109 | +``` |
| 110 | +$ gcloud beta compute networks vpc-access connectors describe CONNECTOR_NAME --region=REGION_ID |
| 111 | +``` |
| 112 | + |
| 113 | +If you continue to see the timeout error, try creating a new VPC connector with a different |
| 114 | +CIDR `range`. |
| 115 | + |
| 116 | +### Name or service not known |
| 117 | + |
| 118 | +If you receive the following error, make sure you set the `REDIS_HOST` environment variable in `app.yaml` to be the |
| 119 | +host of your Memorystore for Redis instance. |
| 120 | + |
| 121 | +``` |
| 122 | +PHP message: PHP Warning: Redis::connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /srv/index.php |
| 123 | +``` |
| 124 | + |
| 125 | +### Request contains an invalid argument |
| 126 | + |
| 127 | +If you receive this error, it is because either the `gcloud` command to create the VPC |
| 128 | +Access connector was missing the `--range` option, or the value supplied to the |
| 129 | +`--range` option did not use the `/28` CIDR mask as required. Be sure to supply a valid |
| 130 | +CIDR range ending in `/28`. |
0 commit comments