Passing User_data to EC2 Instance

Hey,

Below is the sample code I got and trying to pass user_data to the EC2 instance, but facing issues. Is there anyway to get help on this?

resource "aws_instance" "ec2" {

  ami           = "${var.instance_ami}"

  instance_type = "${var.instance_type}"

  key_name = "${var.key_name}"

  subnet_id = "${var.public_subnet_id_1_mod}"

  vpc_security_group_ids       = [ "${var.vpc_security_group_public_id}" ]

  user_data = << EOF

    #!/bin/bash

    yum update -y

    yum install httpd -y

    service httpd start

    chkconfig httpd on

    cd /var/www/html

    echo "<html><h1>Hello Cloud Gurus Welcome To My Webpage</h1></html>" >

    index.html

  EOF

  tags = {

    "${var.tag_key_1}" = "${var.tag_value_1}"

  }

}

Below error messages keep getting

S C:\Users\12016\Documents\test\tf\tf-cloud-infra-coe\environments\intuit> terraform init
Initializing modules...

Error: Invalid expression

On ..\..\ec2\main.tf line 9: Expected the start of an expression, but found an
invalid expression token.


Error: Invalid block definition

On ..\..\ec2\main.tf line 13: A block definition must have block content
delimited by "{" and "}", starting on the same line as the block header.


Error: Invalid block definition

On ..\..\ec2\main.tf line 14: A block definition must have block content
delimited by "{" and "}", starting on the same line as the block header.


Error: Argument or block definition required

On ..\..\ec2\main.tf line 15: An argument or block definition is required
here. To set an argument, use the equals sign "=" to introduce the argument
value.


Error: Argument or block definition required

On ..\..\ec2\main.tf line 17: An argument or block definition is required
here. To set an argument, use the equals sign "=" to introduce the argument
value.


Error: Argument or block definition required

On ..\..\ec2\main.tf line 18: An argument or block definition is required
here. To set an argument, use the equals sign "=" to introduce the argument
value.


Error: Invalid expression

On ..\..\ec2\main.tf line 9: Expected the start of an expression, but found an
invalid expression token.


Error: Invalid block definition

On ..\..\ec2\main.tf line 13: A block definition must have block content
delimited by "{" and "}", starting on the same line as the block header.


Error: Invalid block definition

On ..\..\ec2\main.tf line 14: A block definition must have block content
delimited by "{" and "}", starting on the same line as the block header.


Error: Argument or block definition required

On ..\..\ec2\main.tf line 15: An argument or block definition is required
here. To set an argument, use the equals sign "=" to introduce the argument
value.


Error: Argument or block definition required

On ..\..\ec2\main.tf line 17: An argument or block definition is required
here. To set an argument, use the equals sign "=" to introduce the argument
value.


Error: Argument or block definition required

On ..\..\ec2\main.tf line 18: An argument or block definition is required
here. To set an argument, use the equals sign "=" to introduce the argument
value.


Error: Invalid expression

On ..\..\ec2\main.tf line 9: Expected the start of an expression, but found an
invalid expression token.

Its bit urgent and and greatly appreciated if some one could help at the earliest and thank you so much for your great favor in advance,

Regards,
Latha

It’s difficult to know what’s happening here, because your message isn’t quite formatted correctly, so I can’t read your configuration. Could you please edit your message to use preformatted text blocks using the text editor? Look for the <> icon in the toolbar.

User_data is creating problems over here. Lines 9-18 creating problems and terraform is NOT identifying it as a String.

Does this help?

Thank you,
Latha

I think I see the problem now. There should be no space after the << symbol: << EOF is invalid, and <<EOF is correct.

Note also that your shell script has leading spaces on each line, which I think will make it invalid. You may want to either use the <<-EOF indented version, or keep your shell script in a separate file and use the file function.

resource "aws_instance" "ec2" {
  ami           = "…"
  instance_type = "…"
  user_data = file("userdata.sh")
}

See more documentation on the “heredoc” string expression type here. Hope this helps!

Hey,

Thank you so much for your help and working well and I see the User_data attached to web servers, where as the expectation is services needs to be installed and sample website should be up and running right? but its NOT happening. When I try go /var/www, its telling no such file or directory. Could you please help me do I have to make any additional steps in my resource creation?

image

image

Thank you so much again and have a wonderful day ahead!

All Security Groups, Subnets and VPC setup is good and when I perform these steps manually on this instance, able to launch the web page properly

Regards,
Bala

I’m not sure what the problem is here, but it looks like something to do with your AWS instance configuration, rather than Terraform.

I notice from your screenshots that your user-data script does not have yum install httpd -y or chkconfig httpd on commands. Maybe those will create the /var/www/html directory.

According to the AWS docs, you can debug your user-data scripts by reading the /var/log/cloud-init-output.log file. Hopefully there’s something there that will clear it up!

(I’ve used my moderator powers to tweak the formatting of the original post here so the code examples are readable. Although I see the same code example is in a later comment as a screenshot, formatted text will hopefully make this question more likely to appear when others use search to find help with similar problems.)

Hey,

its working well and nothing wrong in the code.

When I re-ran, worked well.

Than you,
Latha