terraform-provider-coder icon indicating copy to clipboard operation
terraform-provider-coder copied to clipboard

coder metadata not shown

Open anasfanani opened this issue 4 months ago • 0 comments

Im using coder template aws.

resource "aws_vpc" "main" {
  cidr_block           = "10.0.0.0/16"
  enable_dns_hostnames = true
  enable_dns_support   = true

  tags = {
    Name = "coder-vpc"
  }
}

resource "aws_subnet" "main" {
  vpc_id                  = aws_vpc.main.id
  cidr_block              = "10.0.1.0/24"
  availability_zone       = "${data.coder_parameter.region.value}a"
  map_public_ip_on_launch = true

  tags = {
    Name = "coder-subnet"
  }
}

resource "aws_internet_gateway" "main" {
  vpc_id = aws_vpc.main.id

  tags = {
    Name = "coder-igw"
  }
}

resource "aws_route_table" "main" {
  vpc_id = aws_vpc.main.id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.main.id
  }

  tags = {
    Name = "coder-rt"
  }
}

resource "aws_route_table_association" "main" {
  subnet_id      = aws_subnet.main.id
  route_table_id = aws_route_table.main.id
}

resource "aws_security_group" "allow_all" {
  name        = "allow_all"
  description = "Allow all inbound traffic and all outbound traffic"
  vpc_id      = aws_vpc.main.id

  tags = {
    Name = "allow_all"
  }
}
resource "aws_vpc_security_group_ingress_rule" "allow_all_traffic_ipv4" {
  security_group_id = aws_security_group.allow_all.id
  cidr_ipv4         = "0.0.0.0/0"
  ip_protocol       = "-1" # semantically equivalent to all ports
}
resource "aws_vpc_security_group_ingress_rule" "allow_all_traffic_ipv6" {
  security_group_id = aws_security_group.allow_all.id
  cidr_ipv6         = "::/0"
  ip_protocol       = "-1" # semantically equivalent to all ports
}

resource "aws_vpc_security_group_egress_rule" "allow_all_traffic_ipv4" {
  security_group_id = aws_security_group.allow_all.id
  cidr_ipv4         = "0.0.0.0/0"
  ip_protocol       = "-1" # semantically equivalent to all ports
}
resource "aws_vpc_security_group_egress_rule" "allow_all_traffic_ipv6" {
  security_group_id = aws_security_group.allow_all.id
  cidr_ipv6         = "::/0"
  ip_protocol       = "-1" # semantically equivalent to all ports
}
resource "aws_eip" "dev" {
  instance = aws_instance.dev.id
  domain   = "vpc"
}
resource "aws_eip_association" "eip_assoc" {
  instance_id   = aws_instance.dev.id
  allocation_id = aws_eip.dev.id
}

resource "aws_instance" "dev" {
  ami               = data.aws_ami.ubuntu.id
  availability_zone = "${data.coder_parameter.region.value}a"
  instance_type     = data.coder_parameter.instance_type.value
  subnet_id         = aws_subnet.main.id
  vpc_security_group_ids = [
    aws_security_group.allow_all.id
  ]
  key_name  = data.coder_workspace.me.name
  user_data = data.cloudinit_config.user_data.rendered
  tags = {
    Name = "coder-${data.coder_workspace_owner.me.name}-${data.coder_workspace.me.name}"
    # Required if you are using our example policy, see template README
    Coder_Provisioned = "true"
  }
  lifecycle {
    ignore_changes = [ami]
  }
}

resource "coder_metadata" "workspace_info" {
  count       = data.coder_workspace.me.start_count
  resource_id = aws_instance.dev.id
  hide        = false
  item {
    key   = "region"
    value = data.coder_parameter.region.value
  }
  item {
    key   = "instance type"
    value = aws_instance.dev.instance_type
  }
  item {
    key   = "disk"
    value = "${aws_instance.dev.root_block_device[0].volume_size} GiB"
  }
  item {
    key   = "public ip"
    value = aws_eip.dev.public_ip
  }
  item {
    key   = "public dns"
    value = aws_eip.dev.public_dns
  }
}

