This documentation provides a step-by-step guide to setting up the project on the Google Cloud Console, integrating Cloud SQL into your project, deploying Docker containers to Cloud Run, and integrating SendGrid into the application.
- A Google account
- Billing information
- Navigate to the Google Cloud Console.
- Sign in using your Google account credentials.
- Click the project drop-down menu located next to the Google Cloud Platform logo.
- Click on New Project.
- Configure the Project:
- Project Name: Enter a name for your project (e.g., "Property Management Suite").
- Billing Account: Select a billing account if prompted (required for Cloud SQL and Cloud Run).
- Organization: If applicable, select your organization.
- Location: If applicable, select the folder or organization.
- Click Create.
- In the left-hand navigation pane, click on Billing.
- Link Billing Account:
- If prompted, link your billing account to the new project.
- If no billing account is available, follow the prompts to create and configure a new billing account.
- In the left-hand navigation pane, click on APIs & Services > Dashboard.
- Click on Enable APIs and Services.
- Search for and enable the APIs required for your project. Here are some of the main ones:
- Compute Engine API
- Cloud SQL Admin API
- OAuth 2.0 Client IDs API
- Click on each API and then click Enable.
- In the left-hand navigation pane, click on IAM & Admin > IAM.
- Click on Add.
- Enter the email address of the user you want to add.
- Select a role for the user (e.g., Project Editor, Viewer, etc.).
- Click Save.
- In the left-hand navigation pane, click on Cloud SQL.
- Click Create Instance.
- Select the database engine you want to use (MySQL).
- Configure Instance Settings:
- Instance ID: Enter a unique identifier for your instance.
- Password: Set a password for the root user.
- Region: Select the region where you want your instance to be located.
- Zone Availability: Choose if you want a single-zone or multi-zone instance. Configure any additional settings as needed.
- Click Create Instance.
- Click on the instance name.
- Under the Databases tab, click Create Database and enter the database name.
- Under the Users tab, click Add User Account and configure the user details.
- In the left-hand navigation pane, click on APIs & Services > Credentials.
- Click on Create Credentials.
- Select OAuth 2.0 Client ID.
- Configure Consent Screen:
- If this is your first time creating an OAuth 2.0 Client ID, you will be prompted to configure the consent screen.
- OAuth Consent Screen: Provide application name, support email, and other required information.
- Scopes: Add the necessary scopes required for your application.
- Test Users: Add any test users who will have access to the application during the testing phase.
- Click Save and Continue.
- Set Up OAuth 2.0 Client ID:
- Application Type: Select the application type (e.g., Web application).
- Name: Enter a name for the OAuth 2.0 client ID.
- Authorized JavaScript Origins: Enter the origins that are allowed to use this client ID (usually this will be the link to the application as well as your localhost server).
- Authorized Redirect URIs: Enter the URIs to which the OAuth 2.0 server can send responses (usually this will be the link to the application as well as your localhost server).
- Click Create. After creating the client ID, you will see the Client ID and Client Secret.
- Navigate to the Backend/.env file.
- Paste the Client ID in the GOOGLE_CLIENT_ID field.
- Paste Client Secret into the GOOGLE_CLIENT_SECRET field.
- On the Google Console, click on the navigation pane > Cloud SQL.
- Retrieve Public and Private IP Address
If you are connecting to Cloud SQL in your application, your project can be in either of the two states:
- The application is deployed on a Cloud Run container
- In this case, before you deploy your container to Cloud Run, your DB_HOST field in the Backend/.env file must be set to the Private IP address
- The application is being run on a localhost for testing
- In this case, the DB_HOST field in the Backend/.env file must be set to the Public IP address.
- Also, to connect locally to the Cloud SQL database when testing, your current IP Address must be in the list of authorized networks for the database. Note: If you don't add your IP address to this list, you won't be able to connect to the database.
- Find your current IP address from Google Cloud Console.
- On the Google Console, Click on the Navigation Menu > SQL.
- Select the database for your project.
- Click on the Connections tab.
- Click on the Networking tab.
- Click on Add A Network. A New Network box will appear.
- Enter the network name and IP address with the appropriate CIDR notaion.
- Click Done on the New Network box.
- Click Save.
This project works follows a three-tiered web architecture, meaning there is a Frontend, Backend, and Database layer. You will always have a seperate Cloud Run container for the Frontend and Backend. In the following steps, you will learn how to deploy and connect both containers to Cloud Run.
- A working directory with the appropriate Dockerfile configuration
- Docker
WARNING: Before deploying your Backend container, make sure the DB_HOST field in the Backend/.env file is set your database's Private IP address. Also make sure that the VITE_MIDDLEWARE_URL field in the Frontend/.env file is pointing to your current Cloud Run Backend container.
- Navigate to the directory you want to containerize (Ex: cd Backend).
Note: If you are deploying a Backend and Frontend container, you should deploy the Backend container first. This is because the Frontend/.env file needs to know the URL of the Backend container. - Enter the command: docker build -t [IMAGE] .
Note: Replace [IMAGE] with the name of your desired image. - Enter the command: gcloud builds submit --tag gcr.io/[PROJECT-ID]/[IMAGE]
Note: Replace [PROJECT-ID] with your Google Cloud Project's Project ID. Replace [IMAGE] with the same image you specified in Step 2. - Enter the command: gcloud run deploy [CONTAINER] --image=gcr.io/PROJECT-ID]/[IMAGE]
Note: Replace [CONTAINER] with the desired container name (Ex: Backend). Make sure PROJECT-ID and IMAGE are consistent with the previous steps.
You will be prompted to enter which server you are deploying to. Enter the server associated with your project. Your container should be deployed on Cloud Run.
- Sign Up or Login to your SendGrid account.
- Click on the Settings option in the sidebar.
- Under the Settings menu, click on API Keys.
- Click the Create API Key button.
- Provide a name for your API key.
- Set thje desired permissions
- Click the Create & View button.
- Save the generated API Key. (SendGrid does not store this key, write it down somewhere)
- On the Google Cloud Console, search for "Cloud Functions"
- Select the Cloud Functions page
- On the Cloud Functions page, click the Create Function button
- Enter your entrypoint name (This function name will appear in your Cloud Run view)
- Enter your SendGrid API key in the runtime environment variables configuration
- Enter the following code in the index.js (assuming your sendgrid api key is saved as SENDGRID_API_KEY in the runtime environment variables):
const functions = require('@google-cloud/functions-framework');
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
functions.http('sendEmail', async (req, res) => {
const recipient = req.query.recipient || req.body.recipient;
const subject = req.query.subject || req.body.subject || 'Test Email';
const text = req.query.text || req.body.text || 'This is a test email sent from SendGrid via Cloud Function!';
const msg = {
to: recipient,
from: 'ENTER YOUR EMAIL HERE',
subject,
text
};
try {
await sgMail.send(msg);
res.status(200).send('Email sent successfully!');
} catch (error) {
res.status(500).send('Error sending email.');
}
});
- Enter "@sendgrid/mail": "^8.1.1" in the package.json
- Deploy the function
- Retrieve URL endpoint (You will use this in the sendEmail funciton Backend/utils.js)