3

我正在使用一个名为的工具dbt,该工具的数据库身份验证方法使用 IAM。不幸的是,在构建 CodeBuild 项目时不存在 IAM 配置文件,因为它使用的是实例配置文件。因此,我无法连接到我的数据库。

参考这个问题,我尝试aws sts get-caller-identity在项目中运行,看看是否能够得到一些我需要返回的值,但它返回了

botocore.exceptions.ProfileNotFound: The config profile (***) could not be found

有人知道如何~/.aws/config在 CodeBuild 项目中生成我自己的吗?

编辑:该工具使用 boto3 在此处生成临时凭证:https ://github.com/fishtown-analytics/dbt/blob/9d00c000720d17c42a4fa08a26b75bd500cc857f/plugins/redshift/dbt/adapters/redshift/connections.py#L101-L123

但它似乎无法在 CodeBuild 项目中生成这些凭据。

编辑:

buildspec.yml

version: 0.2

env:
  variables:
    MODELS_REPO: dbt-dev
    PYTHON_VERSION: 3.8
  parameter-store:
    AWS_ENVIRONMENT: "/cloudformation/environment"
    AWS_PROFILE: "/cloudformation/environment"
    CODEARTIFACT_COMPANY: "/codeartifact/company"
    GITHUB_OWNER: "/github/owner"
    GITHUB_PERSONAL_ACCESS_TOKEN: "/secret/github/token"
    GITHUB_USER: "/github/user"

phases:
  install:
    runtime-versions:
        python: "${PYTHON_VERSION}"
    commands:
      - pip install -r projects/${PROJECT_NAME}/requirements.txt
      - ./projects/${PROJECT_NAME}/.aws/phases/install.sh
  pre_build:
    commands:
      - ./projects/${PROJECT_NAME}/.aws/phases/pre_build.sh
  build:
    commands:
      - ./projects/${PROJECT_NAME}/.aws/phases/build.sh
  post_build:
    commands:
      - ./projects/${PROJECT_NAME}/.aws/phases/post_build.sh

cache:
  paths:
    - /root/.cache/pip
    - /root/.cache/pip/**/*
    - ~/.cache/pip
    - ~/.cache/pip/**/*
4

1 回答 1

3

以下脚本应该适用于您的用例:

apt install jq -y
creds=$(aws sts get-session-token)

AWS_ACCESS_KEY_ID=$(echo $creds | jq '.Credentials.AccessKeyId')
AWS_SECRET_ACCESS_KEY=$(echo $creds | jq '.Credentials.SecretAccessKey')
AWS_SESSION_TOKEN=$(echo $creds | jq '.Credentials.SessionToken')

aws configure --profile $AWS_PROFILE set region "us-east-1"
aws configure --profile $AWS_PROFILE set output "json"
aws configure --profile $AWS_PROFILE set aws_secret_access_key "$AWS_SECRET_ACCESS_KEY"
aws configure --profile $AWS_PROFILE set aws_access_key_id "$AWS_ACCESS_KEY_ID"
aws configure --profile $AWS_PROFILE set aws_session_token "$AWS_SESSION_TOKEN"

您可以根据需要更改区域。

于 2020-10-06T12:15:42.270 回答