InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller:缺少凭证。

39

我正在尝试使用AWS CodeDeploy将GitHub项目部署到EC2实例。在跟随了两个视频教程和一堆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服务角色用于 CodeDeploy

策略名称: 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 上,我发现当我选择使用 GitHub 进行部署时,CodeDeploy 从未访问过我的存储库,我已设置正确的存储库名称和提交 ID。

在此输入图片描述

问题

我错过了什么?

7个回答

78

我遇到了同样的问题。问题的原因如下:

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

结果:出现错误:缺失凭证-请检查此实例是否是使用IAM实例配置文件启动的

解决方法: 重新启动codedeploy代理。使用以下命令:

sudo service codedeploy-agent restart

现在应该已经没有错误了!


5
我做了完全相同的事情,这解决了问题。 - d512
1
这对我有用。我在创建实例时没有附加 IAM,后来添加了它。所以这次重启对我有用。 - Jeethesh Kotian
1
你如何将IAM角色附加到机器上? - Kuldeep Yadav
这解决了我的问题。另外,似乎codedeploy-agent只会寻找“appspec.yml”文件而不是“appspec.yaml”。 - sareno

24

我遇到了“请检查此实例是否使用IAM实例配置文件启动”的问题。要检查实例是否没有使用IAM配置文件启动,请前往AWS控制台->您的实例->在“描述”选项卡中检查“IAM角色”值,如果为空,则表示您已经启动了没有IAM的实例,以下是解决此问题的方法:

  1. 前往IAM控制台->角色->创建新角色

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

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

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

  4. 尝试再次部署应用程序,应该就可以正常工作了


4

原来默认情况下Debian没有安装curl。在Bash脚本中进行curl请求获取服务器运行区域之前,需要先安装curl。这是脚本中缺失的部分。


很好,你找到了解决方案。我也遇到了同样的问题,你能否友好地分享一下你的步骤,这样我就可以比较一下我漏掉了什么。我正在使用CodeDeploy、BitBucket和AWS EC2实例,并遵循https://medium.com/@asoheili/one-click-deployment-to-aws-with-codedeploy-and-bitbucket-8550103207e。 - Adnan
1
当然,看看我在这里写的东西:https://github.com/0x4447/0x4447-article-how-to-think-about-the-AWS-infrastructure/tree/master/09_CodeDeploy - 我在那篇文章中倾囊相授。我希望它能对你有所帮助。 - David Gatti

2

我认为实例角色权限看起来不错。但是IAM实例配置文件仅在第一次启动实例时添加。在启动实例之前,请确保实例角色具有正确的权限。


你能否告诉我如何做到这一点?或者指向一个链接,展示如何做到这一点,这样我们就可以确定我正在检查正确的方法 :) - David Gatti
当我们启动EC2实例时,在“第3步:配置实例详细信息”页面上,我们被要求提供“IAM角色”。我所提到的在此处提供的IAM角色可能没有足够的权限。示例设置在这里:http://docs.aws.amazon.com/codedeploy/latest/userguide/getting-started-create-iam-instance-profile.html - binbinlu

2

将配置文件从EC2中分离,然后重新附加它(操作 -> 安全)。最后使用以下命令重启代理:

sudo service codedeploy-agent restart

我的情况与其他答案略有不同。我的配置文件看起来正确,并且拥有正确的策略。EC2已经附加到了角色上 - 至少这是我在AWS控制台中看到的。

根本原因是由于EC2没有正确的配置文件,由于相同名称的配置文件角色的重新生成而导致。可以使用curl http://169.254.169.254/latest/meta-data/iam/info进行确认。

404表示出现错误。


0

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服务器上安装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

0
在我的情况下,我不得不重新启动Code Deploy代理:
sudo systemctl restart codedeploy-agent

此外,我不得不将我的部署规范从“appspec.yaml”重命名为“appspec.yml”,似乎代理只查找.yml扩展名--顺便说一下,我在查看/var/log/aws/codedeploy-agent/codedeploy-agent.log日志文件后发现了这个错误。
$ tail /var/log/aws/codedeploy-agent/codedeploy-agent.log
2023-05-27T08:08:35 WARN  [codedeploy-agent(8830)]: InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller: Calling PutHostCommandComplete: "Code Error" 
2023-05-27T08:08:35 INFO  [codedeploy-agent(8830)]: Version file found in /opt/codedeploy-agent/.version with agent version OFFICIAL_1.6.0-49_rpm.
2023-05-27T08:08:35 INFO  [codedeploy-agent(8830)]: [Aws::CodeDeployCommand::Client 200 0.033923 0 retries] put_host_command_complete(command_status:"Failed",diagnostics:{format:"JSON",payload:"{\"error_code\":5,\"script_name\":\"\",\"message\":\"The CodeDeploy agent did not find an AppSpec file within the unpacked revision directory at revision-relative path \\\"appspec.yml\\\". The revision was unpacked to directory \\\"/opt/codedeploy-agent/deployment-root/f5518867-7740-4ce5-b65d-0931a6b26e66/d-THOQCJXLO/deployment-archive\\\", and the AppSpec file was expected but not found at path \\\"/opt/codedeploy-agent/deployment-root/f5518867-7740-4ce5-b65d-0931a6b26e66/d-THOQCJXLO/deployment-archive/appspec.yml\\\". Consult the AWS CodeDeploy Appspec documentation for more information at http://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file.html\",\"log\":\"\"}"}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接