Terraform版本= 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
}
我想将user_data上传到S3并通过调用URL来使用它。我该怎么办?
ex)
resource "template_file" "userdata_sh" {
template = "https://test.s3.ap-northeast-2.amazonaws.com/userdata.sh"
}
[不是100%明确要实现的目标,但是,如果为要使用的EC2实例指定用户数据,则无法在S3中使用sh文件。
需要直接将用户数据内容指定给aws_instance terraform资源。
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
}
}
[如果要上载到S3并将其复制到EC2实例中并作为Shell脚本运行,则无需上载到S3,然后使用AWS CLI S3命令将其复制到EC2实例中,或者使用例如S3保险丝。
首先,使用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"
}
然后使用https://www.terraform.io/docs/providers/aws/r/s3_bucket_object.html上传到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")}"
}
将不可能。模板文件必须位于本地计算机中。如果共享userdata.sh是目标,请考虑使用例如在计算机上安装S3。 S3保险丝。