terraform 将文件复制/上传到 aws ec2 实例 [英] terraform copy/upload files to aws ec2 instance
问题描述
我们有 cronjob 和 shell 脚本,我们希望在使用 terraform 创建实例时将其复制或上传到 aws ec2 实例.
We have cronjob and shell script which we want to copy or upload to aws ec2 instance while creating instance using terraform.
我们尝试过
- 文件配置器:但它不工作,并且阅读此选项不适用于所有 terraform 版本
provisioner "file" {
source = "abc.sh"
destination = "/home/ec2-user/basic2.sh"
}
- 尝试过数据模板文件选项
data "template_file" "userdata_line" {
template = <<EOF
#!/bin/bash
mkdir /home/ec2-user/files2
cd /home/ec2-user/files2
sudo touch basic2.sh
sudo chmod 777 basic2.sh
base64 basic.sh |base64 -d >basic2.sh
EOF
}
尝试了所有选项,但都没有工作.
你能帮忙或建议.
我是 terraform 的新手,所以长期以来一直在努力解决这个问题.
tried all option but none of them working.
could u please help or advise .
I am new to terraform so struggling on this from long time.
推荐答案
不知何故,在公司领域,没有一个选项起作用.但最终我们能够使用 s3 存储桶复制/下载文件.
somehow in corporate domain none of the options worked. but finally we were able to copy /download files using s3 bucket.
创建 s3.tf 以上传此文件 basic2.sh
create s3.tf to upload this files basic2.sh
resource "aws_s3_bucket" "demo-s3" {
bucket = "acom-demo-s3i-<bucketID>-us-east-1"
acl = "private"
tags {
Name = "acom-demo-s3i-<bucketID>-us-east-1"
StackId = "demo-s3"
}
}
resource "aws_s3_bucket_policy" "s3_policy" {
bucket = "${aws_s3_bucket.demo-s3.id}"
policy = <<EOF
{
"Version": "2009-10-17",
"Statement": [
{
"Sid": "Only allow specific role",
"Effect": "allow",
"Principal":{ "AWS": ["arn:aws:iam::<bucketID>:role/demo-s3i"]},
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::acom-demo-s3i-<bucketID>-us-east-1",
"arn:aws:s3:::acom-demo-s3i-<bucketID>-us-east-1/*"
]
}
]
}
EOF
}
resource "aws_s3_bucket_object" "object" {
bucket = "acom-demo-s3i-<bucketID>-us-east-1"
key = "scripts/basic2.sh"
source = "scripts/basic2.sh"
etag = "${filemd5("scripts/basic2.sh")}"
}
然后在其他 tpl 文件中声明文件下载部分.
and then declared file download portion in other tpl file.
aws s3 cp s3://acom-demo-s3i-<bucketID>-us-east-1/scripts/basic2.sh /home/ec2-user/basic2.sh
这篇关于terraform 将文件复制/上传到 aws ec2 实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!