【问题标题】:InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller: Missing credentialsInstanceAgent::Plugins::CodeDeployPlugin::CommandPoller:缺少凭据
【发布时间】:2017-06-19 05:48:00
【问题描述】:

我正在尝试使用 AWS CodeDeploy 将 GitHub 项目部署到 EC2 实例。在遵循 2 个视频教程和一堆 Google 答案后,我仍然收到以下错误:

2017-02-01 12:20:08 INFO  [codedeploy-agent(1379)]: master 1379: Spawned child 1/1
2017-02-01 12:20:09 INFO  [codedeploy-agent(1383)]: On Premises config file does not exist or not readable
2017-02-01 12:20:09 INFO  [codedeploy-agent(1383)]: InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor: Archives to retain is: 5}
2017-02-01 12:20:09 INFO  [codedeploy-agent(1383)]: Version file found in /opt/codedeploy-agent/.version.
2017-02-01 12:20:09 ERROR [codedeploy-agent(1383)]: InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller: Missing credentials - please check if this instance was started with an IAM instance profile

我有两个 IAM:

  • CodeDeployInstanceRole
  • CodeDeployServiceRole

EC2 实例的 CodeDeployInstanceRole

策略名称:AmazonEC2RoleforAWSCodeDeploy

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "s3:GetObject",
        "s3:GetObjectVersion",
        "s3:ListObjects"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}

策略名称:AutoScalingNotificationAccessRole

{
    "Version": "2012-10-17",
    "Statement": [{
        "Effect": "Allow",
        "Resource": "*",
        "Action": [
            "sqs:SendMessage",
            "sqs:GetQueueUrl",
            "sns:Publish"
        ]
      }
    ]
}

信任关系

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": [
          "codedeploy.amazonaws.com",
          "ec2.amazonaws.com"
        ]
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

CodeDeploy 的 CodeDeployServiceRole

策略名称:AWSCodeDeployRole

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "autoscaling:CompleteLifecycleAction",
        "autoscaling:DeleteLifecycleHook",
        "autoscaling:DescribeAutoScalingGroups",
        "autoscaling:DescribeLifecycleHooks",
        "autoscaling:PutLifecycleHook",
        "autoscaling:RecordLifecycleActionHeartbeat",
        "autoscaling:CreateAutoScalingGroup",
        "autoscaling:UpdateAutoScalingGroup",
        "autoscaling:EnableMetricsCollection",
        "autoscaling:DescribeAutoScalingGroups",
        "autoscaling:DescribePolicies",
        "autoscaling:DescribeScheduledActions",
        "autoscaling:DescribeNotificationConfigurations",
        "autoscaling:DescribeLifecycleHooks",
        "autoscaling:SuspendProcesses",
        "autoscaling:ResumeProcesses",
        "autoscaling:AttachLoadBalancers",
        "autoscaling:PutScalingPolicy",
        "autoscaling:PutScheduledUpdateGroupAction",
        "autoscaling:PutNotificationConfiguration",
        "autoscaling:PutLifecycleHook",
        "autoscaling:DescribeScalingActivities",
        "autoscaling:DeleteAutoScalingGroup",
        "ec2:DescribeInstances",
        "ec2:DescribeInstanceStatus",
        "ec2:TerminateInstances",
        "tag:GetTags",
        "tag:GetResources",
        "sns:Publish",
        "cloudwatch:DescribeAlarms",
        "elasticloadbalancing:DescribeLoadBalancers",
        "elasticloadbalancing:DescribeInstanceHealth",
        "elasticloadbalancing:RegisterInstancesWithLoadBalancer",
        "elasticloadbalancing:DeregisterInstancesFromLoadBalancer"
      ],
      "Resource": "*"
    }
  ]
}

信任关系

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": [
          "codedeploy.amazonaws.com",
          "ec2.amazonaws.com"
        ]
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

EC2 实例

我旋转了我自己基于 Debian 创建的映像,因此我已经安装了 NodeJS。当我旋转新实例时,我还将以下代码粘贴到 User data 文本区域以确保已安装 CodeDeploy。

#!/bin/bash -x

REGION=$(curl 169.254.169.254/latest/meta-data/placement/availability-zone/ | sed 's/[a-z]$//') &&

sudo apt-get update -y &&

sudo apt-get install -y python-pip &&

sudo apt-get install -y ruby &&

sudo apt-get install -y wget &&

cd /home/admin &&

wget https://aws-codedeploy-$REGION.s3.amazonaws.com/latest/install &&

chmod +x ./install &&

sudo ./install auto &&

sudo apt-get remove -y wget &&

sudo service codedeploy-agent start

调试

如果我登录我创建的 EC2 实例,并执行以下命令:

