【问题标题】:AWS Configure Bash One LinerAWS 配置 Bash One Liner
【发布时间】:2016-04-22 17:19:58
【问题描述】:

谁能告诉我如何在 bash 中使用一条线来自动化 aws 配置?

例子:

$ aws configure --profile user2
AWS Access Key ID [None]: AKIAI44QH8DHBEXAMPLE
AWS Secret Access Key [None]: je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY
Default region name [None]: us-east-1
Default output format [None]: text

应用程序:我想在 Docker 入口点内自动执行此操作!

【问题讨论】:

  • 如果此 docker 映像超出您的控制范围(即客户等),请不要在其中预配置 AWS 凭证。如果 docker 映像要在 EC2 实例或 ECS 上运行,请改用 IAM 角色。
  • 这将是一个多用途图像。凭据不是映像的一部分,但将为容器实例提供。
  • 容器会在 EC2 上运行吗?
  • 容器将在 EC2 内外的主机上运行。
  • 看看这里:docs.aws.amazon.com/AWSEC2/latest/UserGuide/…。基本上,您创建您的 IAM EC2 角色,运行分配给该角色的 EC2 实例,然后在没有配置 AWS 凭证的情况下在您的实例中使用 AWS CLI。实例将从角色中获取凭据。

标签: bash amazon-web-services docker


【解决方案1】:

如果你想自动化你应该使用文件而不是 CLI。您的 CLI 仅写入这些文件。

➜ cat ~/.aws/config
[profile_1]
output = json
region = eu-west-1
[profile_2]
output = json
region = eu-west-1

➜ cat ~/.aws/credentials
[profile_1]
aws_access_key_id =
aws_secret_access_key =
[profile_2]
aws_access_key_id =
aws_secret_access_key = 

【讨论】:

    【解决方案2】:

    如果您运行aws configure set help,您将看到您可以在命令行上单独提供设置,它们将被写入相关凭据或配置文件。例如:

    aws configure set aws_access_key_id AKIAI44QH8DHBEXAMPLE

    您也可以以交互方式运行它来修改默认凭据:

    aws configure

    或以交互方式运行它以创建/修改命名配置文件:

    aws configure --profile qa

    注意:使用上面的第一种技术,您键入的任何命令都会出现在您的历史记录中,这对于密码、密钥等来说不是一件好事。因此,在这种情况下,请使用不会导致 secret 参数的替代方法记录到历史记录,或prevent the entire command being logged to history

    【讨论】:

    • 我强烈建议不要将访问密钥嵌入到脚本中,这是非常糟糕的做法。以 Thomas L. answer 为例,在配置文件中配置您的密钥,然后将其与您的 docker 容器共享
    • 我也提到了这一点,因为 mayg 愿意自动化其 docker 容器部署,因此他很可能最终得到嵌入他真实访问/密钥的脚本。这是配置,不应混入代码中(例如,不在 github 上提交等)。欢呼
    • 感谢您的提示,我正在查看aws configure --help
    【解决方案3】:

    对于那些倾向于使用 bash 的人来说,以下内容非常有效,并且不会让您的脚本保密。此外,它还会一次性将您的输入保存到指定的配置文件中。

    printf "%s\n%s\nus-east-1\njson" "$KEY_ID" "$SECRET_KEY" | aws configure --profile my-profile
    

    【讨论】:

      【解决方案4】:

      根据 Tom 在 jarmod 的回答中提出的建议,“在配置文件中配置您的密钥,然后与您的 docker 容器共享”。

      我发现这有点令人困惑,因为我刚开始使用 Docker 和 awscli。
      另外,我相信大多数最终遇到这个问题的人都在尝试同时使用 Docker 和awscli

      所以你想要做的,一步一步是:

      创建一个credentials 文件,其中包含
      [default] aws_access_key_id = default_access_key aws_secret_access_key = default_secret_key
      复制到 ~/.aws/credentials,使用 Dockerfile 中的一行,例如
      COPY credentials /root/.aws/credentials

      还有一个config 文件包含
      [default] region = us-west-2 output = table
      复制到 ~/.aws/config,使用 Dockerfile 中的一行,例如
      COPY config /root/.aws/config

      参考:
      aws configure set help

      【讨论】:

        【解决方案5】:

        一个班轮

        aws configure set aws_access_key_id "AKIAI44QH8DHBEXAMPLE" --profile user2 && aws configure set --profile user2 && aws_secret_access_key "je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY" --profile user2 && aws configure set region "us-east-1" --profile user2 && aws configure set output "text" --profile user2
        

        注意:设置区域是可选的(如果你没有任何区域,也永远不要用空字符串设置它,否则会出错);以及用户配置文件,如果您不设置它,它将进入 默认 设置。

        ?Secrets 的更好实践

        使用机密,然后使用关联的环境变量:

        aws configure set aws_access_key_id "$AWS_ACCESS_KEY_ID" --profile user2 && aws configure set --profile user2 && aws_secret_access_key "$AWS_ACCESS_KEY_SECRET" --profile user2 && aws configure set region "$AWS_REGION" --profile user2 && aws configure set output "text" --profile user2
        

        ? 了解更多

        【讨论】:

          【解决方案6】:

          我认为这就是答案

          aws configure set aws_access_key_id $YOUR_ACCESS_KEY_ID; aws configure set aws_secret_access_key $YOUR_SECRET_ACCESS_KEY; aws configure set default.region $YOUR_AWS_DEFAULT_REGION
          

          【讨论】:

            【解决方案7】:

            一个班轮

            aws configure set aws_access_key_id "$AWS_ACCESS_KEY_ID" --profile profile_name_here && aws configure set aws_secret_access_key "$AWS_SECRET_ACCESS_KEY" --profile profile_name_here && aws configure set region "$AWS_REGION" --profile profile_name_here && aws configure set output "json" --profile profile_name_here
            

            设置个人配置

            profile_name_here 是要保存到您的 aws 配置中的 aws 配置文件名称。用你自己的替换它。

            访问密钥

            aws configure set aws_access_key_id "$AWS_ACCESS_KEY_ID" --profile profile_name_here

            秘密访问密钥

            aws configure set aws_secret_access_key "$AWS_SECRET_ACCESS_KEY" --profile profile_name_here

            地区

            aws configure set region "$AWS_REGION" --profile profile_name_here

            输出

            aws configure set output "json" --profile profile_name_here

            此处指定的值为 json,但您可以从aws docs 支持的输出格式列表中替换它。

            • json
            • yaml
            • yaml 流
            • 文字
            • 表格

            注意:

            $AWS_ACCESS_KEY_ID$AWS_SECRET_ACCESS_KEY$AWS_REGION 是您的 AWS 凭证 文件或 中的变量>环境变量(如果您使用 CI)。您也可以使用常规字符串值替换它们,但这并不安全。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-04-05
              • 2014-03-20
              • 2012-12-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-03-19
              • 2015-02-04
              相关资源
              最近更新 更多