DOWNLOAD_SOURCE上的AWS CodeBuild错误:CLIENT_ERROR:找不到主要源和源版本的存储库

时间:2018-12-14 19:24:31

标签: terraform aws-codebuild terraform-provider-aws

我正在尝试使用Terraform创建一个CodeBuild项目,但是在构建时,我在DOWNLOAD_SOURCE步骤上收到以下错误:

CLIENT_ERROR:找不到主要来源和来源版本的存储库

该项目使用CodeCommit存储库作为源。这很奇怪,因为从CodeCommit控制台GUI到存储库的所有链接都可以在此版本中正常工作-我可以看到提交,单击链接并转到CodeCommit存储库,依此类推,因此Source设置似乎还不错。用于构建的策略对存储库具有“ codecommit:GitPull”权限。

奇怪的是,如果我在控制台中转至构建,然后取消选中“允许AWS CodeBuild修改此服务角色,使其可以与该构建项目一起使用”复选框,则选择Update Sources,该构建将起作用!但是我找不到从Terraform进行设置的任何方法,如果返回“更新源”屏幕,它将默认重新打开。

这是我用来创建版本的Terraform代码。

# IAM role for CodeBuild 
resource "aws_iam_role" "codebuild_myapp_build_role" {
  name = "mycompany-codebuild-myapp-build-service-role"
  description = "Managed by Terraform"
  path = "/service-role/"

  assume_role_policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "codebuild.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
EOF
}

# IAM policy for the CodeBuild role
resource "aws_iam_policy" "codebuild_myapp_build_policy" {
  name = "mycompany-codebuild-policy-myapp-build-us-east-1"
  description = "Managed by Terraform"
  policy = <<POLICY
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "ecr:BatchCheckLayerAvailability",
                "ecr:CompleteLayerUpload",
                "ecr:GetAuthorizationToken",
                "ecr:InitiateLayerUpload",
                "ecr:PutImage",
                "ecr:UploadLayerPart"
            ],
            "Resource": "*",
            "Effect": "Allow"
        },
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "logs:CreateLogStream",
                "codecommit:GitPull",
                "logs:PutLogEvents",
                "s3:GetObjectVersion"
            ],
            "Resource": [
                "arn:aws:logs:us-east-1:000000000000:log-group:/aws/codebuild/myapp-build",
                "arn:aws:logs:us-east-1:000000000000:log-group:/aws/codebuild/myapp-build:*",
                "arn:aws:s3:::codepipeline-us-east-1-*",
                "arn:aws:codecommit:us-east-1:000000000000:mycompany-devops-us-east-1"
            ]
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": "logs:CreateLogGroup",
            "Resource": [
                "arn:aws:logs:us-east-1:000000000000:log-group:/aws/codebuild/myapp-build",
                "arn:aws:logs:us-east-1:000000000000:log-group:/aws/codebuild/myapp-build:*"
            ]
        }
    ]
}
POLICY
}

# attach the policy
resource "aws_iam_role_policy_attachment" "codebuild_myapp_build_policy_att" {
    role       = "${aws_iam_role.codebuild_myapp_build_role.name}"
    policy_arn = "${aws_iam_policy.codebuild_myapp_build_policy.arn}"
}

# codebuild project
resource "aws_codebuild_project" "codebuild_myapp_build" {
    name = "myapp-build"
    build_timeout = "60"
    service_role = "${aws_iam_role.codebuild_myapp_build_role.arn}"

    artifacts {
        type = "NO_ARTIFACTS"
    }

    environment {
        compute_type = "BUILD_GENERAL1_SMALL"
        image = "aws/codebuild/docker:17.09.0"
        type = "LINUX_CONTAINER"
        privileged_mode = "true"

        environment_variable {
            "name" = "AWS_DEFAULT_REGION"
            "value" = "us-east-1"
        }

        environment_variable {
            "name" = "AWS_ACCOUNT_ID"
            "value" = "000000000000"
        }

        environment_variable {
            "name" = "IMAGE_REPO_NAME"
            "value" = "myapp-build"
        }

        environment_variable {
            "name" = "IMAGE_TAG"
            "value" = "latest"
        }

        environment_variable {
            "name" = "DOCKERFILE_PATH"
            "value" = "docker/codebuild/myapp_build_agent"
        }
    }

    source {
        type = "CODECOMMIT"
        location = "mycompany-devops-us-east-1"
        git_clone_depth = "1"
        buildspec = "docker/myapp/myapp_build/buildspec.yml"
    }

    tags {
        Name = "myapp-build"
        Environment = "${var.env_name}"   
        Region = "${var.aws_region}"
        ResourceType = "CodeBuild Project"
        ManagedBy = "Terraform"
    }
}

1 个答案:

答案 0 :(得分:0)

您的问题是来源的说明

source {
    type = "CODECOMMIT"
    location = "mycompany-devops-us-east-1"

Here's the Amazon documentation for the source,其中有些重点:

  
    

对于AWS CodeCommit存储库中的源代码,包含存储源代码和构建规范(例如https://git-codecommit.region-ID.amazonaws.com/v1/repos/repo-name)的存储库的HTTPS克隆URL

  

使用您在代码提交控制台中找到的“克隆网址”,可能就是这种情况:

https://git-codecommit.us-east-1.amazonaws.com/v1/repos/mycompany-devops-us-east-1

我在使用私有github存储库源时遇到了这个问题。就我而言,我给出的是URL,而不是github的克隆链接,所以问题非常相似:

bad:  https://github.com/privaterepo/reponame
good: https://github.com/privaterepo/reponame.git