0
votes

Terraform Version = 0.12

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
}

I want to upload user_data to S3 and use it by calling URL. What can I do?

ex)

resource "template_file" "userdata_sh" {
  template = "https://test.s3.ap-northeast-2.amazonaws.com/userdata.sh"
}
1
Not clear what you want to achieve. Do you want to generate a file which contains the rendered userdata.sh and upload it to S3? or Do you want to reference a template file in S3 from terraform script? Or do you simply need to know how to upload a file to S3 using Terraform?mon
Upload the "userdata.sh" file to "S3". And I want to run the script by calling URL from "terraform" of uploaded "userdata.sh" file.sangyul lee
Not clear " I want to run the script by calling URL from "terraform". Please be clear with "where" by "who".mon
What is "calling URL"? URL cannot be called or executed directly Do you mean downloading the file pointed by the URL and then running it? Please be specific with what you want to achieve.mon
I would like to upload the local "user_data" file to "S3" and use it.sangyul lee

1 Answers

1
votes

Not 100% clear what is to be achieved, however, if to specify userdata for EC2 instances to use, then use a sh file in S3 would not be possible.

Need to specify userdata content directly to aws_instance terraform resource.

EC2/userdata

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
  }
}

If it is to upload to S3 and copy it into EC2 instance and run it as a shell script, then would not need to upload to S3 then copy it into EC2 instances with AWS CLI S3 commands or mounting the S3 bucket inside EC2 using e.g. S3 Fuse.

S3 upload

First, use https://www.terraform.io/docs/providers/local/r/file.html

resource "local_file" "userdata_sh" {
  content  = data.template_file.user_data.rendered
  filename = "your_local_userdata_sh_path"
}

Then use https://www.terraform.io/docs/providers/aws/r/s3_bucket_object.html to upload to S3.

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")}"
}

URL in template resource

Will not be possible. Template file needs to be in your local machine. If sharing the userdata.sh is the goal, then consider mounting S3 in your machine using e.g. S3 Fuse.