【问题标题】:Terraform: Using versioned modules stored in AWS CodeCommitTerraform:使用存储在 AWS CodeCommit 中的版本化模块
【发布时间】:2020-06-22 14:11:11
【问题描述】:

目前有一个存储在 AWS CodeCommit 中的 terraform 模块存储库。在 Dev 和 Prod 之间,我们希望为模块使用相同的存储库,但能够让 Dev 和 Prod 使用不同的版本。

我已将标签附加到特定的提交,以便更容易地区分一个版本。但我似乎找不到任何关于如何引用该标签的文档。

我在下面找到了它是如何在 github 上完成的示例

module "stage_vpc" {
  source = "git::git@github.com:gruntwork-io/module-vpc.git//modules/vpc-app?ref=v0.0.4"

  vpc_name         = "stage"
  aws_region       = "us-east-1"
  num_nat_gateways = 3
  cidr_block       = "10.2.0.0/18"
}

但尝试对 CodeCommit 做同样的事情似乎没有用。它报告“错误响应代码:401”

试图确定这个 ?ref 是否是在 codecommit 中引用标签的正确方法。

https://git-codecommit.eu-west-1.amazonaws.com/v1/repos/reddwarf-terraform-infrastructure-modules/modules/subnets?ref=subnets-v0.0.1

谁能确认这是否是正确的方法?或者是否有其他方法?

编辑:我现在按照设置指南创建了一个 SSH 密钥,并将其放入我的 IAM 用户中。

module "subnets" {
  source = "git::ssh://git-codecommit.eu-west-1.amazonaws.com/v1/repos/reddwarf-terraform-infrastructure-modules/Modules//subnets.git"

这产生了以下错误

bobscutter@git-codecommit.eu-west-1.amazonaws.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

./ssh 文件夹存在正确的凭据,不确定我还缺少什么。我还检查了我可以从 Git Bash 连接并且它可以工作。

最终编辑:在从 https 切换到 SSH 并根据 The AWS documentation 创建 ./ssh 目录之后,现在可以正常工作了

我只需要在下面的路径中添加 // 而不是 /

  source = "git::ssh://git-codecommit.eu-west-1.amazonaws.com/v1/repos/reddwarf-terraform-infrastructure-modules//Modules//modules-orchestration//subnets

Terraform 成功找到并应用了模块。

【问题讨论】:

  • “似乎不起作用”是什么意思?你有错误吗?如果是,那是什么错误?请使用帮助您获得答案所需的信息更新您的问题。
  • 忘记粘贴了。已添加
  • @Pydam 您应该发布自己的解决方案作为答案。
  • @AlainO'Dea 谢谢!

标签: amazon-web-services terraform aws-codecommit


【解决方案1】:

为了解决这个问题,请按照AWS documentation 设置 SSH 连接

然后对子网模块使用下面的路径格式。

  source = "git::ssh://git-codecommit.eu-west-1.amazonaws.com/v1/repos/reddwarf-terraform-infrastructure-modules//Modules//modules-orchestration//subnets

然后在执行 Terraform 初始化后,Terraform 成功获取正确的模块。

此外,在使用名称 subnets-v0.0.1 标记提交并将其作为参考添加后,您可以将部署锁定到特定提交。

  source = "git::ssh://git-codecommit.eu-west-1.amazonaws.com/v1/repos/reddwarf-terraform-infrastructure-modules//Modules//modules-orchestration//subnets?ref=subnets-v0.0.1"

【讨论】:

    【解决方案2】:

    对于任何来到这里想知道您是否可以使用 git-remote-codecommit 和 terraform 从 CodeCommit 克隆的人。答案是肯定的,你可以!

    根据the terraform docs

    Terraform 通过运行 git clone 从 Git 存储库安装模块,因此它将尊重您系统上设置的任何本地 Git 配置,包括凭据。

    这意味着只要您的 AWS 配置文件设置正确,您就可以使用以下任一格式将您的模块 source 设置为 codecommmit 存储库

    • codecommit://<profile>@<repository>
    • codecommit::<region>://<profile>@<repository>

    它还通过 AWS_PROFILEAWS_DEFAULT_REGION 等环境变量进行配置

    【讨论】:

      猜你喜欢
      • 2019-05-30
      • 2022-09-25
      • 1970-01-01
      • 2018-06-07
      • 1970-01-01
      • 2020-06-03
      • 1970-01-01
      • 2019-11-22
      • 2015-05-24
      相关资源
      最近更新 更多