Log:

Terraform 1.12.2
coder_agent.main: Plan to create
module.vscode-web[0].coder_app.vscode-web: Plan to create
module.vscode-web[0].coder_script.vscode-web: Plan to create
aws_key_pair.owner: Plan to create
aws_vpc.main: Plan to create
data.cloudinit_config.user_data: Plan to read
aws_internet_gateway.main: Plan to create
aws_subnet.main: Plan to create
aws_security_group.allow_all: Plan to create
aws_route_table.main: Plan to create
aws_route_table_association.main: Plan to create
aws_vpc_security_group_egress_rule.allow_all_traffic_ipv4: Plan to create
aws_vpc_security_group_egress_rule.allow_all_traffic_ipv6: Plan to create
aws_vpc_security_group_ingress_rule.allow_all_traffic_ipv4: Plan to create
aws_vpc_security_group_ingress_rule.allow_all_traffic_ipv6: Plan to create
aws_instance.dev: Plan to create
aws_ec2_instance_state.dev: Plan to create
aws_eip.dev: Plan to create
aws_eip_association.eip_assoc: Plan to create
coder_metadata.workspace_info[0]: Plan to create
coder_agent.main: Creating...
coder_agent.main: Creation complete after 0s [id=4722719b-7e5a-4173-bc89-b3a365787936]
module.vscode-web[0].coder_app.vscode-web: Creating...
module.vscode-web[0].coder_script.vscode-web: Creating...
module.vscode-web[0].coder_app.vscode-web: Creation complete after 0s [id=a0f3f414-9f98-4af1-a9d1-20f2bf1bb796]
module.vscode-web[0].coder_script.vscode-web: Creation complete after 1s [id=d1587801-5f16-4c53-943c-f89fabc192b6]
aws_key_pair.owner: Creating...
aws_vpc.main: Creating...
aws_key_pair.owner: Creation complete after 1s [id=amber-chimpanzee-4]
data.cloudinit_config.user_data: Refreshing...
data.cloudinit_config.user_data: Refresh complete after 0s [id=3779634824]
aws_vpc.main: Still creating... [10s elapsed]
aws_vpc.main: Creation complete after 13s [id=vpc-0dd5cbcffeac77348]
aws_internet_gateway.main: Creating...
aws_security_group.allow_all: Creating...
aws_subnet.main: Creating...
aws_internet_gateway.main: Creation complete after 0s [id=igw-0431d4adce409cfd3]
aws_route_table.main: Creating...
aws_route_table.main: Creation complete after 1s [id=rtb-0f3e9ea396c45f29c]
aws_security_group.allow_all: Creation complete after 2s [id=sg-008d9f0ee46fdeb2f]
aws_vpc_security_group_ingress_rule.allow_all_traffic_ipv4: Creating...
aws_vpc_security_group_egress_rule.allow_all_traffic_ipv6: Creating...
aws_vpc_security_group_ingress_rule.allow_all_traffic_ipv6: Creating...
aws_vpc_security_group_egress_rule.allow_all_traffic_ipv4: Creating...
aws_vpc_security_group_ingress_rule.allow_all_traffic_ipv4: Creation complete after 0s [id=sgr-0f2c9c9c3c9bbfb31]
aws_vpc_security_group_egress_rule.allow_all_traffic_ipv6: Creation complete after 0s [id=sgr-066eb7d7a91198ae6]
aws_vpc_security_group_ingress_rule.allow_all_traffic_ipv6: Creation complete after 1s [id=sgr-03a75204087d488bf]
aws_vpc_security_group_egress_rule.allow_all_traffic_ipv4: Creation complete after 1s [id=sgr-050a28edbcafc501e]
aws_subnet.main: Still creating... [10s elapsed]
aws_subnet.main: Creation complete after 11s [id=subnet-09c86742a6fbe355b]
aws_route_table_association.main: Creating...
aws_instance.dev: Creating...
aws_route_table_association.main: Creation complete after 1s [id=rtbassoc-01fd7c6c8deae5189]
aws_instance.dev: Still creating... [10s elapsed]
aws_instance.dev: Creation complete after 14s [id=i-0afe55eb7577a7fc2]
aws_ec2_instance_state.dev: Creating...
aws_eip.dev: Creating...
aws_eip.dev: Creation complete after 2s [id=eipalloc-0bf226c8779a86b76]
aws_eip_association.eip_assoc: Creating...
coder_metadata.workspace_info[0]: Creating...
coder_metadata.workspace_info[0]: Creation complete after 0s [id=b74a5d2d-ead7-4d9d-bdc5-e5a6b509722e]
aws_eip_association.eip_assoc: Creation complete after 1s [id=eipassoc-0945951d48c66d90a]
aws_ec2_instance_state.dev: Still creating... [10s elapsed]
aws_ec2_instance_state.dev: Creation complete after 10s [id=i-0afe55eb7577a7fc2]
Apply complete! Resources: 19 added, 0 changed, 0 destroyed.
Outputs: 0

