-
Notifications
You must be signed in to change notification settings - Fork 78
Add example with templated and bind mounted config file #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
terraform.tfvars | ||
.terraform | ||
terraform.tfstate.d | ||
terraform.tfstate.backup |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Instance with Config File | ||
|
||
This example illustrates how to deploy and expose a container to a Google Compute Engine instance in GCP, with an templated config file mounted into the container. | ||
|
||
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK --> | ||
## Inputs | ||
|
||
| Name | Description | Type | Default | Required | | ||
|------|-------------|:----:|:-----:|:-----:| | ||
| additional\_metadata | Additional metadata to attach to the instance | map(string) | `<map>` | no | | ||
| client\_email | Service account email address | string | `""` | no | | ||
| cos\_image\_name | The forced COS image to use instead of latest | string | `"cos-stable-77-12371-89-0"` | no | | ||
| instance\_name | The desired name to assign to the deployed instance | string | `"hello-world-container-vm"` | no | | ||
| project\_id | The project ID to deploy resources into | string | n/a | yes | | ||
| subnetwork | The name of the subnetwork to deploy instances into | string | n/a | yes | | ||
| subnetwork\_project | The project ID where the desired subnetwork is provisioned | string | n/a | yes | | ||
| zone | The GCP zone to deploy instances into | string | n/a | yes | | ||
|
||
## Outputs | ||
|
||
| Name | Description | | ||
|------|-------------| | ||
| container | The container metadata provided to the module | | ||
| instance\_name | The deployed instance name | | ||
| ipv4 | The public IP address of the deployed instance | | ||
| vm\_container\_label | The instance label containing container configuration | | ||
| volumes | The volume metadata provided to the module | | ||
|
||
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK --> | ||
|
||
## Running | ||
|
||
To provision this example, run the following from within this directory: | ||
|
||
- `terraform init` to get plugins | ||
- `terraform plan` to dry-run the infrastructure changes | ||
- `terraform apply` to apply the infrastructure changes | ||
- `terraform destroy` to tear down the created infrastructure | ||
|
||
## Debugging | ||
|
||
SSH into the VM instance (e.g. through the cloud console) and run the following commands. | ||
|
||
To verify that the startup script wrote the config file: | ||
|
||
``` | ||
$ cat /etc/hello-app/config.json | ||
{ | ||
"instance_name": "hello-world-container-vm-60b69fa4", | ||
"boot_timestamp": "2020-09-30T19:37:51,065136954+00:00", | ||
} | ||
``` | ||
|
||
To verify that the config file can be read from inside the container: | ||
|
||
``` | ||
$ docker exec "$(docker ps | grep hello-app | cut -f 1 -d ' ')" cat /config.json | ||
{ | ||
"instance_name": "hello-world-container-vm-60b69fa4", | ||
"boot_timestamp": "2020-09-30T19:37:51,065136954+00:00", | ||
} | ||
``` | ||
|
||
To debug the startup script, see [viewing startup script logs](https://cloud.google.com/compute/docs/startupscript#viewing_startup_script_logs) and [rerunning a startup script](https://cloud.google.com/compute/docs/startupscript#rerunthescript). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/** | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
provider "google" { | ||
project = var.project_id | ||
version = "~> 2.20" | ||
} | ||
|
||
provider "template" { | ||
version = "~> 2.1" | ||
} | ||
|
||
locals { | ||
instance_name = format("%s-%s", var.instance_name, substr(md5(module.gce-container.container.image), 0, 8)) | ||
config_path = "/etc/hello-app/config.json" | ||
} | ||
|
||
module "gce-container" { | ||
source = "../../" | ||
|
||
cos_image_name = var.cos_image_name | ||
|
||
container = { | ||
image = "gcr.io/google-samples/hello-app:1.0" | ||
|
||
volumeMounts = [ | ||
{ | ||
mountPath = "/config.json" | ||
name = "config" | ||
readOnly = true | ||
}, | ||
] | ||
} | ||
|
||
volumes = [ | ||
{ | ||
name = "config" | ||
hostPath = { | ||
path = local.config_path | ||
} | ||
}, | ||
] | ||
|
||
restart_policy = "Always" | ||
} | ||
|
||
data "template_file" "startup_script" { | ||
template = "${file("${path.module}/startup.sh.tpl")}" | ||
vars = { | ||
instance_name = local.instance_name | ||
config_path = local.config_path | ||
} | ||
} | ||
|
||
resource "google_compute_instance" "vm" { | ||
project = var.project_id | ||
name = local.instance_name | ||
machine_type = "n1-standard-1" | ||
zone = var.zone | ||
|
||
boot_disk { | ||
initialize_params { | ||
image = module.gce-container.source_image | ||
} | ||
} | ||
|
||
network_interface { | ||
subnetwork_project = var.subnetwork_project | ||
subnetwork = var.subnetwork | ||
access_config {} | ||
} | ||
|
||
tags = ["container-vm-example"] | ||
|
||
metadata = merge( | ||
{ | ||
gce-container-declaration = module.gce-container.metadata_value | ||
google-logging-enabled = "true" | ||
google-monitoring-enabled = "true" | ||
}, | ||
var.additional_metadata, | ||
) | ||
|
||
labels = { | ||
container-vm = module.gce-container.vm_container_label | ||
} | ||
|
||
service_account { | ||
email = var.client_email | ||
scopes = [ | ||
"https://www.googleapis.com/auth/cloud-platform", | ||
] | ||
} | ||
|
||
metadata_startup_script = data.template_file.startup_script.rendered | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
output "instance_name" { | ||
description = "The deployed instance name" | ||
value = google_compute_instance.vm.name | ||
} | ||
|
||
output "vm_container_label" { | ||
description = "The instance label containing container configuration" | ||
value = module.gce-container.vm_container_label | ||
} | ||
|
||
output "container" { | ||
description = "The container metadata provided to the module" | ||
value = module.gce-container.container | ||
} | ||
|
||
output "volumes" { | ||
description = "The volume metadata provided to the module" | ||
value = module.gce-container.volumes | ||
} | ||
|
||
output "ipv4" { | ||
description = "The public IP address of the deployed instance" | ||
value = google_compute_instance.vm.network_interface.0.access_config.0.nat_ip | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# Generates a config file with templated values. | ||
# | ||
# Terraform interpolation uses standard shell interpolation syntax ($). | ||
# So shell interpolation inside a Terraform template must be escaped ($$). | ||
# Command substitution does not need escaping ($). | ||
|
||
set -o errexit -o nounset -o pipefail -o posix | ||
|
||
boot_timestamp="$(date --iso-8601=ns)" | ||
|
||
mkdir -p "$(dirname "${config_path}")" | ||
|
||
cat > "${config_path}" << EOF | ||
{ | ||
"instance_name": "${instance_name}", | ||
"boot_timestamp": "$${boot_timestamp}", | ||
} | ||
EOF |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
variable "project_id" { | ||
description = "The project ID to deploy resources into" | ||
} | ||
|
||
variable "subnetwork_project" { | ||
description = "The project ID where the desired subnetwork is provisioned" | ||
} | ||
|
||
variable "subnetwork" { | ||
description = "The name of the subnetwork to deploy instances into" | ||
} | ||
|
||
variable "instance_name" { | ||
description = "The desired name to assign to the deployed instance" | ||
default = "hello-world-container-vm" | ||
} | ||
|
||
variable "zone" { | ||
description = "The GCP zone to deploy instances into" | ||
type = string | ||
} | ||
|
||
variable "additional_metadata" { | ||
type = map(string) | ||
description = "Additional metadata to attach to the instance" | ||
default = {} | ||
} | ||
|
||
variable "client_email" { | ||
description = "Service account email address" | ||
type = string | ||
default = "" | ||
} | ||
|
||
variable "cos_image_name" { | ||
description = "The forced COS image to use instead of latest" | ||
default = "cos-stable-77-12371-89-0" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding this, nice touch!