|
| 1 | +# Cloud Composer Example within VPC SC and Shared VPC |
| 2 | + |
| 3 | +This guide provides the infrastructure as code as a reference point for users/ customers |
| 4 | +who want to perform all the steps documented in these links through Terraform |
| 5 | +- Shared VPC : https://cloud.google.com/composer/docs/composer-2/configure-shared-vpc |
| 6 | +- Firewall rules : https://cloud.google.com/composer/docs/composer-2/configure-private-ip#step_3_configure_firewall_rules |
| 7 | +- Cloud DNS rules: https://cloud.google.com/composer/docs/composer-2/configure-vpc-sc#connectivity_to_the_restrictedgoogleapiscom_endpoint |
| 8 | + |
| 9 | +## Compatibility |
| 10 | + |
| 11 | +This module is meant for use with Terraform 0.13+ and tested using Terraform 1.0+. |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | + |
| 16 | +```hcl |
| 17 | +
|
| 18 | +
|
| 19 | +module "composer_env" { |
| 20 | + source = "terraform-google-modules/composer/google//modules/create_environment_v2" |
| 21 | + project_id = var.service_project_id |
| 22 | + network_project_id = var.network_project_id |
| 23 | + composer_env_name = var.composer_env_name |
| 24 | + region = var.region |
| 25 | + composer_service_account = google_service_account.composer_sa.email |
| 26 | + network = var.network |
| 27 | + subnetwork = var.subnetwork |
| 28 | + pod_ip_allocation_range_name = var.pod_ip_allocation_range_name |
| 29 | + service_ip_allocation_range_name = var.service_ip_allocation_range_name |
| 30 | + grant_sa_agent_permission = true |
| 31 | + use_private_environment = true |
| 32 | + enable_private_endpoint = true |
| 33 | + environment_size = "ENVIRONMENT_SIZE_SMALL" |
| 34 | + scheduler = { |
| 35 | + cpu = 1 |
| 36 | + memory_gb = 1.875 |
| 37 | + storage_gb = 1 |
| 38 | + count = 1 |
| 39 | + } |
| 40 | + web_server = { |
| 41 | + cpu = 1 |
| 42 | + memory_gb = 2 |
| 43 | + storage_gb = 10 |
| 44 | + } |
| 45 | + worker = { |
| 46 | + cpu =1 |
| 47 | + memory_gb = 2 |
| 48 | + storage_gb = 1 |
| 49 | + min_count = 1 |
| 50 | + max_count = 6 |
| 51 | + } |
| 52 | +} |
| 53 | +
|
| 54 | +``` |
| 55 | + |
| 56 | + |
| 57 | +## Requirements |
| 58 | + |
| 59 | +Before this module can be used on a project, you must ensure that the following pre-requisites are fulfilled: |
| 60 | + |
| 61 | +1. The projects (host and service) are added within a VPC Service Control Perimeter and have the Cloud DNS/ routes configured appropriately for for Restricted VIP. |
| 62 | +2. The Service Account you execute the module with has the right [permissions](#configure-a-service-account). |
| 63 | +3. The Compute Engine and Kubernetes Engine APIs are [active](#enable-apis) on the project you will launch the cluster in. |
| 64 | +4. If you are using a Shared VPC, the APIs must also be activated on the Shared VPC host project and your service account needs the proper permissions there. |
| 65 | +5. As Composer uses GKE under the hood, certain networking configurations such as creation of network, subnets, GKE IP ranges are done before hand. This code only configures composer specific firewall rules and DNS entry. |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +### Configure a Service Account |
| 70 | +In order to execute this module you must have a Service Account with the |
| 71 | +following project roles in the Service Project: |
| 72 | +- roles/viewer - to get the service project |
| 73 | +- roles/composer.admin - to create the composer environment |
| 74 | +- roles/iam.serviceAccountCreator- to create the Service account attached to the composer environment |
| 75 | +- roles/resourcemanager.projectIamAdmin - to make iam bindings in service project for composer worker |
| 76 | +- roles/serviceaccountuser - If you do not assign this, you get the User not authorized to act as service account 'xxxx. The user must be granted iam.serviceAccounts.actAs permission |
| 77 | + |
| 78 | +Roles needed in the Network project (where host vpc resides) |
| 79 | +- roles/dns.admin - Create DNS Zone that contains records to access Airflow UI |
| 80 | +- roles/compute.securityAdmin - You can explore custom roles as well to compute.firewalls.create in network project (Compute Security Admin) |
| 81 | +- roles/resourcemanager.projectIamAdmin - to make iam bindings in host project for Compute Network user |
| 82 | + |
| 83 | +As the project's are within VPC SC, you may need to allow list the orchestrator service account that has these permissions |
| 84 | +### Destroy |
| 85 | +When destroying, destroy the composer resource first through targeted operation |
| 86 | + |
| 87 | +terraform destroy -target=module.composer_env |
| 88 | + |
| 89 | +Then proceed to destroying everything else |
| 90 | +### Enable APIs |
| 91 | +In order to operate with the Service Account you must activate the following APIs on the project where the Service Account was created: |
| 92 | + |
| 93 | +- Cloud Composer - composer.googleapis.com |
| 94 | +- Kubernetes Engine API - container.googleapis.com |
| 95 | +- Service Usage API - serviceusage.googleapis.com |
| 96 | + |
| 97 | +<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK --> |
| 98 | +## Inputs |
| 99 | + |
| 100 | +| Name | Description | Type | Default | Required | |
| 101 | +|------|-------------|------|---------|:--------:| |
| 102 | +| network | The name of the network being created | `string` | n/a | yes | |
| 103 | +| project\_id | The project ID of the shared VPC's host (for shared vpc support) | `string` | n/a | yes | |
| 104 | +| service\_project\_id | Project ID where Cloud Composer Environment is created. | `string` | n/a | yes | |
| 105 | + |
| 106 | +## Outputs |
| 107 | + |
| 108 | +| Name | Description | |
| 109 | +|------|-------------| |
| 110 | +| airflow\_uri | URI of the Apache Airflow Web UI hosted within the Cloud Composer Environment. | |
| 111 | +| composer\_env\_id | ID of Cloud Composer Environment. | |
| 112 | +| composer\_env\_name | Name of the Cloud Composer Environment. | |
| 113 | +| gcs\_bucket | Google Cloud Storage bucket which hosts DAGs for the Cloud Composer Environment. | |
| 114 | +| gke\_cluster | Google Kubernetes Engine cluster used to run the Cloud Composer Environment. | |
| 115 | +| service\_project\_id | The id of the project where cloud composer was created. | |
| 116 | + |
| 117 | +<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK --> |
0 commit comments