Output is:

Image

Then i remove aws_eip

# resource "aws_eip" "dev" {
#   instance = aws_instance.dev.id
#   domain   = "vpc"
# }
# resource "aws_eip_association" "eip_assoc" {
#   instance_id   = aws_instance.dev.id
#   allocation_id = aws_eip.dev.id
# }

Change coder metadata

  item {
    key   = "public ip"
    value = aws_instance.dev.public_ip
  }
  item {
    key   = "public dns"
    value = aws_instance.dev.public_dns
  }

Log:

Terraform 1.12.2
coder_agent.main: Plan to create
module.vscode-web[0].coder_app.vscode-web: Plan to create
module.vscode-web[0].coder_script.vscode-web: Plan to create
aws_key_pair.owner: Plan to create
aws_vpc.main: Plan to create
data.cloudinit_config.user_data: Plan to read
aws_internet_gateway.main: Plan to create
aws_subnet.main: Plan to create
aws_security_group.allow_all: Plan to create
aws_route_table.main: Plan to create
aws_route_table_association.main: Plan to create
aws_vpc_security_group_ingress_rule.allow_all_traffic_ipv6: Plan to create
aws_vpc_security_group_egress_rule.allow_all_traffic_ipv6: Plan to create
aws_vpc_security_group_ingress_rule.allow_all_traffic_ipv4: Plan to create
aws_vpc_security_group_egress_rule.allow_all_traffic_ipv4: Plan to create
aws_instance.dev: Plan to create
aws_ec2_instance_state.dev: Plan to create
coder_metadata.workspace_info[0]: Plan to create
coder_agent.main: Creating...
coder_agent.main: Creation complete after 0s [id=f488ed37-a6ea-47ba-a8a4-14e7040e51fb]
module.vscode-web[0].coder_app.vscode-web: Creating...
module.vscode-web[0].coder_app.vscode-web: Creation complete after 0s [id=46daf579-c62c-46fb-80aa-bf373969b209]
module.vscode-web[0].coder_script.vscode-web: Creating...
module.vscode-web[0].coder_script.vscode-web: Creation complete after 0s [id=536c3227-ebe7-4803-b965-5e14f053ed62]
aws_key_pair.owner: Creating...
aws_vpc.main: Creating...
aws_key_pair.owner: Creation complete after 1s [id=magenta-halibut-69]
data.cloudinit_config.user_data: Refreshing...
data.cloudinit_config.user_data: Refresh complete after 0s [id=873515121]
aws_vpc.main: Still creating... [10s elapsed]
aws_vpc.main: Creation complete after 12s [id=vpc-0501c39d417ae43d5]
aws_internet_gateway.main: Creating...
aws_security_group.allow_all: Creating...
aws_subnet.main: Creating...
aws_internet_gateway.main: Creation complete after 1s [id=igw-093cef8088979d687]
aws_route_table.main: Creating...
aws_route_table.main: Creation complete after 2s [id=rtb-097655b71e5574d70]
aws_security_group.allow_all: Creation complete after 3s [id=sg-0a0a2d95770e73890]
aws_vpc_security_group_egress_rule.allow_all_traffic_ipv4: Creating...
aws_vpc_security_group_egress_rule.allow_all_traffic_ipv6: Creating...
aws_vpc_security_group_ingress_rule.allow_all_traffic_ipv4: Creating...
aws_vpc_security_group_ingress_rule.allow_all_traffic_ipv6: Creating...
aws_vpc_security_group_ingress_rule.allow_all_traffic_ipv4: Creation complete after 0s [id=sgr-02eacedfba7d7bb4c]
aws_vpc_security_group_egress_rule.allow_all_traffic_ipv4: Creation complete after 0s [id=sgr-03872bbd4b721bc6b]
aws_vpc_security_group_egress_rule.allow_all_traffic_ipv6: Creation complete after 0s [id=sgr-0ddaefbd6ae55f302]
aws_vpc_security_group_ingress_rule.allow_all_traffic_ipv6: Creation complete after 0s [id=sgr-006d4b68a781d4c69]
aws_subnet.main: Still creating... [10s elapsed]
aws_subnet.main: Creation complete after 12s [id=subnet-03305999f8bbef5aa]
aws_route_table_association.main: Creating...
aws_instance.dev: Creating...
aws_route_table_association.main: Creation complete after 0s [id=rtbassoc-03e83c7177894ca9e]
aws_instance.dev: Still creating... [10s elapsed]
aws_instance.dev: Creation complete after 13s [id=i-06499f1df61f4e2bc]
aws_ec2_instance_state.dev: Creating...
coder_metadata.workspace_info[0]: Creating...
coder_metadata.workspace_info[0]: Creation complete after 0s [id=3038dd14-7081-4589-adb2-82cb12bc26c0]
aws_ec2_instance_state.dev: Still creating... [10s elapsed]
aws_ec2_instance_state.dev: Creation complete after 10s [id=i-06499f1df61f4e2bc]
Apply complete! Resources: 17 added, 0 changed, 0 destroyed.
Outputs: 0

