TolMera
TolMera

Reputation: 416

Define: Terraform - AWS - aws_instance - user_data

I'm working with Terraform, launching an ECS cluster.

I'm using a script that was written by someone else. I understand most of the launch configuration script, other than this one point:

I'm trying to find the link between the autoscaling group and the ECS cluster.

I have an aws_autoscaling_group which uses an aws_launch_configuration (see below). The aws_launch_configuration has a user_data parameter. This is the only link between the autoscaling group and the cluster that I can find.

Looking at the links (bottom) to the documentation, it's not giving a very good explanation of 'what' user_data is. Can someone please help me understand "how" the user_data links the autoscaling group with the cluster, and if at all possible give some example or link to what it's normally used for and how it's normally used.

...
resource "aws_launch_configuration" "ecs_host" {
  ...

  user_data = <<EOF
#!/bin/bash
echo ECS_CLUSTER=${var.cluster_name} >> /etc/ecs/ecs.config
EOF
}
...

terraform apply outputs:

  ...
  + aws_launch_configuration.ecs_host
      ...
      user_data: "4e27e8feea0896af43ada0c647877da3766f5dcb"
  ...

https://www.terraform.io/docs/providers/aws/r/instance.html#user_data https://www.terraform.io/docs/providers/aws/r/launch_configuration.html#user_data

Upvotes: 4

Views: 9723

Answers (3)

BMW
BMW

Reputation: 45273

official explanation by terraform

user_data - (Optional) The user data to provide when launching the instance. Do not pass gzip-compressed data via this argument; see user_data_base64 instead.

Offical aws document to explain what's user_data

https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-instance-metadata.html

So in general, after you define the launch configuration and autoscaling group, the autoscaling group will make sure how many ec2 instances need be started (desired capacity).

When start a new instance, it will reference the launch configuration, which define which AWS AMI for the instance and what init command will be run. the init commands are saved in user_data script.

In your case, the user data script registers itself to nominated ECS cluster. So ecs cluster can discover these ec2 instances easily

echo ECS_CLUSTER=${var.cluster_name} >> /etc/ecs/ecs.config

In background, there is a docker container called amazon/amazon-ecs-agent running to help these registions and manage other containers (ecs services)

If you want to know the details, you can go through this document: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-install.html

Upvotes: 2

Claire Bellivier
Claire Bellivier

Reputation: 189

user_data attribute allows you to specify script payload or cloud daemon instructions. Data can be inline template as your case with the script, or file template where you will need to deal with the Template Provider. So when you want to create a new launch configuration, used for autoscaling groups this is one of the option to execute here a script, and got the output as the cluster's hash for instance.

The limitations are you cannot pass gzip-compressed data via this argument; see user_data_base64 instead and you should not store sensitive data, such as passwords, as user data.

Upvotes: 1

Master_Yoda
Master_Yoda

Reputation: 1132

Simply put user data is a script that is run upon bootstrapping an ec2 instance. You can automate actions that need to take place to ensure your ec2 is configured correctly, and specify them as user_data, instructing aws to run the script on machine bootstrap.

Upvotes: 2

Related Questions