terraform将文件复制/上传到AWS EC2实例 [英] terraform copy/upload files to aws ec2 instance

查看:214
本文介绍了terraform将文件复制/上传到AWS EC2实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有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.

我们尝试了

  1. 文件供应商: 但它不能正常工作,并且阅读此选项不适用于所有terraform版本
  1. file provisioner : but its not wokring , and read this option does not work with all terraform version

      provisioner "file" {
        source      = "abc.sh"
        destination = "/home/ec2-user/basic2.sh"
      }

  1. 尝试数据模板文件选项

    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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