【问题标题】:How to Configure Terraform AWS Provider?如何配置 Terraform AWS 提供商?
【发布时间】:2023-01-19 03:59:54
【问题描述】:

我正在尝试创建 Terraform 文档中提到的 EC2 实例。


   terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}
provider "aws" {
  access_key = "Acxxxxxxxxxxxxxxxxx"
  secret_key = "UxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxO"
  region     = "ap-south-1"
}

resource "aws_instance" "app_server" {
  ami           = "ami-076e3a557efe1aa9c"
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleAppServerInstance"
  }
}

但面临问题配置 Terraform AWS Provider 时出错:加载配置:凭证类型 source_profile 配置文件默认.

我尝试导出 cmd 并配置默认配置文件,但对我没有任何作用。

我在这里做错了什么?

我删除了 .terraform 和 lock.hcl 并尝试了新的 terraform init

【问题讨论】:

  • 请不要将代码或错误作为屏幕截图发布,而是使用格式正确的代码块。
  • 您使用的 Terraform 版本是什么?
  • 还要检查 aws cli 是否正常工作。尝试运行“aws s3 ls”。或者与 aws ec2 相关的 aws cli 命令。
  • 如果您之前的配置不起作用,我建议您删除 .terraform 目录并再次运行 terraform init。此外,您是否在代码中的任何位置定义了带有 required_providersterraform 块?
  • 虽然这看起来确实是正确的配置,但我建议不要在 terraform 中配置凭据。考虑让 Terraform 期望环境已经具有所需的访问权限。也就是说,在共享配置中设置您的配置,然后在运行 terraform export AWS_PROFILE=some-profile 之前。

标签: terraform devops terraform-provider-aws aws-devops


【解决方案1】:

感谢这个问题。

我宁愿选择以下内容:

  1. 配置 AWS 配置文件:
    aws configure
    

    或者

    vim ~/.aws/config
    

    然后

    vim ~/.aws/credentials
    

    写一个新的配置文件名称或默认值如下:

    ~/.aws/凭据

    [default]
    region = us-east-1
    output = json
    
    [profile TERRAFORM]
    region=us-east-1
    output=json
    

    ~/.aws/凭据

    # Sitech
    [default]
    aws_access_key_id = A****
    aws_secret_access_key = B*********
    
    [TERRAFORM]
    aws_access_key_id = A****
    aws_secret_access_key = B*********
    
    1. 使用 terraform provider profile 术语而不是访问密钥和秘密访问密钥

    主程序

    provider "aws" {
      profile = var.aws_profile
      region  = var.main_aws_region
    }
    

    terraform.tfvars

    aws_profile     = "TERRAFORM"
    main_aws_region = "us-east-1"
    

【讨论】:

    猜你喜欢
    • 2019-11-17
    • 2021-03-27
    • 2019-04-06
    • 2023-04-02
    • 1970-01-01
    • 2020-04-21
    • 2019-06-06
    • 2021-09-29
    • 2018-12-14
    相关资源
    最近更新 更多