echo $(curl http://169.254.169.254/latest/meta-data/iam/security-credentials/)

我收到以下回复CodeDeployInstanceRole

当我执行时

curl http://169.254.169.254/latest/meta-data/iam/security-credentials/CodeDeployInstanceRole

我收到以下回复

{
  "Code" : "Success",
  "LastUpdated" : "2017-02-01T12:38:07Z",
  "Type" : "AWS-HMAC",
  "AccessKeyId" : "THE_KEY",
  "SecretAccessKey" : "SECRET",
  "Token" : "TOKEN",
  "Expiration" : "2017-02-01T19:08:43Z"
}

在 GitHub 上,我看到 CodeDeploy 永远不会访问我的存储库,即使我选择使用 GitHub 进行部署,我设置了正确的存储库名称和提交 ID。

问题

我错过了什么?

【问题讨论】:

    标签: amazon-web-services github amazon-ec2 amazon-iam aws-code-deploy


    【解决方案1】:

    我遇到了同样的问题。简述问题的原因:

    • 启动不附加任何角色的实例
    • 然后在该机器上安装 codedeploy-agent
    • 最后才将 IAM 角色附加到机器

    结果:我收到错误:Missing credentials - please check if this instance was started with an IAM instance profile

    解决方案:重启 codedeploy 代理。使用:

    sudo service codedeploy-agent restart
    

    错误现在应该消失了!

    【讨论】:

    • 我做了完全相同的事情,这解决了它。
    • 这对我有用。创建实例时我没有附加 IAM。我后来添加了它。所以这次重启对我有用
    • 如何将 IAM 角色附加到机器上?
    【解决方案2】:

    我收到“请检查此实例是否使用 IAM 实例配置文件启动”。要检查您的实例是否在没有 IAM 配置文件的情况下启动,请转到 AWS 控制台 -> 您的实例 -> 签入描述选项卡“IAM 角色”值,如果它为空,那么您已经启动了没有 IAM 的实例,这是解决问题的方法:

    1. 转到 IAM 控制台 -> 角色 -> 创建新角色

      选择 AWS 服务 -> EC2 -> 下一步:权限(不要更改任何内容)-> 下一步:标签 -> 下一步:审核 -> 提供名称并单击创建角色。

    2. 转到 AWS EC2 控制台 -> 选择实例 -> 操作 -> 实例设置 -> 附加/替换 IAM 角色 -> 选择您刚刚创建的 IAM 角色

    3. 重启 codedeploy 代理:sudo service codedeploy-agent restart

    4. 尝试再次部署,它应该可以工作

    【讨论】:

      【解决方案3】:

      事实证明,默认情况下 Debian 没有安装 curl。在发出 curl 请求以获取服务器正在运行的区域之前安装 curl 是 Bash 脚本中缺少的部分。

      【讨论】:

      • 您找到了解决方案,这很好。我也面临同样的问题,您能否如此友善并分享您的步骤,以便我可以比较我所缺少的。我正在使用 CodeDeploy、BitBucket 和 AWS EC2 实例并关注 medium.com/@asoheili/…
      • 当然,看看我在这里写了什么:github.com/0x4447/… - 我确实倾倒了我在那篇文章中学到的所有东西。希望对您有所帮助。
      【解决方案4】:

      实例角色权限对我来说看起来不错。但 IAM 实例配置文件仅在实例首次启动时添加。您能否在启动实例之前确保实例角色具有正确的权限?

      【讨论】:

      • 你能不能这么好心,告诉我怎么做?或者指向一个链接,它显示了如何做到这一点,所以我们可以确定我正在检查正确的方式:)
      • 当我们启动一个 ec2 实例时,我们被要求在“步骤 3:配置实例详细信息”页面上提供“IAM 角色”。我提到的此处提供的 IAM 角色可能没有足够的权限。示例设置在这里:docs.aws.amazon.com/codedeploy/latest/userguide/…
      【解决方案5】:

      这就是 2021 年在 Ubuntu 16.04 上对我有用的方法

      从 Python 3.5.2 升级到 3.6

      https://www.rosehosting.com/blog/how-to-install-python-3-6-on-ubuntu-16-04/ 用 sudo ...

      cd /opt
      wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
      tar -xvf Python-3.6.3.tgz
      cd Python-3.6.3
      ./configure
      apt-get install zlib1g-dev
      make
      make install
      

      安装最新版本的 aws cli v1

      cd ~
      curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
      unzip awscli-bundle.zip
      sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws
      

      修改实例元数据

      https://docs.aws.amazon.com/cli/latest/reference/ec2/modify-instance-metadata-options.html

      aws ec2 modify-instance-metadata-options \
        --instance-id ${FOO_ID} \
        --http-tokens optional \
        --http-endpoint enabled
      

      为 Ubuntu Server 安装 CodeDeploy 代理

      https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-ubuntu.html

      sudo apt-get update
      sudo apt-get install ruby
      sudo apt-get install wget
      cd /home/ubuntu
      wget https://aws-codedeploy-us-west-2.s3.us-west-2.amazonaws.com/latest/install
      chmod +x ./install
      sudo ./install auto
      sudo service codedeploy-agent restart
      sudo service codedeploy-agent status
      

      查看 Amazon Linux、RHEL 和 Ubuntu Server 实例上的部署日志文件

      https://docs.aws.amazon.com/codedeploy/latest/userguide/deployments-view-logs.html

      tail -f /var/log/aws/codedeploy-agent/codedeploy-agent.log
      tail -f /opt/codedeploy-agent/deployment-root/deployment-logs/codedeploy-agent-deployments.log
      

      【讨论】:

        猜你喜欢
        • 2019-03-02
        • 1970-01-01
        • 1970-01-01
        • 2020-09-26
        • 2013-03-06
        • 2015-03-19
        • 2014-01-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多