Google Compute Engine Disk
This page shows how to write Terraform for Compute Engine Disk and write them securely.
google_compute_disk (Terraform)
The Disk in Compute Engine can be configured in Terraform with the resource name google_compute_disk
. The following sections describe 1 example of how to use the resource and its parameters.
Example Usage from GitHub
resource "google_compute_disk" "neo-database-01-disk" {
name = "neo-database-01-disk"
type = "pd-ssd"
zone = "us-east1-b"
size = "100"
}
Security Best Practices for google_compute_disk
There are 2 settings in google_compute_disk that should be taken care of for security reasons. The following section explain an overview and example code.
Ensure the encryption key for your GCE disk is stored securely
It is better to store the encryption key for your GCE disk securely. Secret Manager could be used instead.
Ensure your GCE disk is encrypted
It is better to encrypt GCE disk. Encryption reduces the risk of data leakage.
Parameters
-
creation_timestamp
optional computed - string
Creation timestamp in RFC3339 text format.
-
description
optional - string
An optional description of this resource. Provide this property when you create the resource.
The image from which to initialize this disk. This can be one of: the image's 'self_link', 'projects/[project]/global/images/[image]', 'projects/[project]/global/images/family/[family]', 'global/images/[image]', 'global/images/family/[family]', 'family/[family]', '[project]/[family]', '[project]/[image]', '[family]', or '[image]'. If referred by family, the images names must include the family name. If they don't, use the google_compute_image data source. For instance, the image 'centos-6-v20180104' includes its family name 'centos-6'. These images can be referred by family name here.
-
label_fingerprint
optional computed - string
The fingerprint used for optimistic locking of this resource. Used internally during updates.
-
labels
optional - map from string to string
Labels to apply to this disk. A list of key->value pairs.
-
last_attach_timestamp
optional computed - string
Last attach timestamp in RFC3339 text format.
-
last_detach_timestamp
optional computed - string
Last detach timestamp in RFC3339 text format.
-
name
required - string
Name of the resource. Provided by the client when the resource is created. The name must be 1-63 characters long, and comply with RFC1035. Specifically, the name must be 1-63 characters long and match the regular expression 'a-z?' which means the first character must be a lowercase letter, and all following characters must be a dash, lowercase letter, or digit, except the last character, which cannot be a dash.
-
physical_block_size_bytes
optional computed - number
Physical block size of the persistent disk, in bytes. If not present in a request, a default value is used. Currently supported sizes are 4096 and 16384, other sizes may be added in the future. If an unsupported value is requested, the error message will list the supported values for the caller's project.
-
project
optional computed - string -
self_link
optional computed - string -
size
optional computed - number
Size of the persistent disk, specified in GB. You can specify this field when creating a persistent disk using the 'image' or 'snapshot' parameter, or specify it alone to create an empty persistent disk. If you specify this field along with 'image' or 'snapshot', the value must not be less than the size of the image or the size of the snapshot. ~>NOTE If you change the size, Terraform updates the disk size if upsizing is detected but recreates the disk if downsizing is requested. You can add 'lifecycle.prevent_destroy' in the config to prevent destroying and recreating.
-
snapshot
optional - string
The source snapshot used to create this disk. You can provide this as a partial or full URL to the resource. If the snapshot is in another project than this disk, you must supply a full URL. For example, the following are valid values: 'https://www.googleapis.com/compute/v1/projects/project/global/snapshots/snapshot' 'projects/project/global/snapshots/snapshot' 'global/snapshots/snapshot' 'snapshot'
-
source_image_id
optional computed - string
The ID value of the image used to create this disk. This value identifies the exact image that was used to create this persistent disk. For example, if you created the persistent disk from an image that was later deleted and recreated under the same name, the source image ID would identify the exact version of the image that was used.
-
source_snapshot_id
optional computed - string
The unique ID of the snapshot used to create this disk. This value identifies the exact snapshot that was used to create this persistent disk. For example, if you created the persistent disk from a snapshot that was later deleted and recreated under the same name, the source snapshot ID would identify the exact version of the snapshot that was used.
-
type
optional - string
URL of the disk type resource describing which disk type to use to create the disk. Provide this when creating the disk.
-
users
optional computed - list of string
Links to the users of the disk (attached instances) in form: project/zones/zone/instances/instance
-
zone
optional computed - string
A reference to the zone where the disk resides.
-
disk_encryption_key
list block-
kms_key_self_link
optional - string
The self link of the encryption key used to encrypt the disk. Also called KmsKeyName in the cloud console. Your project's Compute Engine System service account ('service-[[PROJECT_NUMBER]]@compute-system.iam.gserviceaccount.com') must have 'roles/cloudkms.cryptoKeyEncrypterDecrypter' to use this feature. See https://cloud.google.com/compute/docs/disks/customer-managed-encryption#encrypt_a_new_persistent_disk_with_your_own_keys
-
kms_key_service_account
optional - string
The service account used for the encryption request for the given KMS key. If absent, the Compute Engine Service Agent service account is used.
-
raw_key
optional - string
Specifies a 256-bit customer-supplied encryption key, encoded in RFC 4648 base64 to either encrypt or decrypt this resource.
-
sha256
optional computed - string
The RFC 4648 base64 encoded SHA-256 hash of the customer-supplied encryption key that protects this resource.
-
-
source_image_encryption_key
list block-
kms_key_self_link
optional - string
The self link of the encryption key used to encrypt the disk. Also called KmsKeyName in the cloud console. Your project's Compute Engine System service account ('service-[[PROJECT_NUMBER]]@compute-system.iam.gserviceaccount.com') must have 'roles/cloudkms.cryptoKeyEncrypterDecrypter' to use this feature. See https://cloud.google.com/compute/docs/disks/customer-managed-encryption#encrypt_a_new_persistent_disk_with_your_own_keys
-
kms_key_service_account
optional - string
The service account used for the encryption request for the given KMS key. If absent, the Compute Engine Service Agent service account is used.
-
raw_key
optional - string
Specifies a 256-bit customer-supplied encryption key, encoded in RFC 4648 base64 to either encrypt or decrypt this resource.
-
sha256
optional computed - string
The RFC 4648 base64 encoded SHA-256 hash of the customer-supplied encryption key that protects this resource.
-
-
source_snapshot_encryption_key
list block-
kms_key_self_link
optional - string
The self link of the encryption key used to encrypt the disk. Also called KmsKeyName in the cloud console. Your project's Compute Engine System service account ('service-[[PROJECT_NUMBER]]@compute-system.iam.gserviceaccount.com') must have 'roles/cloudkms.cryptoKeyEncrypterDecrypter' to use this feature. See https://cloud.google.com/compute/docs/disks/customer-managed-encryption#encrypt_a_new_persistent_disk_with_your_own_keys
-
kms_key_service_account
optional - string
The service account used for the encryption request for the given KMS key. If absent, the Compute Engine Service Agent service account is used.
-
raw_key
optional - string
Specifies a 256-bit customer-supplied encryption key, encoded in RFC 4648 base64 to either encrypt or decrypt this resource.
-
sha256
optional computed - string
The RFC 4648 base64 encoded SHA-256 hash of the customer-supplied encryption key that protects this resource.
-
-
timeouts
single block
Explanation in Terraform Registry
Persistent disks are durable storage devices that function similarly to the physical disks in a desktop or a server. Compute Engine manages the hardware behind these devices to ensure data redundancy and optimize performance for you. Persistent disks are available as either standard hard disk drives (HDD) or solid-state drives (SSD). Persistent disks are located independently from your virtual machine instances, so you can detach or move persistent disks to keep your data even after you delete your instances. Persistent disk performance scales automatically with size, so you can resize your existing persistent disks or add more persistent disks to an instance to meet your performance and storage space requirements. Add a persistent disk to your instance when you need reliable and affordable storage with consistent performance characteristics. To get more information about Disk, see:
- API documentation
- How-to Guides
- Adding a persistent disk
Warning: All arguments including
disk_encryption_key.raw_key
will be stored in the raw state as plain-text. Read more about sensitive data in state.
Tips: Best Practices for The Other Google Compute Engine Resources
In addition to the google_compute_firewall, Google Compute Engine has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.
google_compute_firewall
Ensure your VPC firewall blocks unwanted outbound traffic
It is better to block unwanted outbound traffic not to expose resources in the VPC to unwanted attacks.
google_compute_instance
Ensure appropriate service account is assigned to your GCE instance
It is better to create a custom service account for the instance and assign it.
google_compute_project_metadata
Ensure OS login for your GCE instances is enabled at project level
It is better to enable OS login for your GCE instances. Enabling OS login ensures that SSH keys used to connect to instances are mapped with IAM users, allowing centralized and automated SSH key management.
google_compute_ssl_policy
Ensure to use modern TLS protocols
It's better to adopt TLS v1.2+ instead of outdated TLS protocols.
google_compute_subnetwork
Ensure VPC flow logging is enabled
It is better to enable VPC flow logging. VPC flow logging allows us to audit traffic in your network.
Frequently asked questions
What is Google Compute Engine Disk?
Google Compute Engine Disk is a resource for Compute Engine of Google Cloud Platform. Settings can be wrote in Terraform.
Where can I find the example code for the Google Compute Engine Disk?
For Terraform, the NarrativeCompany/narrative-infrastructure source code example is useful. See the Terraform Example section for further details.