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

Automation: Tour of Beam infrastructure deployment #25793

Merged
merged 38 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
59ddccf
Added Terraform scripts for TOB infra
ruslan-ikhsan Mar 10, 2023
0ae7250
ToB Frontend related updates
ruslan-ikhsan Mar 10, 2023
f10c10e
Update settings.gradle.kts
ruslan-ikhsan Mar 10, 2023
8d9fe77
Deleted redundant file and minor README change
ruslan-ikhsan Mar 13, 2023
70b984c
Merge remote-tracking branch 'origin/master' into tourofbeam-infra-de…
ruslan-ikhsan Mar 16, 2023
289c07a
Addressing comments in the PR
ruslan-ikhsan Mar 16, 2023
8e3f7a1
Added newline at the end of variables.tf file
ruslan-ikhsan Mar 16, 2023
4f0b6fc
Merge remote-tracking branch 'origin/master' into tourofbeam-infra-de…
ruslan-ikhsan Mar 29, 2023
4f72d86
Update README.md
ruslan-ikhsan Mar 29, 2023
7141c51
Updates related to Tour of Beam infrastructure
ruslan-ikhsan Apr 18, 2023
ef7ea84
Update locals.tf
ruslan-ikhsan Apr 18, 2023
8ccfc44
Output.tf updates
ruslan-ikhsan Apr 18, 2023
3454a21
Update output.tf
ruslan-ikhsan Apr 18, 2023
e92725e
Updates
ruslan-ikhsan Apr 18, 2023
a21f5d7
Update main.tf
ruslan-ikhsan Apr 18, 2023
63b7cd4
Updates to cloudfunctions_bucket variable
ruslan-ikhsan Apr 18, 2023
274d210
service_account_id changes
ruslan-ikhsan Apr 18, 2023
2238064
Update main.tf
ruslan-ikhsan Apr 18, 2023
5248ee1
Update README.md
ruslan-ikhsan Apr 19, 2023
0017def
Update README.md
ruslan-ikhsan Apr 19, 2023
71c7dfb
Update README.md
ruslan-ikhsan Apr 19, 2023
2986675
Update README.md
ruslan-ikhsan Apr 19, 2023
1f18ff8
Update README.md
ruslan-ikhsan Apr 19, 2023
7aef545
Bulk update of terraform scripts
ruslan-ikhsan Apr 20, 2023
73d8825
Update README.md
ruslan-ikhsan Apr 21, 2023
9c2cd04
Update README.md
ruslan-ikhsan Apr 25, 2023
633b4ad
Datastore_namespace updates
ruslan-ikhsan Apr 25, 2023
f1193f2
Update README.md
ruslan-ikhsan Apr 26, 2023
3bdd595
Update README.md
ruslan-ikhsan Apr 26, 2023
8146919
Update README.md
ruslan-ikhsan Apr 26, 2023
f1fe20a
Merge branch 'master' into tourofbeam-infra-deployment
ruslan-ikhsan Apr 26, 2023
1e68053
Update README.md
ruslan-ikhsan Apr 27, 2023
87e02d8
Update main.tf
ruslan-ikhsan Apr 27, 2023
dff9e0b
Update README.md
ruslan-ikhsan Apr 27, 2023
d1b9011
Update README.md
ruslan-ikhsan Apr 27, 2023
6d91188
Update README.md
ruslan-ikhsan Apr 27, 2023
d4c09c2
Some minor TF updates
ruslan-ikhsan Apr 28, 2023
284148a
Update README.md
ruslan-ikhsan Apr 28, 2023
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
16 changes: 16 additions & 0 deletions learning/tour-of-beam/frontend/firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"hosting": {
"public": "build/web",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
286 changes: 286 additions & 0 deletions learning/tour-of-beam/terraform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
# The Tour of Beam deployment on GCP
This guide provides instructions on how to deploy the Tour of Beam environment on Google Cloud Platform (GCP) and Firebase environment.
Before starting the deployment, ensure that you have the following prerequisites in place:

## Prerequisites:

1. [GCP project](https://cloud.google.com/resource-manager/docs/creating-managing-projects)
2. [GCP User account](https://cloud.google.com/appengine/docs/standard/access-control?tab=python) _(Note: You will find the instruction "How to create User account" for your new project)_<br>
Ensure that the account has at least following privileges:
- Cloud Datastore Owner
- Create Service Accounts
- Security Admin
- Service Account User
- Service Usage Admin
- Storage Admin
- Kubernetes Engine Cluster Viewer

3. [Google Cloud Storage bucket](https://cloud.google.com/storage/docs/creating-buckets) for saving deployment state

4. An OS with the following software installed:

* [Flutter (3.7.3 >)](https://docs.flutter.dev/get-started/install)
* [Dart SDK (2.19.2)](https://dart.dev/get-dart)
* [Firebase-tools CLI](https://www.npmjs.com/package/firebase-tools)
* [Terraform](https://www.terraform.io/downloads)
* [gcloud CLI](https://cloud.google.com/sdk/docs/install-sdk)
* [Kubectl authentication plugin](https://cloud.google.com/blog/products/containers-kubernetes/kubectl-auth-changes-in-gke)

5. Existing Beam Playground environment

6. Apache Beam Git repository cloned locally

# Prepare deployment configuration:


1. Navigate to Apache Beam cloned repository's `beam/learning/tour-of-beam/terraform` directory

```
cd beam/learning/tour-of-beam/terraform
```

2. Configure authentication for the Google Cloud Platform (GCP). _(Note: Authentication to the GCP Project required to run gcloud commands)_<br>

```
gcloud init

gcloud auth application-default login
```

3. Configure authentication in the GCP Docker registry:
```
gcloud auth configure-docker `chosen_region`-docker.pkg.dev
```

4. And the authentication in GCP Google Kubernetes Engine: _(Note: Authentication to docker and GKE required to fetch GRPC router ip:port info)_<br>
```
gcloud container clusters get-credentials --region `chosen_gke_zone` `gke_name` --project `project_id`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This step requires an existing cluster but I didn't see this as a pre-requisite.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Existing Playground environment" has been added as pre-requisite
It consists of various services, including GKE

```

5. Create datastore indexes:
```
gcloud datastore indexes create ../backend/internal/storage/index.yaml
```

# Deploy the Tour of Beam Backend Infrastructure:

6. Initialize terraform
```
terraform init -backend-config="bucket=`created_gcs_bucket`"
```

7. Run terraform apply to create Tour-Of-Beam backend infrastructure

```
terraform plan -var "gcloud_init_account=$(gcloud config get-value core/account)" \
-var "environment=prod" \
-var "region=us-west1" \
-var "project_id=$(gcloud config get-value project)" \
-var "datastore_namespace=playground-datastore-namespace" \
-var "pg_router_host=$(kubectl get svc -l app=backend-router-grpc -o jsonpath='{.items[0].status.loadBalancer.ingress[0].ip}:{.items[0].spec.ports[0].port}')"
```

```
terraform apply -var "gcloud_init_account=$(gcloud config get-value core/account)" \
-var "environment=prod" \
-var "region=us-west1" \
-var "project_id=$(gcloud config get-value project)" \
-var "datastore_namespace=playground-datastore-namespace" \
-var "pg_router_host=$(kubectl get svc -l app=backend-router-grpc -o jsonpath='{.items[0].status.loadBalancer.ingress[0].ip}:{.items[0].spec.ports[0].port}')"
```

Where:
- **environment** - Infrastructure environment name
- **region** - GCP region for your infrastructure
- **datastore_namespace** - Beam Playground Datastore's namespace

# Deploy the Tour of Beam Frontend Infrastructure:

8. Update config.dart configuration file under beam/learning/tour-of-beam/frontend/lib:

8.1. Navigate to beam/learning/tour-of-beam/frontend/lib.

8.2. Update config.dart file, replacing values in ${ } with your actual values.

Where:
- **${cloudfunctions_region}** - region where GCP Cloud Functions have been deployed
- **${project_id}** - GCP project where infrastructure being deployed
- **${environment}** - Infrastructure environment name
- **${dns_name}** - DNS record name reserved for Beam Playground environment

```
const _cloudFunctionsProjectRegion = '${cloudfunctions_region}';
const _cloudFunctionsProjectId = '${project_id}';
const cloudFunctionsBaseUrl = 'https://'
'$_cloudFunctionsProjectRegion-$_cloudFunctionsProjectId'
'.cloudfunctions.net/${environment}_';


const String kAnalyticsUA = 'UA-73650088-2';
const String kApiClientURL =
'https://router.${dns_name}';
const String kApiJavaClientURL =
'https://java.${dns_name}';
const String kApiGoClientURL =
'https://go.${dns_name}';
const String kApiPythonClientURL =
'https://python.${dns_name}';
const String kApiScioClientURL =
'https://scio.${dns_name}';
```

9. Create file .firebaserc under beam/learning/tour-of-beam/frontend

9.1. Navigate to beam/learning/tour-of-beam/frontend.

9.2. Create .firebaserc file with the following content.

Where:
- **${project_id}** - GCP project where infrastructure being deployed

```
{
"projects": {
"default": "${project_id}"
}
}
```

10. Login into the Firebase CLI

```
# To use an interactive mode (forwards to a browser webpage)
firebase login
```

```
# To use non-interactive mode (generates link)
firebase login --no-localhost
```


11. Create Firebase Project

```
firebase projects:addfirebase
```

12. Create Firebase Web App and prepare Firebase configuration file

```
firebase apps:create WEB ${webapp_name} --project=$(gcloud config get-value project)
```

Once Firebase Web App has been created, there will be following output example:

```
Create your WEB app in project cloudbuild-383310:
✔ Creating your Web app

🎉🎉🎉 Your Firebase WEB App is ready! 🎉🎉🎉

App information:
- App ID: WEBAPP_ID
- Display name: WEBAPP_NAME

You can run this command to print out your new app's Google Services config:
firebase apps:sdkconfig WEB WEBAPP_ID
```

Copy and paste into the terminal last line to get Web App configuration.

Output example:

```
✔ Downloading configuration data of your Firebase WEB app
// Copy and paste this into your JavaScript code to initialize the Firebase SDK.
// You will also need to load the Firebase SDK.
// See https://firebase.google.com/docs/web/setup for more details.

firebase.initializeApp({
"projectId": "cloudbuild-384304",
"appId": "1:1111111111:web:111111111111",
"storageBucket": "cloudbuild-384304.appspot.com",
"locationId": "us-west1",
"apiKey": "someApiKey",
"authDomain": "cloudbuild-384304.firebaseapp.com",
"messagingSenderId": "111111111111"
});
```

Copy the lines inside the curly braces and redact them.

You will need to:

1) Remove "locationId" line.
2) Remove quotes (") from key of "key": "value" pair.
3) E.g. `projectId: "cloudbuild-384304"`
4) In overall, redacted and ready to be inserted data should be as follows:

```
projectId: "cloudbuild-384304",
appId: "1:1111111111:web:111111111111",
storageBucket: "cloudbuild-384304.appspot.com",
apiKey: "someApiKey",
authDomain: "cloudbuild-384304.firebaseapp.com",
messagingSenderId: "111111111111"
```

Paste (replace) the redacted data inside the parentheses in beam/learning/tour-of-beam/frontend/lib/firebase_options.dart file.

```
static const FirebaseOptions web = FirebaseOptions(


);
```

13. Run flutter and firebase commands to deploy Tour of Beam frontend

Navigate to beam/playground/frontend/playground_components and run flutter commands

```
# Go to beam/playground/frontend/playground_components first
flutter pub get
flutter pub run build_runner build --delete-conflicting-outputs
```

Navigate to beam/learning/tour-of-beam/frontend and run flutter commands

```
# Go to beam/learning/tour-of-beam/frontend first
flutter pub get
flutter pub run build_runner build --delete-conflicting-outputs
flutter build web --profile --dart-define=Dart2jsOptimization=O0
firebase deploy --project=$(gcloud config get-value project)
```

# Validate the deployment of the Tour of Beam:

14. Open the Tour of Beam webpage in a web browser (Hosting URL will be provided in terminal output) to ensure that deployment has been successfully completed.

Example:
```
✔ Deploy complete!

Project Console: https://console.firebase.google.com/project/some-gcp-project-id/overview
Hosting URL: https://some-gcp-project-id.web.app
```
29 changes: 29 additions & 0 deletions learning/tour-of-beam/terraform/api_enable/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

# GCP API Services to be enabled
resource "google_project_service" "required_services" {
for_each = toset([
"cloudresourcemanager",
"iam",
"cloudbuild",
"cloudfunctions",
"firebase"
])
service = "${each.key}.googleapis.com"
disable_on_destroy = false
}
Loading