3
respostas

Erro ao executar terraform apply na prod

Ao executar o terraform apply dentro da pasta de produção recebo o seguinte erro

Error: Invalid function argument │ │ on ../../infra/main.tf line 29, in resource "aws_key_pair" "chaveSSH": │ 29: public_key = file("${var.chave}.pub") │ ├──────────────── │ │ while calling file(path) │ │ var.chave is "IaC-Prod" │ │ Invalid value for "path" parameter: no file exists at "IaC-Prod.pub"; this function works only with files that are distributed as part of the configuration source code, so if this file will be created by a resource in this │ configuration you must instead obtain this result from an attribute of that resource.

3 respostas

Bom dia, Heitor. Tudo bem?

Pode compartilhar seu projeto pelo github para que possamos ter contexto do código? Ou compartilhar o código aqui mesmo usando o botão de inserir bloco de código acima do editor de texto </>.

Bom dia Vitor!

Vou compartilhar os códigos por aqui, o projeto no github não atualizei totalmente ainda, segue.

infra/main

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }

  required_version = ">= 1.2.0"
}

provider "aws" {
  region  = var.regiao_aws
}

resource "aws_instance" "app_server" {
  ami           = "ami-0735c191cf914754d"
  instance_type = var.instancia
  key_name = var.chave
  tags = {
    Name = "Terraform Ansible Python"
  }
  security_groups = ["aws_security_group.acesso_geral_${var.ambiente}"]
  vpc_security_group_ids = ["aws_security_group.acesso_geral_${var.ambiente}.id"]
}

resource "aws_key_pair" "chaveSSH" {
  key_name = var.chave
  public_key = file("${var.chave}.pub")
}

output "IP_publico" {
  value = aws_instance.app_server.public_ip
}

env/dev/main

module "aws-dev" {
    source = "../../infra"
    instancia = "t2.micro"
    regiao_aws = "us-west-2"
    chave = "IaC-DEV"
    ambiente = "DEV"
}

output "IP" {
  value = module.aws-dev.IP_publico
}

env/prod/main

module "aws-prod" {
    source = "../../infra"
    instancia = "t2.micro"
    regiao_aws = "us-west-2"
    chave = "IaC-Prod"
    ambiente = "PROD"
}

output "IP" {
  value = module.aws-prod.IP_publico
}

Os nomes das chaves estão corretos de acordo com como criei elas. O erro acontece somente em prod, em dev não ocorre. Já tentei recriar as chaves de prod porém o erro persistiu.

Atualização:

Atualizei o Terraform para a versão mais recente, mas nada mudou.

Ao passar o caminho absoluto do arquivo funciona, porém não fica viável:

public_key = file("/home/heitor/IaC/env/PROD/Iac-Prod.pub")

Alterei o código acima usando as variáveis criadas e o erro retornou

public_key = file("/home/heitor/IaC/env/${var.ambiente}/${var.chave}.pub")
Error: Invalid function argument
│ 
│   on ../../infra/main.tf line 37, in resource "aws_key_pair" "chaveSSH":
│   37:   public_key = file("/home/heitor/IaC/env/${var.ambiente}/${var.chave}.pub")
│     ├────────────────
│     │ while calling file(path)
│     │ var.ambiente is "PROD"
│     │ var.chave is "IaC-Prod"
│ 
│ Invalid value for "path" parameter: no file exists at "/home/heitor/IaC/env/PROD/IaC-Prod.pub";
│ this function works only with files that are distributed as part of the configuration source code,
│ so if this file will be created by a resource in this configuration you must instead obtain this
│ result from an attribute of that resource.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software