首页 > 解决方案 > 使用 GitHub SSH 私钥初始化和拉取 terraform 公共模块

问题描述

上下文:我有 gitlab 跑步者正在执行terraform init命令,该命令正在拉取所有必要的 terraform 模块。最近,我开始遇到 github 节流问题(每小时 60 次调用 github api)。所以我正在尝试重新配置我的管道,以便它使用 Github 用户的私钥。

目前,我的管道中有以下内容,但它似乎仍然无法正常工作,并且没有使用私钥来拉取 terraform 模块。

- GITHUB_SECRET=$(aws --region ${REGION} ssm get-parameters-by-path --path /github/umotifdev --with-decryption --query 'Parameters[*].{Name:Name,Value:Value}' --output json);
- PRIVATE_KEY=$(echo "${GITHUB_SECRET}" | jq -r '.[] | select(.Name == "/github/umotifdev/private_key").Value' | base64 -d);
- PUBLIC_KEY=$(echo "${GITHUB_SECRET}" | jq -r '.[] | select(.Name == "/github/umotifdev/public_key").Value' | base64 -d);
- mkdir -p ~/.ssh;
- echo "${PRIVATE_KEY}" | tr -d '\r' > ~/.ssh/id_rsa;
- chmod 700 ~/.ssh/id_rsa;
- eval $(ssh-agent -s);
- ssh-add ~/.ssh/id_rsa;
- ssh-keyscan -H 'github.com' >> ~/.ssh/known_hosts;
- ssh-keyscan github.com | sort -u - ~/.ssh/known_hosts -o ~/.ssh/known_host;
- echo -e "Host github.com\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config;
- echo ${PUBLIC_KEY} >> ~/.ssh/authorized_keys

我在管道中看到的错误类似于(基本上是来自 github 的限制):

Error: Failed to download module
Could not download module "vpc" (vpc.tf:17) source code from
"https://api.github.com/repos/terraform-aws-modules/terraform-aws-vpc/tarball/v2.21.0//*?archive=tar.gz":
bad response code: 403.

任何人都可以建议如何解决私钥不用于提取 terraform 模块的问题?

标签: githubsshgitlabterraform

解决方案


推荐阅读