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.