Google Cloud Run Service
This page shows how to write Terraform for Cloud Run Service and write them securely.
google_cloud_run_service (Terraform)
The Service in Cloud Run can be configured in Terraform with the resource name google_cloud_run_service
. The following sections describe 5 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "google_cloud_run_service" "run_secondary_1" {
name = "secondary-1"
location = "us-central1"
template {
spec {
resource "google_cloud_run_service" "run_primary_1" {
name = "primary-1"
location = "us-east1"
template {
spec {
resource "google_cloud_run_service" "app" {
name = "app"
location = "us-east1"
autogenerate_revision_name = true
template {
spec {
resource "google_cloud_run_service" "ingestion_service" {
name = var.ingestion_service_name
location = var.compute_region
project = var.project_id
template {
resource "google_cloud_run_service" "cloud-run-test" {
name = "cloud-run-test"
location = var.DEFAULT_REGION
template {
spec {
Parameters
-
autogenerate_revision_name
optional - bool -
id
optional computed - string -
location
required - string
The location of the cloud run instance. eg us-central1
-
name
required - string
Name must be unique within a namespace, within a Cloud Run region. Is required when creating resources. Name is primarily intended for creation idempotence and configuration definition. Cannot be updated. More info: http://kubernetes.io/docs/user-guide/identifiers#names
The current status of the Service.
-
conditions
- list of object -
latest_created_revision_name
- string -
latest_ready_revision_name
- string -
observed_generation
- number -
url
- string -
metadata
list block-
annotations
optional computed - map from string to string
Annotations is a key value map stored with a resource that may be set by external tools to store and retrieve arbitrary metadata. More info: http://kubernetes.io/docs/user-guide/annotations Note: The Cloud Run API may add additional annotations that were not provided in your config. If terraform plan shows a diff where a server-side annotation is added, you can add it to your config or apply the lifecycle.ignore_changes rule to the metadata.0.annotations field. Cloud Run (fully managed) uses the following annotation keys to configure features on a Service: - 'run.googleapis.com/ingress' sets the ingress settings for the Service. For example, '"run.googleapis.com/ingress" = "all"'.
-
generation
optional computed - number
A sequence number representing a specific generation of the desired state.
-
labels
optional computed - map from string to string
Map of string keys and values that can be used to organize and categorize (scope and select) objects. May match selectors of replication controllers and routes. More info: http://kubernetes.io/docs/user-guide/labels
-
namespace
optional computed - string
In Cloud Run the namespace must be equal to either the project ID or project number.
-
resource_version
optional computed - string
An opaque value that represents the internal version of this object that can be used by clients to determine when objects have changed. May be used for optimistic concurrency, change detection, and the watch operation on a resource or set of resources. They may only be valid for a particular resource or set of resources. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#concurrency-control-and-consistency
-
self_link
optional computed - string
SelfLink is a URL representing this object.
-
uid
optional computed - string
UID is a unique id generated by the server on successful creation of a resource and is not allowed to change on PUT operations. More info: http://kubernetes.io/docs/user-guide/identifiers#uids
-
-
template
list block-
metadata
list block-
annotations
optional computed - map from string to string
Annotations is a key value map stored with a resource that may be set by external tools to store and retrieve arbitrary metadata. More info: http://kubernetes.io/docs/user-guide/annotations Note: The Cloud Run API may add additional annotations that were not provided in your config. If terraform plan shows a diff where a server-side annotation is added, you can add it to your config or apply the lifecycle.ignore_changes rule to the metadata.0.annotations field.
-
generation
optional computed - number
A sequence number representing a specific generation of the desired state.
-
labels
optional - map from string to string
Map of string keys and values that can be used to organize and categorize (scope and select) objects. May match selectors of replication controllers and routes. More info: http://kubernetes.io/docs/user-guide/labels
-
name
optional computed - string
Name must be unique within a namespace, within a Cloud Run region. Is required when creating resources. Name is primarily intended for creation idempotence and configuration definition. Cannot be updated. More info: http://kubernetes.io/docs/user-guide/identifiers#names
-
namespace
optional computed - string
In Cloud Run the namespace must be equal to either the project ID or project number. It will default to the resource's project.
-
resource_version
optional computed - string
An opaque value that represents the internal version of this object that can be used by clients to determine when objects have changed. May be used for optimistic concurrency, change detection, and the watch operation on a resource or set of resources. They may only be valid for a particular resource or set of resources. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#concurrency-control-and-consistency
-
self_link
optional computed - string
SelfLink is a URL representing this object.
-
uid
optional computed - string
UID is a unique id generated by the server on successful creation of a resource and is not allowed to change on PUT operations. More info: http://kubernetes.io/docs/user-guide/identifiers#uids
-
-
spec
list block-
container_concurrency
optional computed - number
ContainerConcurrency specifies the maximum allowed in-flight (concurrent) requests per container of the Revision. Values are: - '0' thread-safe, the system should manage the max concurrency. This is the default value. - '1' not-thread-safe. Single concurrency - '2-N' thread-safe, max concurrency of N
-
service_account_name
optional - string
Email address of the IAM service account associated with the revision of the service. The service account represents the identity of the running revision, and determines what permissions the revision has. If not provided, the revision will use the project's default service account.
-
serving_state
optional computed - string
ServingState holds a value describing the state the resources are in for this Revision. It is expected that the system will manipulate this based on routability and load.
-
timeout_seconds
optional computed - number
TimeoutSeconds holds the max duration the instance is allowed for responding to a request.
-
containers
list block-
args
optional - list of string
Arguments to the entrypoint. The docker image's CMD is used if this is not provided. Variable references $(VAR_NAME) are expanded using the container's environment. If a variable cannot be resolved, the reference in the input string will be unchanged. The $(VAR_NAME) syntax can be escaped with a double $$, ie: $$(VAR_NAME). Escaped references will never be expanded, regardless of whether the variable exists or not. More info: https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell
-
command
optional - list of string
Entrypoint array. Not executed within a shell. The docker image's ENTRYPOINT is used if this is not provided. Variable references $(VAR_NAME) are expanded using the container's environment. If a variable cannot be resolved, the reference in the input string will be unchanged. The $(VAR_NAME) syntax can be escaped with a double $$, ie: $$(VAR_NAME). Escaped references will never be expanded, regardless of whether the variable exists or not. More info: https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell
-
image
required - string
Docker image name. This is most often a reference to a container located in the container registry, such as gcr.io/cloudrun/hello More info: https://kubernetes.io/docs/concepts/containers/images
-
working_dir
optional - string
Container's working directory. If not specified, the container runtime's default will be used, which might be configured in the container image.
-
env
list block-
name
optional - string
Name of the environment variable.
-
value
optional - string
Variable references $(VAR_NAME) are expanded using the previous defined environment variables in the container and any route environment variables. If a variable cannot be resolved, the reference in the input string will be unchanged. The $(VAR_NAME) syntax can be escaped with a double $$, ie: $$(VAR_NAME). Escaped references will never be expanded, regardless of whether the variable exists or not. Defaults to "".
-
-
env_from
list block-
prefix
optional - string
An optional identifier to prepend to each key in the ConfigMap.
-
config_map_ref
list block-
optional
optional - bool
Specify whether the ConfigMap must be defined
-
local_object_reference
list block-
name
required - string
Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
-
-
-
secret_ref
list block-
optional
optional - bool
Specify whether the Secret must be defined
-
local_object_reference
list block-
name
required - string
Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
-
-
-
-
ports
list block-
container_port
required - number
Port number.
-
name
optional - string
Name of the port.
-
protocol
optional - string
Protocol used on port. Defaults to TCP.
-
-
resources
list block-
limits
optional computed - map from string to string
Limits describes the maximum amount of compute resources allowed. The values of the map is string form of the 'quantity' k8s type: https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/apimachinery/pkg/api/resource/quantity.go
-
requests
optional - map from string to string
Requests describes the minimum amount of compute resources required. If Requests is omitted for a container, it defaults to Limits if that is explicitly specified, otherwise to an implementation-defined value. The values of the map is string form of the 'quantity' k8s type: https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/apimachinery/pkg/api/resource/quantity.go
-
-
-
-
-
timeouts
single block -
traffic
list block-
latest_revision
optional - bool
LatestRevision may be optionally provided to indicate that the latest ready Revision of the Configuration should be used for this traffic target. When provided LatestRevision must be true if RevisionName is empty; it must be false when RevisionName is non-empty.
-
percent
required - number
Percent specifies percent of the traffic to this Revision or Configuration.
-
revision_name
optional - string
RevisionName of a specific revision to which to send this portion of traffic.
-
Explanation in Terraform Registry
Service acts as a top-level container that manages a set of Routes and Configurations which implement a network service. Service exists to provide a singular abstraction which can be access controlled, reasoned about, and which encapsulates software lifecycle decisions such as rollout policy and team resource ownership. Service acts only as an orchestrator of the underlying Routes and Configurations (much as a kubernetes Deployment orchestrates ReplicaSets). The Service's controller will track the statuses of its owned Configuration and Route, reflecting their statuses and conditions as its own. See also: https://github.com/knative/serving/blob/master/docs/spec/overview.md#service To get more information about Service, see:
- API documentation
- How-to Guides
- Official Documentation
Warning:
google_cloudrun_service
creates a Managed Google Cloud Run Service. If you need to create a Cloud Run Service on Anthos(GKE/VMWare) then you will need to create it using the kubernetes alpha provider. Have a look at the Cloud Run Anthos example below.
Frequently asked questions
What is Google Cloud Run Service?
Google Cloud Run Service is a resource for Cloud Run of Google Cloud Platform. Settings can be wrote in Terraform.
Where can I find the example code for the Google Cloud Run Service?
For Terraform, the mike-longledge/ProjectAtyeti, mike-longledge/ProjectAtyeti and theomessin/sandbox source code examples are useful. See the Terraform Example section for further details.