aws / aws/aws-cdk

codebuild: project creation fails when GitHub source webhook is enabled

Open
#31,726 10 comments 13 reactions 0 assignees View on GitHub
@aws-cdk/aws-codebuild bug effort/small p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

CloudFormation throws an error when we try to create a CodeBuild project with GitHub webhook is configured.

> 16:13:04 | CREATE_FAILED | AWS::CodeBuild::Project | MyProject2B52B17CC
Failed to call CreateWebhook, reason: Access denied to connection arn:aws:codeconnections:us-west-2:REDACTED:connection/REDACTED (Service: AWSCodeBuild; Status Cod
e: 400; Error Code: InvalidInputException; Request ID: REDACTED; Proxy: null)

### Regression Issue

- [ ] Select this option if this issue appears to be a regression.

### Last Known Working CDK Version

_No response_

### Expected Behavior

CodeBuild project is successfully created.

### Current Behavior

CloudFormation throws an error when creating a project.

### Reproduction Steps

1. First, create a GitHub app connection from the management console. https://docs.aws.amazon.com/codebuild/latest/userguide/connections-github-app.html
2. Deploy the below stack, and you'll get an error. (replace `YOUR_GITHUB_NAME` and `YOUR_REPOSITORY_NAME` to an existing repo.)

```ts
import * as codebuild from 'aws-cdk-lib/aws-codebuild';

export class CodebuildGhaCdkStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const project = new codebuild.Project(this, 'MyProject', {
source: codebuild.Source.gitHub({
owner: 'YOUR_GITHUB_NAME',
repo: 'YOUR_REPOSITORY_NAME',
webhookFilters: [codebuild.FilterGroup.inEventOf(codebuild.EventAction.WORKFLOW_JOB_QUEUED)],
}),
});
// uncommenting this makes it work
// project.role!.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName('AdministratorAccess'));
}
}

```

### Possible Solution

https://github.com/aws/aws-cdk/issues/31726#issuecomment-2413937735

This was a problem of both permission and dependency.

### Additional Information/Context

When I added Administrator permission to the project, CFn successfully deployed the project. But not sure exactly which permission is missing.

```ts
// WORKS
project.role!.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName('AdministratorAccess'));
```

Allowing codeconnections did not work:

```ts
// NOT WORK
project.addToRolePolicy(
new PolicyStatement({
actions: [
'codeconnections:*',
'codestar-connections:*',
],
resources: ['*'],
})
);
```

### CDK CLI Version

2.162.0

### Framework Version

_No response_

### Node.js Version

20

### OS

macos

### Language

TypeScript

### Language Version

_No response_

### Other information

We need to enable webhook to use [a CodeBuild-hosted GitHub Actions runner](https://docs.aws.amazon.com/codebuild/latest/userguide/sample-github-action-runners.html).

Contributor guide

Open the contributing guide

Research direction

Start by running the supplied TypeScript CDK reproduction with Source.gitHub and webhookFilters enabled, then inspect the project role permissions and resource dependency behavior described in the issue. Done means CloudFormation can create the CodeBuild project with only the required permissions and correct ordering, without AdministratorAccess.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, github, typescript
Domain
cloud, devops
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.