# Deploy feature flags with the Google Cloud console

This quickstart is for application developers and platform engineers who want to manage feature flags in Google Cloud console without provisioning cloud infrastructure through App Lifecycle Manager. You model a logical service in Application Design Center, deliver a boolean feature flag to a unit in App Lifecycle Manager using a rollout, and evaluate the flag locally in Python with the OpenFeature SDK.

## Before you begin

1. Ensure that your user account has the following Identity and Access Management roles on your Google Cloud project:
   - App Lifecycle Manager Admin (`roles/saasservicemgmt.admin`)
   - App Design Center Admin (`roles/designcenter.admin`)
   - SaaS Config Viewer (`roles/saasconfig.viewer`)
   - Service Account Admin (`roles/iam.serviceAccountAdmin`) and Project IAM Admin (`roles/resourcemanager.projectIamAdmin`)
2. Ensure that an App Design Center space and the [App Lifecycle Manager service accounts](https://docs.cloud.google.com/app-lifecycle-manager/service-accounts.md.txt) are configured, including a user-managed [actuation service account](https://docs.cloud.google.com/app-lifecycle-manager/service-accounts.md.txt#actuation-sa) granted the Service Account Token Creator (`roles/iam.serviceAccountTokenCreator`), Infra Manager Admin (`roles/config.admin`), and Storage Admin (`roles/storage.admin`) roles on your project. For more information about App Lifecycle Manager service accounts, see [Service accounts](https://docs.cloud.google.com/app-lifecycle-manager/service-accounts.md.txt).
3. Install Python 3.10 or later and `pip` on your local machine, and then authenticate Google Cloud CLI for Application Default Credentials (ADC):

   ```
   gcloud auth application-default login
   ```

## Create a SaaS offering in the console

Create a SaaS offering resource as the top-level container for your service
model and feature flags:

1. In Google Cloud console, go to **App Lifecycle Manager** \>
   **SaaS offerings**.

   [Go to SaaS offerings](https://console.cloud.google.com/saas/saasOfferings)
2. Click **Create**.

3. In the **SaaS offering name** field, enter `flags-quickstart-offering`.

4. In the **Region** list, select `us-central1`, and then click **OK**.

5. Click **Create** . When creation completes, the **SaaS offering details**
   page opens.

## Model your service in App Design Center

In the App-Centric workflow, you define unit kinds by authoring an application
template in App Design Center. When you want to use feature flags
without provisioning cloud infrastructure through App Lifecycle Manager, you
create a composite template containing an empty standard application template
for each kind of service in your architecture (for example, a backend app,
frontend app, or gateway service). An empty standard template declares a service
type without deploying cloud resources. When you're ready to manage
infrastructure deployments through App Lifecycle Manager, you can add Terraform
components to the standard template.

To create an empty standard template inside a composite template and generate a
unit kind, complete these steps:

1. On the **SaaS offering details** page for `flags-quickstart-offering`, click **Create new template**.
2. In the **Template ID** field, enter `flags-composite-template`, and then click **Create template**.
3. In the **Connected application template** section, click the **Manage
   template** menu, and then click **Configure in canvas** to open App Design Center.
4. In the **Components** area on the design canvas, click **Embed template** , and then click **Create a new template**.
5. In the **Template ID** field, enter `backend-service-template`, and then click **Create template** to open the standard template canvas.
6. Leave the `backend-service-template` canvas empty without adding components, and then click **Add to Catalog** to publish the standard template to your space's catalog.
7. Return to the `flags-composite-template` design canvas, click **Embed
   template** , select `backend-service-template` from **Templates** , and then click **Select**.
8. Click **Add to Catalog** to publish the composite template revision, and then click **Continue** \> **Deploy at scale with App Lifecycle Manager**.
9. In the **Connected application template** section of the **SaaS offering
   details** page, click **Update template revision** , select the latest revision of `flags-composite-template`, and then click **Update** (or click **Connect existing template** if connecting a template created directly in App Design Center).
10. Open the **Unit kinds** tab on the **SaaS offering details** page and verify that App Lifecycle Manager created the unit kind for `backend-service-template`.

## Create and provision a unit

Create and provision a unit to represent a regional instance of your service
that receives feature flag configurations. Because your standard template in
App Design Center is empty, provisioning runs a no-operation
infrastructure actuation without deploying cloud resources and transitions the
unit to the **Ready** state so `saasconfig.googleapis.com` can serve its
feature flag configuration:

1. In Google Cloud console, go to **App Lifecycle Manager** \> **Units**.

   [Go to Units](https://console.cloud.google.com/saas/units)
2. Click **Create**.

3. In the **Unit name** field, enter `backend-service-unit`.

4. In the **SaaS offering** list, select `flags-quickstart-offering`.

5. In the **Region** list, select `us-central1`.

6. In the **Unit kind** list, select your `backend-service-template-` unit kind
   generated in the preceding section.

7. Click **Create** . When creation completes, the **Unit details** page opens.

8. On the **Unit details** page, click **Provision** to open the **Provision
   unit** panel.

9. In the **Release** list, select the generated release for your
   `backend-service-template-` unit kind.

10. In the **Service account** list, select your actuation service account, and
    then select **Grant App Lifecycle Manager permission to act on behalf of
    the actuation service account**.

11. Click **Add tenant project variables** , and then select your project to
    populate `tenant_project_id` and `tenant_project_number`.

12. Click **Provision** , and then wait for the unit **State** on the **Unit
    details** page to reach **Ready**.

## Create and deploy a feature flag in the console

Define a boolean feature flag and run a rollout to deliver the flag release to
your unit:

1. In Google Cloud console, open **App Lifecycle Manager** \> **Feature
   flags** , and then click **Create feature flag**.
2. In the **Unit kind** field, select your `backend-service-template-` unit kind.
3. In the **Flag key** field, enter `enable-new-feature` (**Flag id** autofills automatically after you enter the flag key).
4. Set **Flag value** to `enabled`, and then click **Create**.
5. Go to **App Lifecycle Manager** \> **Flag releases** , and then click **Create**.
6. Enter `enable-new-feature-rel-1` in the **Flag release name** field, select your `backend-service-template-` unit kind in the **Unit kind** list, select **Feature flag** in the **Release type** list, select your `enable-new-feature` flag in the **Select feature flags** list, and then click **Create**. App Lifecycle Manager creates the underlying flag revision automatically.
7. Go to **App Lifecycle Manager** \> **Rollout kinds** , and then click **Create**.
8. In the **Rollout kind name** field, enter `flags-quickstart-rollout-kind`.
9. In the **Unit Kind** list, select your `backend-service-template-` unit kind.
10. In the **Rollout Strategy** list, select **All locations at once**.
11. Click **Create**.
12. Go to **App Lifecycle Manager** \> **Rollouts** , and then click **Create**.
13. Select **Feature flags rollout**.
14. In the **Rollout name** field, enter `flags-quickstart-rollout`.
15. In the **Unit Kind** list, select your `backend-service-template-` unit kind.
16. In the **Feature flags release** list, select `enable-new-feature-rel-1`, and then click **Next**.
17. Select **Select existing rollout kind** , and then select `flags-quickstart-rollout-kind` in the **Rollout Kind** list.
18. Click **Create** to start the rollout and deliver the configuration to `backend-service-unit`.
19. Wait for the rollout status to reach **Succeeded**.

## Evaluate the feature flag

Evaluate the `enable-new-feature` flag locally using the OpenFeature SDK and the
`flagd` provider connected to `saasconfig.googleapis.com:443`:

1. Install the required Python packages:

   ```bash
   pip install --upgrade google-auth grpcio requests "openfeature-sdk>=0.8.2" "openfeature-provider-flagd>=0.6.1"
   ```
2. Create a file named `evaluate_flag.py` with the following code:

   ```python
   import os
   import time
   import google.auth
   import google.auth.transport.grpc
   import google.auth.transport.requests
   import grpc
   from openfeature import api
   from openfeature.contrib.provider.flagd import FlagdProvider
   from openfeature.contrib.provider.flagd.config import ResolverType

   provider_id = os.environ["FLAGD_SOURCE_PROVIDER_ID"]
   credentials, _ = google.auth.default(
       scopes=["https://www.googleapis.com/auth/cloud-platform"]
   )
   channel_creds = grpc.composite_channel_credentials(
       grpc.ssl_channel_credentials(),
       grpc.metadata_call_credentials(
           google.auth.transport.grpc.AuthMetadataPlugin(
               credentials, google.auth.transport.requests.Request()
           )
       ),
       grpc.metadata_call_credentials(
           lambda ctx, cb: cb(
               [("x-goog-request-params", f"name={provider_id}")], None
           )
       ),
   )
   api.set_provider(
       FlagdProvider(
           resolver_type=ResolverType.IN_PROCESS,
           host="saasconfig.googleapis.com",
           port=443,
           sync_metadata_disabled=True,
           provider_id=provider_id,
           channel_credentials=channel_creds,
       )
   )
   time.sleep(5)
   value = api.get_client().get_boolean_value("enable-new-feature", False)
   print(f"Flag 'enable-new-feature' evaluated to: {value}")
   ```
3. Set the `FLAGD_SOURCE_PROVIDER_ID` environment variable to your unit's
   feature flag configuration resource path and run the script:

   ```bash
   export FLAGD_SOURCE_PROVIDER_ID="projects/PROJECT_ID/locations/us-central1/featureFlagsConfigs/backend-service-unit"
   python evaluate_flag.py
   ```

   Replace `PROJECT_ID` with your Google Cloud
   project ID.

   The script outputs `Flag 'enable-new-feature' evaluated to: True`.

## Clean up


To avoid incurring charges to your Google Cloud account for
the resources used on this page, follow these steps.

To avoid ongoing charges, either delete your test project in
[Manage resources](https://console.cloud.google.com/iam-admin/projects),
or remove the resources that you created in reverse dependency order. Marking a
feature flag for cleanup (`CLEANUP` state) to unlink it from a unit kind
requires the gcloud CLI or the App Lifecycle Manager API:

1. Set the flag state to `cleanup`, create a cleanup flag release, and run a
   cleanup rollout to unlink the flag from your unit kind:

   ```bash
   gcloud beta app-lifecycle-manager flags update FLAG_ID \
       --project=PROJECT_ID \
       --location=global \
       --state=cleanup

   gcloud beta app-lifecycle-manager flags releases create enable-new-feature-cleanup-rel \
       --project=PROJECT_ID \
       --unit-kind=UNIT_KIND_ID \
       --flags=FLAG_ID \
       --location=global

   gcloud beta app-lifecycle-manager rollouts create enable-new-feature-cleanup-rollout \
       --project=PROJECT_ID \
       --flag-release=enable-new-feature-cleanup-rel \
       --rollout-kind=flags-quickstart-rollout-kind \
       --location=global
   ```

   Replace the following:
   - `FLAG_ID`: the ID of your feature flag (autofilled as `UNIT_KIND_ID-enable-new-feature`).
   - `PROJECT_ID`: your Google Cloud project ID.
   - `UNIT_KIND_ID`: the ID of your `backend-service-template-` unit kind.
2. After the cleanup rollout reaches **Succeeded** , delete the rollouts, rollout
   kind (`flags-quickstart-rollout-kind`), and flag releases
   (`enable-new-feature-rel-1` and `enable-new-feature-cleanup-rel`) in
   Google Cloud console.

3. Look up and delete the auto-generated flag revisions, and then delete your
   feature flag:

   ```bash
   gcloud beta app-lifecycle-manager flags revisions list \
       --project=PROJECT_ID \
       --location=global \
       --filter="flag:FLAG_ID"

   gcloud beta app-lifecycle-manager flags revisions delete REVISION_ID \
       --project=PROJECT_ID \
       --location=global
   ```

   Replace `REVISION_ID` with each revision ID returned by
   the `list` command, and then delete the feature flag in
   Google Cloud console.
4. Deprovision `backend-service-unit` on its **Unit details** page (click
   **Deprovision** , confirm in the dialog, and wait for the state to reach
   **Not provisioned** ), and then delete the remaining resources in reverse
   dependency order: unit (`backend-service-unit`), the unit kind's default
   release association and generated blueprint release (clear the default
   release on the unit kind before deleting the blueprint release), unit kind,
   and SaaS offering (`flags-quickstart-offering`). For App Lifecycle Manager
   deletion rules, see [Delete resources](https://docs.cloud.google.com/app-lifecycle-manager/delete-resources.md.txt).

5. Delete `flags-composite-template` and `backend-service-template` in
   App Design Center.

## What's next

- Read the [App Lifecycle Manager feature flags overview](https://docs.cloud.google.com/app-lifecycle-manager/flags/flags-overview.md.txt).
- Try the [Deploy feature flags quickstart](https://docs.cloud.google.com/app-lifecycle-manager/flags/flags-quickstart.md.txt) for infrastructure-managed deployments.
- Configure percentage-based traffic splits in [Use gradual feature rollouts](https://docs.cloud.google.com/app-lifecycle-manager/flags/flags-gradual-rollout.md.txt).
- Diagnose flag evaluation or rollout issues in [Troubleshoot feature flags](https://docs.cloud.google.com/app-lifecycle-manager/flags/flags-troubleshooting.md.txt).