1

I have an ECS service that spawns some containers:

resource "aws_ecs_service" "rocketchat" {
  name            = "rocketchat-ecs-service"
  cluster         = "${aws_ecs_cluster.rocketchat.id}"
  task_definition = "${aws_ecs_task_definition.rocketchat.arn}"
  desired_count   = 2
  iam_role        = "${aws_iam_role.ecs_service.name}"
...

But the EC2 instances it creates have no 'Name' tag. I thought that name_prefix was what I was looking for, but it doesn't work:

resource "aws_launch_configuration" "rocketchat" {
  security_groups             = ["${aws_security_group.instance_sg.id}"]
  name_prefix                 = "${var.project_prefix}-rocketchat-"
  key_name                    = "${aws_key_pair.circles_rocketchat.key_name}"
...

The task definition for this container also has a name field, which doesn't work either:

[
  {
    "name": "rocketchat",
    "cpu": 256,
    "essential": true,
...

Amazon docs say here that 'tagging on creation' is not supported 'Launch template' but I am not sure that is exactly relevant; esp when you take my other ECS service on the same cluster, which doesn't have a load balancer, and therefore has no launch configuration.

EDIT:

@B.Miller s suggestion below has not tagged the actual instances but it does show in the console under EC2 > Auto Scaling Groups > Tags

+-------------+---------------------+-------------------+ | Key | Value | Tag New Instances | +-------------+---------------------+-------------------+ | Environment | dev | Yes | +-------------+---------------------+-------------------+ | Name | rocketchat-instance | Yes | +-------------+---------------------+-------------------+

edzillion
  • 139
  • 1
  • 9

1 Answers1

3

name_prefix creates a randomly unique name for the launch configuration using the prefix, not prefixs the things launched with it. name in a task definition is the name of the task definition itself. Because you are using a launch configuration I assume you are using an autoscaling group. Autoscaling groups allow you to set tags then propagate those tags.

For example you can specify individual tags and have them propogate:

resource "aws_autoscaling_group" "default" {
  ...
  tags = [
    {
      key                 = "explicit1"
      value               = "value1"
      propagate_at_launch = true
    },
    {
      key                 = "explicit2"
      value               = "value2"
      propagate_at_launch = true
    },
  ]
  ...
}

Edit: note that this works with the ECS-Optimised Amazon AMI but not CoreOS.

edzillion
  • 139
  • 1
  • 9
B. Miller
  • 667
  • 3
  • 9
  • That didn't work for me; no errors in terraform but the instances are still not tagged. – edzillion Jun 14 '18 at 15:09
  • Console actually shows the tags are set to propagate as well? @edzillion – B. Miller Jun 14 '18 at 15:23
  • It does, but I am having some weird issue now with my ASG. It's being ingored. Maybe that's the underlying problem – edzillion Jun 14 '18 at 16:46
  • actually I think it might just be an issue with the aws console. I can see my ASG in one view but it states I don't have one when I view the cluster. Still no Name tags though. – edzillion Jun 14 '18 at 20:37
  • Odd, you'd think Terraform would throw an error if it didn't exist. In any case, this is how the tagging works. Not sure why it's not working though. – B. Miller Jun 14 '18 at 20:42
  • hey @b.miller I realised it wasn't working because I was using CoreOS and this feature requires the Amazon ECS-optimised AMI. I have edited your answer just to point that out, and accepted it. I hope you don't mind! – edzillion Jun 17 '18 at 17:32