【问题标题】:Using Jenkins and Terraform EC2 auto deploy issues使用 Jenkins 和 Terraform EC2 自动部署问题
【发布时间】:2021-11-02 18:28:31
【问题描述】:

我是 DevOps 的新手。我正在尝试使用以下脚本部署 EC2 实例。它在 Terraform 初始化阶段失败。我的代码中确实有 terraform init。问题出在哪里?

pipeline {

    parameters {
        string(name: 'environment', defaultValue: 'terraform', description: 'Workspace/environment file to use for deployment')
        booleanParam(name: 'autoApprove', defaultValue: false, description: 'Automatically run apply after generating plan?')

    }


     environment {
        AWS_ACCESS_KEY_ID     = credentials('AWS_ACCESS_KEY_ID')
        AWS_SECRET_ACCESS_KEY = credentials('AWS_SECRET_ACCESS_KEY')
    }

   agent  any
        options {
                timestamps ()
                        }
    stages {
        stage('checkout') {
            steps {
                 script{
                        dir("terraform")
                        {
                            git "https://github.com/Ravinderhub/Jenkins-Terraform-AWS.git"
                        }
                    }
                }
            }

        stage('Plan') {
            steps {
                sh 'pwd;cd terraform/aws-instance-first-script ; terraform init -input=false'
                sh 'pwd;cd terraform/aws-instance-first-script ; terraform workspace new ${environment}'
                sh 'pwd;cd terraform/aws-instance-first-script ; terraform workspace select ${environment}'
                sh "pwd;cd terraform/aws-instance-first-script ;terraform plan -input=false -out tfplan "
                sh 'pwd;cd terraform/aws-instance-first-script ;terraform show -no-color tfplan > tfplan.txt'
            }
        }
        stage('Approval') {
           when {
               not {
                   equals expected: true, actual: params.autoApprove
               }
           }

           steps {
               script {
                    def plan = readFile 'terraform/aws-instance-first-script/tfplan.txt'
                    input message: "Do you want to apply the plan?",
                    parameters: [text(name: 'Plan', description: 'Please review the plan', defaultValue: plan)]
               }
           }
       }

        stage('Apply') {
            steps {
                sh "pwd;cd terraform/aws-instance-first-script ; terraform apply -input=false tfplan"
            }
        }
    }

  }

【问题讨论】:

  • 得到以下错误,我认为问题出在下面的脚本行需要修改这里是错误>>>Terraform配置必须在初始化之前有效,以便Terraform可以确定哪些模块和需要安装提供程序。这是脚本行>>> sh 'pwd;cd terraform/aws-instance-first-script ; terraform init -input=false'

标签: amazon-web-services jenkins automation terraform


【解决方案1】:

配置中存在阻止初始化的无效变量。

ami_id变量defines an invalid type

更正为:

variable "ami_id" {
  type = "map"

  default = {
    us-east-1    = "ami-035b3c7efe6d061d5"
    eu-west-2    = "ami-132b3c7efe6sdfdsfd"
    eu-central-1 = "ami-9787h5h6nsn"
  }
}

【讨论】:

  • 我的代码中有上述内容,我收到错误并将其更改为 map1。还有其他建议吗?
  • 类型应该是“map”而不是“map1”。
  • 是的,我知道.. 我已经更改了它,但我仍然遇到同样的错误。我放 1 以查看错误是否消失的原因。有 1 或没有我仍然得到同样的错误。这就是我想说的!!
  • 您是否拥有工作流配置文件中的存储库 https://github.com/Ravinderhub/Jenkins-Terraform-AWS.git 并在其中应用了更改?
  • 是的,我拥有存储库!是的,已对 repo 进行了更改并已提交。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
  • 2021-09-20
  • 1970-01-01
相关资源
最近更新 更多