【问题标题】:terraform to generate a file, zip and upload to s3terraform 生成文件,压缩并上传到 s3
【发布时间】:2021-03-26 15:05:17
【问题描述】:

我想动态生成一个文件,将其压缩并使用 terraform 将其上传到 s3。以下是我目前所拥有的。

#Setup a local variable with docker container information
locals {
  file_content = jsonencode({
    "AWSEBDockerrunVersion" : "1",
    "Image" : {
      "Name" : "${var.aws_account_id}.dkr.ecr.${var.aws_region}.amazonaws.com/vin-${var.application}-api:${var.environment}",
      "Update" : "true"
    },
    "Ports" : {
      "ContainerPort" : 80,
      "HostPort" : 80
    }
  })
}

#Generate the Dockerrun.aws.json with the container information and environment tag
resource "local_file" "docker_container_info" {
  content  = local.file_content
  filename = "./${var.service_name}-${var.environment}-Dockerrun.aws.json"
}

#Zip the file
data "archive_file" "source" {
  type        = "zip"
  source_dir  = "./${var.service_name}-${var.environment}-Dockerrun.aws.json"
  output_path = "./${var.service_name}-${var.environment}-Dockerrun.aws.json.zip"

  depends_on = [
    local_file.docker_container_info
  ]
}

/*
#Upload the zip file to s3 bucket under DockerRunFiles folder
resource "aws_s3_bucket_object" "file_upload" {
  bucket           = var.run_file_bucket
  key              = "${var.bucket_name}/${var.service_name}-${var.environment}-Dockerrun.aws.json.zip"
  source           = "${data.archive_file.source.output_path}"
}
*/


variable "aws_region" {
  default = "us-east-1"
}

variable "environment" {
  default = "lab"
}

variable "aws_environment" {
  default = "lab"
}

variable "service_name" {
  default = "service_name"
}

variable "application" {
  default = "application_name"
}

variable "aws_account_id" {
  default = "1234567890"
}

variable "bucket_name" {
  default = "my_bucket"
}

我看到service_name-lab-Dockerrun.aws.json 文件正确生成,其中包含 json。但我得到的错误是Error: error archiving directory: could not archive directory that is a file: ./service_name-lab-Dockerrun.aws.json。 terraform data archive_file 是否只能压缩文件夹而不能压缩 json 文件?感谢帮助

版本详情:

Terraform v0.12.28
+ provider.archive v2.0.0
+ provider.local v2.0.0

【问题讨论】:

    标签: amazon-web-services terraform terraform0.12+


    【解决方案1】:

    您的"./${var.service_name}-${var.environment}-Dockerrun.aws.json" 是一个文件,而不是一个目录。因此,您应该使用source_file 而不是source_dir,如terraform docs 中所示。

    #Zip the file
    data "archive_file" "source" {
      type        = "zip"
      source_file  = "./${var.service_name}-${var.environment}-Dockerrun.aws.json"
      output_path = "./${var.service_name}-${var.environment}-Dockerrun.aws.json.zip"
    
      depends_on = [
        local_file.docker_container_info
      ]
    }
    

    【讨论】:

    • 天哪!谢谢。这是我的经理经常对我说的话,换一种眼光可能会发现问题..
    猜你喜欢
    • 2014-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    相关资源
    最近更新 更多