Google Cloud IoT Core Device

This page shows how to write Terraform for Cloud IoT Core Device and write them securely.

google_cloudiot_device (Terraform)

The Device in Cloud IoT Core can be configured in Terraform with the resource name google_cloudiot_device. The following sections describe 5 examples of how to use the resource and its parameters.

Example Usage from GitHub

devices.tf#L1
resource "google_cloudiot_device" "raspberrypi3" {
  name     = "raspberrypi3"
  registry = google_cloudiot_registry.iot_poc_registry.id

  credentials {
    public_key {
iot.tf#L22
resource "google_cloudiot_device" "foglamp-demo-device" {
    name     = "foglamp-demo-device"
    registry = google_cloudiot_registry.foglamp-demo-registry.id

    credentials {
        public_key {
main.tf#L7
resource "google_cloudiot_device" "this" {
  blocked   = var.blocked
  log_level = var.log_level
  metadata  = var.metadata
  name      = var.name
  registry  = var.registry
main.tf#L108
resource "google_cloudiot_device" "device_dev1" {
  name     = "device-dev1"
  registry = google_cloudiot_registry.test_registry.id

  credentials {
    public_key {
main.tf#L210
resource "google_cloudiot_device" "instance-1" {
  name     = "instance-1"
  registry = google_cloudiot_registry.test-registry.id
}

Review your Terraform file for Google best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

The following arguments are supported:

  • name - (Required) A unique name for the resource.

  • registry - (Required) The name of the device registry where this device should be created.


  • credentials - (Optional) The credentials used to authenticate this device. Structure is documented below.

  • blocked - (Optional) If a device is blocked, connections or requests from this device will fail.

  • log_level - (Optional) The logging verbosity for device activity. Possible values are NONE, ERROR, INFO, and DEBUG.

  • metadata - (Optional) The metadata key-value pairs assigned to the device.

  • gateway_config - (Optional) Gateway-related configuration and state. Structure is documented below.

The credentials block supports:

  • expiration_time - (Optional) The time at which this credential becomes invalid.

  • public_key - (Required) A public key used to verify the signature of JSON Web Tokens (JWTs). Structure is documented below.

The public_key block supports:

  • format - (Required) The format of the key. Possible values are RSA_PEM, RSA_X509_PEM, ES256_PEM, and ES256_X509_PEM.

  • key - (Required) The key data.

The gateway_config block supports:

  • gateway_type - (Optional) Indicates whether the device is a gateway. Default value is NON_GATEWAY. Possible values are GATEWAY and NON_GATEWAY.

  • gateway_auth_method - (Optional) Indicates whether the device is a gateway. Possible values are ASSOCIATION_ONLY, DEVICE_AUTH_TOKEN_ONLY, and ASSOCIATION_AND_DEVICE_AUTH_TOKEN.

  • last_accessed_gateway_id - The ID of the gateway the device accessed most recently.

  • last_accessed_gateway_time - The most recent time at which the device accessed the gateway specified in last_accessed_gateway.

In addition to the arguments listed above, the following computed attributes are exported:

  • id - an identifier for the resource with format [[registry]]/devices/[[name]]

  • num_id - A server-defined unique numeric ID for the device. This is a more compact way to identify devices, and it is globally unique.

  • last_heartbeat_time - The last time an MQTT PINGREQ was received.

  • last_event_time - The last time a telemetry event was received.

  • last_state_time - The last time a state event was received.

  • last_config_ack_time - The last time a cloud-to-device config version acknowledgment was received from the device.

  • last_config_send_time - The last time a cloud-to-device config version was sent to the device.

  • last_error_time - The time the most recent error occurred, such as a failure to publish to Cloud Pub/Sub.

  • last_error_status - The error message of the most recent error, such as a failure to publish to Cloud Pub/Sub. Structure is documented below.

  • config - The most recent device configuration, which is eventually sent from Cloud IoT Core to the device. Structure is documented below.

  • state - The state most recently received from the device. Structure is documented below.

The `last_error_status` block contains:
  • number - (Optional) The status code, which should be an enum value of google.rpc.Code.

  • message - (Optional) A developer-facing error message, which should be in English.

  • details - (Optional) A list of messages that carry the error details.

The `config` block contains:
  • version - The version of this update.

  • cloud_update_time - The time at which this configuration version was updated in Cloud IoT Core.

  • device_ack_time - The time at which Cloud IoT Core received the acknowledgment from the device, indicating that the device has received this configuration version.

  • binary_data - (Optional) The device configuration data.

The `state` block contains:
  • update_time - (Optional) The time at which this state version was updated in Cloud IoT Core.

  • binary_data - (Optional) The device state data.

Explanation in Terraform Registry

A Google Cloud IoT Core device. To get more information about Device, see:

Frequently asked questions

What is Google Cloud IoT Core Device?

Google Cloud IoT Core Device is a resource for Cloud IoT Core of Google Cloud Platform. Settings can be wrote in Terraform.

Where can I find the example code for the Google Cloud IoT Core Device?

For Terraform, the s1moe2/gcp-iot-poc, badal-io/dataflow-timeseries-iot-gas-demo and niveklabs/google source code examples are useful. See the Terraform Example section for further details.

security-icon

Automate config file reviews on your commits

Fix issues in your infrastructure as code with auto-generated patches.