Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (5.6k points)

Below is my code: 

data "template_file" "user_data" {

  template = file("${path.module}/userdata.sh")

}

resource "aws_instance" "bespin-ec2-web-a" {

  ami = "ami-0bea7fd38fabe821a"

  instance_type = "t2.micro"

  vpc_security_group_ids = [aws_security_group.bespin-sg.id]

  subnet_id = aws_subnet.bespin-subnet-public-a.id

  associate_public_ip_address = true

  tags = {

    Name = "bespin-ec2-web-a"

  }

  user_data = data.template_file.user_data.rendered

}

Now I want to upload user data to S3 and will use it by calling URL, So can I do so?

1 Answer

0 votes
by (12.4k points)

Here you need to specify user data content to terraform resource "aws_instance".

Here is the user data:

resource "aws_instance" "this" {

  ami = "${local.ami_this_id}"

  instance_type = "${var.instance_type}"

  subnet_id = "${var.subnet_id}"

  vpc_security_group_ids = "${var.security_group_ids}"

  key_name = "${aws_key_pair.this.key_name}"

  iam_instance_profile = "${var.ec2_instance_profile_id}"

  user_data = data.template_file.user_data.rendered   # <----- Specify userdata content

  root_block_device {

    volume_type = "${var.root_volume_type}"

    volume_size = "${var.root_volume_size}"

    delete_on_termination = true

  }

}

then for S3 upload:

resource "local_file" "userdata_sh" {

  content  = data.template_file.user_data.rendered

  filename = "your_local_userdata_sh_path"

}

resource "aws_s3_bucket_object" "object" {

  bucket = "your_s3_bucket_name"

  key    = "userdata.sh"

  source = "your_local_userdata_sh_path"

  etag = "${filemd5("your_local_userdata_sh_path")}"

}

Interested to know about AWS? Come & join AWS Training provided by Intellipaat.

Related questions

+4 votes
1 answer

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
+7 votes
1 answer

Browse Categories

...