I have just upgraded from terraform v0.11 to v0.12 But i am facing lot of error
complete code @ https://github.com/sivatest82/Terraform1/tree/master/secretsv12
especially in the block
secrets = [
{
secret_id = "docker_secret.mysql_db_password.id"
secret_name = "docker_secret.mysql_db_password.name"
file_name = "/run/secrets/docker_secret.mysql_db_password.name"
},
{secret_id = "docker_secret.mysql_root_password.id"
secret_name = "docker_secret.mysql_root_password.name"
file_name = "/run/secrets/docker_secret.mysql_root_password.name"
}
]
v0.11 code which had no issue
resource "docker_service" "mysql-service" {
name = "mysql_db"
task_spec {
container_spec {
image = "${docker_image.mysql_image.name}"
secrets = [
{
secret_id = "${docker_secret.mysql_root_password.id}"
secret_name = "${docker_secret.mysql_root_password.name}"
file_name = "/run/secrets/${docker_secret.mysql_root_password.name}"
},
{
secret_id = "${docker_secret.mysql_db_password.id}"
secret_name = "${docker_secret.mysql_db_password.name}"
file_name = "/run/secrets/${docker_secret.mysql_db_password.name}"
}
]
env {
MYSQL_ROOT_PASSWORD_FILE = "/run/secrets/${docker_secret.mysql_root_password.name}"
MYSQL_DATABASE = "mydb"
MYSQL_PASSWORD_FILE = "/run/secrets/${docker_secret.mysql_db_password.name}"
}
mounts = [
{
target = "/var/lib/mysql"
source = "${docker_volume.mysql_data_volume.name}"
type = "volume"
}
]
}
networks = [
"${docker_network.private_overlay_network.name}"
]
}
}
v0.12 code
resource "docker_service" "mysql-service" {
name = "mysql_db"
task_spec {
container_spec {
image = "docker_image.mysql_image.name"
secrets = [
{
secret_id = "docker_secret.mysql_db_password.id"
secret_name = "docker_secret.mysql_db_password.name"
file_name = "/run/secrets/docker_secret.mysql_db_password.name"
},
{secret_id = "docker_secret.mysql_root_password.id"
secret_name = "docker_secret.mysql_root_password.name"
file_name = "/run/secrets/docker_secret.mysql_root_password.name"
}
]
env = {
MYSQL_ROOT_PASSWORD_FILE = "/run/secrets/docker_secret.mysql_root_password.name"
MYSQL_DATABASE = "mydb"
MYSQL_PASSWORD_FILE = "/run/secrets/docker_secret.mysql_db_password.name"
}
mounts = {
target = "/var/lib/mysql"
source = "docker_volume.mysql_data_volume.name"
type = "volume"
}
}
networks = "docker_network.private_overlay_network.name"
}
}
Error
[cloud_user@siwgftd9213c secrets]$ terraform validate
Error: Unsupported argument
on main.tf line 8, in resource "docker_service" "mysql-service":
8: secrets = {
An argument named "secrets" is not expected here. Did you mean to define a
block of type "secrets"?
Error: Unsupported argument
on main.tf line 26, in resource "docker_service" "mysql-service":
26: mounts = {
An argument named "mounts" is not expected here. Did you mean to define a
block of type "mounts"?
Error: Incorrect attribute value type
on main.tf line 34, in resource "docker_service" "mysql-service":
34: networks = "docker_network.private_overlay_network.name"
Inappropriate value for attribute "networks": set of string required.
I have already have a seperate file for them
Secrets.tf
resource "docker_secret" "mysql_root_password" {
name = "root_password"
data = "var.mysql_root_password"
}
resource "docker_secret" "mysql_db_password" {
name = "db_password"
data = "var.mysql_db_password"
}
And variables tf file
variable "mysql_root_password" {
default = "cDRzU1dvUmQwIQo="
}
variable "mysql_db_password" {
default = "cDRzU1dvUmQwIQo="
}
After tfvars.tf creation
- Able to compile code
- But unable to apply the plan
Error: Error response from daemon: rpc error: code = InvalidArgument desc = malformed secret reference
on main.tf line 1, in resource "docker_service" "mysql-service":
1: resource "docker_service" "mysql-service" {
main.tf
resource "docker_service" "mysql-service" {
name = "mysql_db"
task_spec {
container_spec {
image = docker_image.mysql_image.name
secrets {
file_name = "${local.secrets[0].file_name}"
secret_id = "${local.secrets[0].secret_id}"
}
secrets {
file_name = "${local.secrets[1].file_name}"
secret_id = "${local.secrets[1].secret_id}"
}
env = {
MYSQL_ROOT_PASSWORD_FILE = "/run/secrets/${docker_secret.mysql_root_password.name}"
MYSQL_DATABASE = "mydb"
MYSQL_PASSWORD_FILE = "/run/secrets/${docker_secret.mysql_db_password.name}"
}
mounts {
target = "/var/lib/mysql"
source = "docker_volume.mysql_data_volume.name"
type = "volume"
}
}
networks = [docker_network.private_overlay_network.name]
}
}
tfvars.tf
locals {
secrets = [
{ secret_id = docker_secret.mysql_root_password.id
secret_name = docker_secret.mysql_root_password.name
file_name = "/run/secrets/${docker_secret.mysql_root_password.name}"
},
{
secret_id = docker_secret.mysql_db_password.id
secret_name = docker_secret.mysql_db_password.name
file_name = "/run/secrets/${docker_secret.mysql_db_password.name}"
}
]
}