Importing custom Terraform modules into Application Design Center lets you add
your own reusable Google Cloud configurations as components in
your application templates. When you import a Terraform module,
App Design Center creates a component template in your space's
catalog so developers can reuse your established design decisions and policies.

> [!WARNING]
>
> **Preview**
>
>
> This product or feature is
>
> subject to the "Pre-GA Offerings Terms" in the General Service Terms section
> of the [Service Specific
> Terms](https://docs.cloud.google.com/terms/service-terms#1).
>
> Pre-GA products and features are available "as is" and might have limited support.
>
> For more information, see the
> [launch stage descriptions](https://cloud.google.com/products/#product-launch-stages).

For background on authoring Terraform modules for Google Cloud, see the
following:

- [Terraform overview](https://docs.cloud.google.com/docs/terraform/terraform-overview.md.txt)

- [Best practices for general style and structure](https://docs.cloud.google.com/docs/terraform/best-practices/general-style-structure.md.txt)

- [Best practices for reusable modules](https://docs.cloud.google.com/docs/terraform/best-practices/reusable-modules.md.txt)

## Before you begin

To perform the tasks in this document, ask your administrator to grant you the
necessary roles on the [app-enabled folder](https://docs.cloud.google.com/application-design-center/docs/key-concepts.md.txt#app-enabled-folder) or [management project](https://docs.cloud.google.com/application-design-center/docs/key-concepts.md.txt#mgmt-project), as listed in the following table:

| Task | Required roles |
|---|---|
| Import Terraform modules as custom components | Application Design Center Admin (`roles/designcenter.admin`), or Application Design Center User (`roles/designcenter.user`) |

For more information about roles, see [Access control with IAM](https://docs.cloud.google.com/application-design-center/docs/access-control.md.txt).

## Configure and import your modules

Complete the following steps to connect to GitHub, create a metadata file, and
import a Terraform module.

### Connect to your repository using Developer Connect

Create a Developer Connect connection to the GitHub repository
where you store your Google Cloud Terraform modules. You'll use this
connection when you import.

1. Create a Developer Connect connection.

   For steps, see [Connect to GitHub](https://docs.cloud.google.com/developer-connect/docs/connect-github-repo.md.txt).
2. Create a Developer Connect repository link.

   For steps, see [Add repository links to existing connections](https://docs.cloud.google.com/developer-connect/docs/connect-github-repo.md.txt#link-repos).

### Optional: Prepare your Terraform module metadata

When you import your Terraform module, you can supply your own metadata, or let
App Design Center create a metadata file for you.

For example, the following is a `metadata.yaml` file that you might create for a
service account:

```yaml
  spec:
    info:
      actuationTool:
        flavor: Terraform
        version: ">= 1.3"
    interfaces: # Optional
      variables:
        - name: service_account
          connections:
            - source:
                source: github.com/terraform-google/terraform-google-service-accounts
                version: ">= 4.4"
              spec:
                outputExpr: email
    requirements:
      roles: # Optional after IAM integrations
        - level: Project
          roles:
            - roles/iam.serviceAccountUser
            - roles/iap.admin
            - roles/run.admin
            - roles/iam.serviceAccountAdmin
      providerVersions:
        - source: hashicorp/google
          version: ">= 6, < 7"
        - source: hashicorp/google-beta
          version: ">= 6, < 7"
    ui: # Optional
      input:
        variables:
          ca_root_module:
            name: ca_root_module
            title: CA Root Module
          service_account:
            name: service_account
            title: Service Account
```

### Import from your repository

You can create an App Design Center component by importing a
Terraform module that is based on a single Google Cloud product. If you
make changes to your Terraform module in your repository, repeat these steps to
import your changes.

To import your Terraform module, do the following:

1. Identify the catalog where you want to create your component.

   ```bash
     gcloud design-center spaces catalogs list \
     --project=PROJECT \
     --location=LOCATION \
     --space=SPACE
   ```

   Replace the following:
   - `PROJECT`: Your [management project](https://docs.cloud.google.com/application-design-center/docs/key-concepts.md.txt#mgmt-project) ID.
   - `LOCATION`: Your location ID.
   - `SPACE`: Your space ID.

   For more information, see [gcloud design-center spaces catalogs list](https://docs.cloud.google.com/sdk/gcloud/reference/design-center/spaces/catalogs/list.md.txt).
2. Create a catalog template in your space's private catalog.

   ```bash
     gcloud design-center spaces catalogs templates create COMPONENT_TEMPLATE \
     --project=PROJECT \
     --catalog=CATALOG \
     --location=LOCATION \
     --space=SPACE \
     --template-category=component-template
   ```

   Replace the following:
   - `COMPONENT_TEMPLATE`: The template ID for the component template that you want to create.
   - `PROJECT`: Your [management project](https://docs.cloud.google.com/application-design-center/docs/key-concepts.md.txt#mgmt-project) ID.
   - `CATALOG`: Your catalog ID.
   - `LOCATION`: Your location ID.
   - `SPACE`: Your space ID.

   For more information, see [gcloud design-center spaces catalogs templates create](https://docs.cloud.google.com/sdk/gcloud/reference/design-center/spaces/catalogs/templates/create.md.txt).
3. Create a catalog template revision based on the Terraform module in your
   repository.

   ```bash
     gcloud design-center spaces catalogs templates revisions create REVISION \
     --project=PROJECT \
     --catalog=CATALOG \
     --location=LOCATION \
     --space=SPACE \
     --template=COMPONENT_TEMPLATE \
     --developer-connect-repo=DEVELOPER_CONNECT_REPO \
     --developer-connect-repo-ref=DEVELOPER_CONNECT_REPO_REF \
     --developer-connect-repo-dir=DEVELOPER_CONNECT_REPO_DIR \
     --metadata=METADATA

   ```

   Replace the following:
   - `REVISION`: The revision ID for the revision that you want to create.
   - `PROJECT`: Your [management project](https://docs.cloud.google.com/application-design-center/docs/key-concepts.md.txt#mgmt-project) ID.
   - `CATALOG`: Your catalog ID.
   - `LOCATION`: Your location ID.
   - `SPACE`: Your space ID.
   - `COMPONENT_TEMPLATE`: The template ID that you created in the previous step.
   - `DEVELOPER_CONNECT_REPO`: The Developer Connect repository to use as a source. For example, `projects/my-project/locations/us-central1/connections/my-connection/gitRepositoryLinks/my-repo`.
   - `DEVELOPER_CONNECT_REPO_REF`: The Git branch or tag in the repository. For example, `refs/tags/v1.0.0` or `refs/heads/main`.
   - `DEVELOPER_CONNECT_REPO_DIR`: The directory in the repository. For example, `modules/my-product`.
   - To specify the Terraform module metadata, do one of the following:
     - To use [your own metadata file](https://docs.cloud.google.com/application-design-center/docs/import-components.md.txt#prepare-terraform-module-metadata), replace `METADATA` with your `metadata.yaml` file.
     - To automatically use metadata files located in your repository, leave the --metadata flag unspecified.

   For more information, see [gcloud design-center spaces catalogs templates revisions create](https://docs.cloud.google.com/sdk/gcloud/reference/design-center/spaces/catalogs/templates/revisions/create).
4. Share your catalog with other spaces.

   ```bash
     gcloud design-center spaces catalogs shares create SHARE \
     --project=PROJECT \
     --catalog=CATALOG \
     --location=LOCATION \
     --space=SPACE \
     --destination-space=DESTINATION_SPACE
   ```

   Replace the following:
   - `SHARE`: The share ID for the share that you want to create.
   - `PROJECT`: Your [management project](https://docs.cloud.google.com/application-design-center/docs/key-concepts.md.txt#mgmt-project) ID.
   - `CATALOG`: Your catalog ID.
   - `LOCATION`: Your location ID.
   - `SPACE`: Your space ID.
   - `DESTINATION_SPACE`: The destination space ID.

   For more information, see [gcloud design-center spaces catalogs shares create](https://docs.cloud.google.com/sdk/gcloud/reference/design-center/spaces/catalogs/shares/create.md.txt).

## Create an application template using imported components

Create an application template using your imported components and
Google-provided components.

1. Create a new application template.

   ```bash
     gcloud design-center spaces application-templates create APPLICATION_TEMPLATE \
     --project=PROJECT \
     --location=LOCATION \
     --space=SPACE
   ```

   Replace the following:
   - `APPLICATION_TEMPLATE`: The template ID for the application template that you want to create.
   - `PROJECT`: Your [management project](https://docs.cloud.google.com/application-design-center/docs/key-concepts.md.txt#mgmt-project) ID.
   - `LOCATION`: Your location ID.
   - `SPACE`: Your space ID.

   For more information, see [gcloud design-center spaces application-templates create](https://docs.cloud.google.com/sdk/gcloud/reference/design-center/spaces/application-templates/create.md.txt).
2. Identify the template revision URIs for the components that you want to add
   to the template.

   ```bash
     gcloud design-center spaces shared-templates list \
     --project=PROJECT \
     --location=LOCATION \
     --space=SPACE
   ```

   Replace the following:
   - `PROJECT`: Your [management project](https://docs.cloud.google.com/application-design-center/docs/key-concepts.md.txt#mgmt-project) ID.
   - `LOCATION`: Your location ID.
   - `SPACE`: Your space ID.

   For more information, see [gcloud design-center spaces shared-templates list](https://docs.cloud.google.com/sdk/gcloud/reference/design-center/spaces/shared-templates/list).
3. Add a component to your application template.

   ```bash
     gcloud design-center spaces application-templates components create COMPONENT \
     --project=PROJECT \
     --location=LOCATION \
     --space=SPACE \
     --application-template=APPLICATION_TEMPLATE \
     --shared-template-revision-uri=SHARED_TEMPLATE_URI
   ```

   Replace the following:
   - `COMPONENT`: The component ID for the component that you want to add to the template.
   - `PROJECT`: Your [management project](https://docs.cloud.google.com/application-design-center/docs/key-concepts.md.txt#mgmt-project) ID.
   - `LOCATION`: Your location ID.
   - `SPACE`: Your space ID.
   - `APPLICATION_TEMPLATE`: The template ID for the application template where you are adding the component.
   - `SHARED_TEMPLATE_URI`: The component shared template URI. For example, `projects/my-project/locations/us-central1/spaces/my-space/sharedTemplates/my-shared-template/revisions/rev1`.

   For more information, see [gcloud design-center spaces application-templates components create](https://docs.cloud.google.com/sdk/gcloud/reference/design-center/spaces/application-templates/components/create.md.txt).
4. Add a second component to your template.

   ```bash
     gcloud design-center spaces application-templates components create COMPONENT_2 \
     --project=PROJECT \
     --location=LOCATION \
     --space=SPACE \
     --application-template=APPLICATION_TEMPLATE \
     --shared-template-revision-uri=SHARED_TEMPLATE_URI_2
   ```

   Replace the following:
   - `COMPONENT_2`: The component ID for the second component that you want to add to the template.
   - `PROJECT`: Your [management project](https://docs.cloud.google.com/application-design-center/docs/key-concepts.md.txt#mgmt-project) ID.
   - `LOCATION`: Your location ID.
   - `SPACE`: Your space ID.
   - `APPLICATION_TEMPLATE`: The template ID for the application template where you are adding the component.
   - `SHARED_TEMPLATE_URI_2`: The second component shared template URI. For example, `projects/my-project/locations/us-central1/spaces/my-space/sharedTemplates/my-shared-template/revisions/rev1`.

   For more information, see [gcloud design-center spaces application-templates components create](https://docs.cloud.google.com/sdk/gcloud/reference/design-center/spaces/application-templates/components/create.md.txt).
5. Create a connection between the two components.

   ```bash
     gcloud design-center spaces application-templates components connections create CONNECTION \
     --project=PROJECT \
     --location=LOCATION \
     --space=SPACE \
     --application-template=APPLICATION_TEMPLATE \
     --component=COMPONENT \
     --destination-component-uri=COMPONENT_2
   ```

   Replace the following:
   - `CONNECTION`: The connection ID for the connection that you want to add to the template.
   - `PROJECT`: Your [management project](https://docs.cloud.google.com/application-design-center/docs/key-concepts.md.txt#mgmt-project) ID.
   - `LOCATION`: Your location ID.
   - `SPACE`: Your space ID.
   - `APPLICATION_TEMPLATE`: The template ID for the application template where you're adding the connection.
   - `COMPONENT`: The source component ID.
   - `COMPONENT_2`: The destination component ID.

   For more information, see [gcloud design-center spaces application-templates components connections create](https://docs.cloud.google.com/sdk/gcloud/reference/design-center/spaces/application-templates/components/connections/create).

## What's next

- Before you deploy, [create an application](https://docs.cloud.google.com/application-design-center/docs/manage-applications.md.txt).
- [Deploy applications](https://docs.cloud.google.com/application-design-center/docs/deploy-applications.md.txt).
- [Export](https://docs.cloud.google.com/application-design-center/docs/download-and-deploy.md.txt) your Terraform code.