Output: Image

The problem using aws_instance.dev.public_ip is output blank when restart workspace.

Log:

Terraform 1.12.2
module.vscode-web[0].coder_app.vscode-web: Plan to create
module.vscode-web[0].coder_script.vscode-web: Plan to create
coder_metadata.workspace_info[0]: Plan to create
aws_ec2_instance_state.dev: Plan to update
module.vscode-web[0].coder_script.vscode-web: Creating...
module.vscode-web[0].coder_app.vscode-web: Creating...
module.vscode-web[0].coder_app.vscode-web: Creation complete after 0s [id=531d48ca-40db-4b52-9a3a-9286cca9c755]
module.vscode-web[0].coder_script.vscode-web: Creation complete after 0s [id=862f4e51-c611-4065-ac75-ad4671d4fb92]
coder_metadata.workspace_info[0]: Creating...
aws_ec2_instance_state.dev: Modifying... [id=i-06499f1df61f4e2bc]
coder_metadata.workspace_info[0]: Creation complete after 0s [id=706b61ea-ad0a-4991-8a37-903c577ea4f5]
aws_ec2_instance_state.dev: Still modifying... [10s elapsed]
aws_ec2_instance_state.dev: Still modifying... [20s elapsed]
aws_ec2_instance_state.dev: Modifications complete after 22s [id=i-06499f1df61f4e2bc]
Apply complete! Resources: 3 added, 1 changed, 0 destroyed.
Outputs: 0
Image

Edit:

I was ask gpt and got this solution.

To always show the current public IP (even after restart), use a data source to fetch the instance details dynamically.

Add this data source:

data "aws_instance" "dev" {
  instance_id = aws_instance.dev.id
}

Then update your coder_metadata to use the data source:

item {
  key   = "public ip"
  value = data.aws_instance.dev.public_ip
}
item {
  key   = "public dns"
  value = data.aws_instance.dev.public_dns
}

This way, the metadata will always reflect the current public IP and DNS, even after restarts.

anasfanani avatar Aug 20 '25 21:08 anasfanani