terraform 生成文件,压缩并上传到 s3
terraform to generate a file, zip and upload to s3
我想动态生成一个文件,将其压缩并使用 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
您的 "./${var.service_name}-${var.environment}-Dockerrun.aws.json"
是文件,不是目录。因此,您应该使用 source_file
而不是 source_dir
,如地形 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
]
}
我想动态生成一个文件,将其压缩并使用 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
您的 "./${var.service_name}-${var.environment}-Dockerrun.aws.json"
是文件,不是目录。因此,您应该使用 source_file
而不是 source_dir
,如地形 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
]
}