AWS CodeBuild create from CloudFormation can not find source

Viewed 25

I have am building a simple code AWS Code Build task, loading code from code commit. When I create the task from the console all is good. But if I create the task from CloudFormation, I get the following error when running the task:

CLIENT_ERROR: repository not found for primary source and source version

My CloudFormation looks like this:

"Project": {
        "Type": "AWS::CodeBuild::Project",
        "Properties": {
            "Name": "DockerBuild",
            "Description": "Build a docker Image of the Project",
            "ServiceRole": {
                "Fn::GetAtt": [
                    "CodeBuildServiceRole",
                    "Arn"
                ]
            },
            "Artifacts": {
                "Type": "no_artifacts"
            },
            "Environment": {
                "Type": "LINUX_CONTAINER",
                "ComputeType": "BUILD_GENERAL1_SMALL",
                "Image": "aws/codebuild/java:openjdk-8",
                "EnvironmentVariables": [
                    {
                        "Name": "AWS_DEFAULT_REGION",
                        "Type": "PLAINTEXT",
                        "Value": "ca-central-1"
                    },
                    {
                        "Name": "AWS_ACCOUNT_ID",
                        "Type": "PLAINTEXT",
                        "Value": {
                            "Ref": "ServiceName"
                        }
                    },
                    {
                        "Name": "IMAGE_TAG",
                        "Type": "PLAINTEXT",
                        "Value": "latest"
                    },
                    {
                        "Name": "IMAGE_REPO_NAME",
                        "Type": "PLAINTEXT",
                        "Value": {"Ref":"Repository"}
                    }
                ]
            },
            
            "Source": {
                "Location": "git-codecommit.ca-central-1.amazonaws.com/v1/repos/demoRepo",
                "Type": "CODECOMMIT"
            },
            "SourceVersion": "refs/heads/main",
            "TimeoutInMinutes": 10,
            "Tags": []
        }
    }

If I try to update the Source from the task, it finds all commit number, so the source should be good.

Note that, not sure if it is related or not, but if I try to save my update on source with the option "allow AWS CodeBuild to modify this service role", I get the following error:

The policy was not attached to role CodeBuild-CodeBuildServiceRole-XXXXXXX

I am not sure why or if this has an impact.

If that can help, here is the role created through CloudFormation:

  "CodeBuildServiceRole": {
        "Type": "AWS::IAM::Role",
        "Properties": {
            "AssumeRolePolicyDocument": {
                "Version": "2012-10-17",
                "Statement": [
                    {
                        "Effect": "Allow",
                        "Principal": {
                            "Service": [
                                "codebuild.amazonaws.com"
                            ]
                        },
                        "Action": [
                            "sts:AssumeRole"
                        ]
                    }
                ]
            },
            "Path": "/",
            "Policies": [
                {
                    "PolicyName": "root",
                    "PolicyDocument": {
                        "Version": "2012-10-17",
                        "Statement": [
                            {
                                "Action": [
                                    "codebuild:*",
                                    "codecommit:*",
                                    "cloudwatch:GetMetricStatistics",
                                    "ec2:DescribeVpcs",
                                    "ec2:DescribeSecurityGroups",
                                    "ec2:DescribeSubnets",
                                    "ecr:DescribeRepositories",
                                    "ecr:ListImages",
                                    "elasticfilesystem:DescribeFileSystems",
                                    "events:DeleteRule",
                                    "events:DescribeRule",
                                    "events:DisableRule",
                                    "events:EnableRule",
                                    "events:ListTargetsByRule",
                                    "events:ListRuleNamesByTarget",
                                    "events:PutRule",
                                    "events:PutTargets",
                                    "events:RemoveTargets",
                                    "logs:GetLogEvents",
                                    "logs:CreateLogGroup",
                                    "logs:CreateLogStream",
                                    "s3:GetBucketLocation",
                                    "s3:ListAllMyBuckets"
                                ],
                                
                                "Effect": "Allow",
                                "Resource": "*"
                            },
                            {
                                "Action": [
                                    "logs:DeleteLogGroup"
                                ],
                                "Effect": "Allow",
                                "Resource": "arn:aws:logs:*:*:log-group:/aws/codebuild/*:log-stream:*"
                            },
                            {
                                "Effect": "Allow",
                                "Action": [
                                    "ssm:PutParameter"
                                ],
                                "Resource": "arn:aws:ssm:*:*:parameter/CodeBuild/*"
                            },
                            {
                                "Effect": "Allow",
                                "Action": [
                                    "ssm:StartSession"
                                ],
                                "Resource": "arn:aws:ecs:*:*:task/*/*"
                            },
                            {
                                "Sid": "CodeStarConnectionsReadWriteAccess",
                                "Effect": "Allow",
                                "Action": [
                                    "codestar-connections:CreateConnection",
                                    "codestar-connections:DeleteConnection",
                                    "codestar-connections:UpdateConnectionInstallation",
                                    "codestar-connections:TagResource",
                                    "codestar-connections:UntagResource",
                                    "codestar-connections:ListConnections",
                                    "codestar-connections:ListInstallationTargets",
                                    "codestar-connections:ListTagsForResource",
                                    "codestar-connections:GetConnection",
                                    "codestar-connections:GetIndividualAccessToken",
                                    "codestar-connections:GetInstallationUrl",
                                    "codestar-connections:PassConnection",
                                    "codestar-connections:StartOAuthHandshake",
                                    "codestar-connections:UseConnection"
                                ],
                                "Resource": "arn:aws:codestar-connections:*:*:connection/*"
                            },
                            {
                                "Sid": "CodeStarNotificationsReadWriteAccess",
                                "Effect": "Allow",
                                "Action": [
                                    "codestar-notifications:CreateNotificationRule",
                                    "codestar-notifications:DescribeNotificationRule",
                                    "codestar-notifications:UpdateNotificationRule",
                                    "codestar-notifications:DeleteNotificationRule",
                                    "codestar-notifications:Subscribe",
                                    "codestar-notifications:Unsubscribe"
                                ],
                                "Resource": "*",
                                "Condition": {
                                    "StringLike": {
                                        "codestar-notifications:NotificationsForResource": "arn:aws:codebuild:*"
                                    }
                                }
                            },
                            {
                                "Sid": "CodeStarNotificationsListAccess",
                                "Effect": "Allow",
                                "Action": [
                                    "codestar-notifications:ListNotificationRules",
                                    "codestar-notifications:ListEventTypes",
                                    "codestar-notifications:ListTargets",
                                    "codestar-notifications:ListTagsforResource"
                                ],
                                "Resource": "*"
                            },
                            {
                                "Sid": "CodeStarNotificationsSNSTopicCreateAccess",
                                "Effect": "Allow",
                                "Action": [
                                    "sns:CreateTopic",
                                    "sns:SetTopicAttributes"
                                ],
                                "Resource": "arn:aws:sns:*:*:codestar-notifications*"
                            },
                            {
                                "Sid": "SNSTopicListAccess",
                                "Effect": "Allow",
                                "Action": [
                                    "sns:ListTopics",
                                    "sns:GetTopicAttributes"
                                ],
                                "Resource": "*"
                            },
                            {
                                "Sid": "CodeStarNotificationsChatbotAccess",
                                "Effect": "Allow",
                                "Action": [
                                    "chatbot:DescribeSlackChannelConfigurations"
                                ],
                                "Resource": "*"
                            }
                        ]
                    }
                }
            ]
        }
    },
1 Answers

So the problem is the Location needs ton include https in front. Value should have been

"Location": "https://git-codecommit.ca-central-1.amazonaws.com/v1/repos/demoRepo"

Related