如何在执行之前从python创建terraform backend.tf文件以消除插值状态文件问题

如何在执行之前从python创建terraform backend.tf文件以消除插值状态文件问题,python,terraform,terraform-provider-aws,Python,Terraform,Terraform Provider Aws,实际上,我们从那里构建webapp,我们通过 如下 terraform apply -input=false -auto-approve -var ami="%ami%" -var region="%region%" -var icount="%count%" -var type="%instance_type%" terraform { backend “s3” { bucket = “terraform-007” key = “key”

实际上,我们从那里构建webapp,我们通过

如下

terraform apply -input=false -auto-approve -var ami="%ami%" -var region="%region%" -var icount="%count%" -var type="%instance_type%"
terraform {
    backend “s3” {
        bucket = “terraform-007”
        key = “key”
        region = “ap-south-1”
        profile=“venu”
    }
} 

provider “aws” {
    profile = “ var.awsprofile"
    region="{var.aws_region}”
}

resource “aws_instance” “VM” {
    count = var.icount
    ami = var.ami
    instance_type = var.type
    tags = {
        Environment = “${var.env_indicator}”
    }
}
实际上,这里的问题是后端不支持变量,我需要将这些值也传递给应用程序

为了解决这个问题,我找到了一些解决方案,比如我们需要在执行之前创建backend.tf

但是我不知道怎么做,如果有人对此有任何疑问,请帮助我

提前谢谢

我需要使用以下变量从python创建backend.tf文件

并且需要替换key=“${profile}/tfstate”

对于每个外形,外形都需要更换

我正在考虑使用git-repo,通过使用git我们创建文件并提取值,然后再次提交并执行

请帮我举一些例子和想法

代码如下所示:

我的主要任务如下

terraform apply -input=false -auto-approve -var ami="%ami%" -var region="%region%" -var icount="%count%" -var type="%instance_type%"
terraform {
    backend “s3” {
        bucket = “terraform-007”
        key = “key”
        region = “ap-south-1”
        profile=“venu”
    }
} 

provider “aws” {
    profile = “ var.awsprofile"
    region="{var.aws_region}”
}

resource “aws_instance” “VM” {
    count = var.icount
    ami = var.ami
    instance_type = var.type
    tags = {
        Environment = “${var.env_indicator}”
    }
}
vars.tf-like

variable “aws_profile” {
    default = “default”
    description = “AWS profile name, as set in ~/.aws/credentials”
}

variable “aws_region” {
    type = “string”
    default = “ap-south-1”
    description = “AWS region in which to create resources”
}

variable “env_indicator” {
    type = “string”
    default = “dev”
    description = “What environment are we in?”
}

variable “icount” {
    default = 1
}

variable “ami” {
    default =“ami-54d2a63b”
}

variable “bucket” {
    default=“terraform-002”
}

variable “type” {
    default=“t2.micro”
}
output.tf-like:

output “ec2_public_ip” {    
    value = ["${aws_instance.VM.*.public_ip}"]    
}

output “ec2_private_ip” {
    value = ["${aws_instance.VM.*.private_ip}"]
}
实际上,这里的问题是后端不支持变量,我需要将这些值也传递给应用程序

为了解决这个问题,我找到了一些解决方案,比如我们需要在执行之前创建backend.tf

但是我不知道怎么做,如果有人对此有任何疑问,请帮助我


提前感谢。

因为后端的配置不能使用插值,所以我们使用了按约定配置的方法

  • 我们所有状态集合(微服务和其他基础设施)的terraform使用相同的S3存储桶进行状态存储,使用相同的DynamoDB表进行锁定

  • 在执行terraform时,我们使用相同的IAM角色(仅限terraform的专用用户)

  • 我们通过约定为状态定义密钥,这样就不需要生成密钥

    key = "platform/services/{name-of-service}/terraform.tfstate"
    
  • 我将避免在部署基础架构代码时导致更改的过程,以确保工程师能够最大限度地理解/维护代码

    编辑:添加关键示例

    对于用户服务:

    key = "platform/services/users/terraform.tfstate"
    
    搜索服务:

    key = "platform/services/search/terraform.tfstate"
    
    对于产品服务:

    key = "platform/services/products/terraform.tfstate"
    

    感谢您的评论。key=“platform/services/{name of service}/terraform.tfstate”是{name of service is static或dynamic}这里我们有一个单独的terraform文件用于部署每个服务,因此{name of service}将特定于该文件正在部署的服务。