Terraform Tutorial
Terraform User_Data
user_data
The user_data only runs at instance launch time.
Here is a sample of using user_data embedded into tf file:
resource "aws_instance" "user_data_example" { ami = lookup(var.ami_id, var.region) instance_type = var.instance_type # subnet_id = aws_subnet.public_1.id
# Security group assign to instance vpc_security_group_ids = [aws_security_group.allow_ssh.id]
# key name key_name = var.key_name
user_data = <<EOF #! /bin/bash sudo yum update -y sudo yum install -y httpd.x86_64 sudo service httpd start sudo service httpd enable echo "<h1>Welcome to Httpd Server</h1>" |
sudo tee /var/www/html/index.html EOF
tags = { Name = "Ec2-User-data" } }
|
But we prefer to use a file() function:
resource "aws_instance" "user_data_example_input_file" { ami = lookup(var.ami_id, var.region) instance_type = var.instance_type # subnet_id = aws_subnet.public_1.id
# Security group assign to instance vpc_security_group_ids = [aws_security_group.allow_ssh.id]
# key name key_name = var.key_name user_data = "${file("apache_config.sh")}"
tags = { Name = "Ec2-User-data-with-file" } }
|
The apache_config.sh looks like this:
#! /bin/bash sudo yum update -y sudo yum install -y httpd.x86_64 sudo yum service httpd start sudo yum service httpd enable echo "<h1>Welcome to Httpd Server</h1>" | yum tee
/var/www/html/index.html
|
Run Terraform Apply and see the Public IP in browser to see the above message
Great Article. I found so many interesting stuff in your blog especially its discussionvisit site
ReplyDelete