(cdk-pipelines): Cannot build Docker assets in a VPC: Not authorized to perform DescribeSecurityGroups
Use case
We need to be able to access company internal repositories during the Docker build which means they need to run within a VPC.
However, when adding a VPC configuration to a CDK Pipeline the UpdatePipeline stage fails with:
PipelinemasterAssetsDockerAsset1FDB4A08A Failed to call UpdateProject, reason: Not authorized to perform DescribeSecurityGroups (Service: AWSCodeBuild; Status Code: 400; Error Code: InvalidInputException
Reproduction Steps
const createPipeline = (scope: cdk.Construct, repository: codecommit.IRepository, repositoryName: string, branch: string, sourceArtifact: codepipeline.Artifact) => {
const cloudAssemblyArtifact = new codepipeline.Artifact();
const vpc = ec2.Vpc.fromVpcAttributes(scope, `PipelineVpc-${branch}`, VpcAttrs);
return new pipelines.CdkPipeline(scope, `Pipeline-${branch}`, {
pipelineName: `${repositoryName}-${branch}`,
cloudAssemblyArtifact,
// Adding VPC causes a failure
vpc,
sourceAction: new codepipeline_actions.CodeCommitSourceAction({
actionName: 'Source',
repository,
branch,
output: sourceArtifact,
}),
synthAction: pipelines.SimpleSynthAction.standardNpmSynth({
environment: { privileged: true },
sourceArtifact,
cloudAssemblyArtifact,
buildCommand: 'npm run build',
subdirectory: 'cdk',
}),
});
}
const createPipelineForBranch = (scope: cdk.Construct, repository: codecommit.IRepository, repositoryName: string, branch: string) => {
const pipeline = createPipeline(scope, repository, repositoryName, branch, sourceArtifact);
const stage = new cdk.Stage(scope, 'stage');
const buildStack = new cdk.Stack(stage, `build-stack`);
const dockerAsset = new assets.DockerImageAsset(buildStack, 'app', {
directory: '../app',
});
}
What did you expect to happen?
The Docker asset would be built in the VPC.
What actually happened?
CloudFormation update failed.
Environment
- CDK CLI Version : 1.75.0 (build 7708242)
- Framework Version: 1.75.0 (build 7708242)
- Node.js Version: v14.15.1
- OS : macOs
- Language (Version): TypeScript 4
Other
This is :bug: Bug Report
This implies the permissions of the cdk-deploy-role.
What kind of policies did you attach to it?
The deployment works just fine without specifying vpc for CdkPipeline . We are using the default app-name-PipelinemasterPipelineBu-12NJSZ97QETRK role with the default policy PipelinemasterPipelineBuildSynthCdkBuildProjectRoleDefaultPolicy267E5B41.
The failure happens in the UpdatePipeline-SelfMutate action after adding the VPC property to CdkPipeline. @rix0rrr does this answer your question?
It does not, really.
You didn't say, but I'm assuming the
PipelinemasterAssetsDockerAsset1FDB4A08A Failed to call UpdateProject, reason: Not authorized to perform DescribeSecurityGroups (Service: AWSCodeBuild; Status Code: 400; Error Code: InvalidInputException
Error is an error reported by CloudFormation (check your Stack Events to confirm)? If so, the PipelinemasterPipelineBu-12NJSZ97QETRK role's permissions are no longer the ones being used. It's actually the permissions of
cdk-hnb659fds-cfn-exec-role-111111111111-us-east-1
That are of interest.
@rix0rrr the cdk-hnb659fds-cfn-exec-role-xxxx-eu-central-1 role has AdministratorAccess policy. The error is indeed produced by CloudFormation when deploying the resource CodeBuild::Project named PipelinemasterAssetsDockerAsset1FDB4A08A.
Trying to configure a VPC manually for PipelinemasterAssetsDockerA-pRPcWndNLB3x does not work either and produces an error "The trusted principals don't belong to the service." in the AWS Console.
The call chain might also look like this:
┌───────────────────┐ ┌───────────────────┐ ┌───────────────────┐
│ │ │ │ │ │
│ CloudFormation │ │ CodeBuild │ │ EC2 │
│ │ │ │ │ │
└───────────────────┘ └───────────────────┘ └───────────────────┘
│ │ │
UpdateStack │ │ │
────────────────────────▶│ │ │
│ │ │
│───┐ ASSUME │ │
│ │ cdk-hnb659fds-cfn │ │
│◀──┘ -exec-role-xxxx-yyyy │ │
│ │ │
│ │ │
│ │ │
│ UpdateProject │ │
├───────────────────────────────────▶│ │
│ │ ASSUME │
│ │───┐ Pipelinemaster │
│ │ │ AssetsDockerARole-xxxxx │
│ │◀──┘ │
│ │ │
│ │ DescribeSecurityGroups │
│ │─────────────────────────────────▶│
│ │ │
│ │ │ !! ERROR !!
│ │ Not authorized │
│ │◀─────────────────────────────────│
│ Not authorized │ │
│◀───────────────────────────────────│ │
│ │ │
│ │ │
A quick glance at CloudTrail will tell you for sure, but if it's not the exec role then it must be the execution role of the CodeBuild project.
My knowledge of what permissions would be necessary for this elude me, but @skinny85 might know.
This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.
@skinny85 @rix0rrr have you been able to confirm this? We are still seeing the error with CDK 1.78.0.
I am also seeing the same issue. ~~The problem seems to be with:~~
~~https://github.com/aws/aws-cdk/blob/v1.80.0/packages/@aws-cdk/pipelines/lib/pipeline.ts#L453-L459~~
~~According to CodeBuild documentation the generated role is missing some permissions when running in VPC.~~
After a bit more digging I found out the code to add these permissions already exists in aws-codebuild.Project.addVpcRequiredPermissions():
https://github.com/aws/aws-cdk/blob/v1.80.0/packages/%40aws-cdk/aws-codebuild/lib/project.ts#L1114
I'm unsure what makes the special role created by generateAssetRole() to not be modified by it as well. I can't see anything too obvious.
It does work for my synth action as I can see it has the right permissions from addVpcRequiredPermissions().
As a workaround, you can pass role_policy_statements as part of the constructors for SimpleSynthAction. Adding ec2:DescribeAvailabilityZones fixes this issue.
@davidhessler unfortunately that does not solve the problem described in this issue. This is about an issue with ec2: DescribeSecurityGroups which seems to be missing somewhere. All guidance would be appreciated to be able to use the CDK Pipelines with Docker assets in a VPC to comply with the enterprise security setup that requires the usage of internal repositories.
⚠️COMMENT VISIBILITY WARNING⚠️
Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.
@markusl Was this solved for you? I have similar issues on CDK version 1.100.0. Adding the vpc property to my CdkPipeline causes deploy/creation to fail with a similar error message as you reported: Not authorized to perform DescribeSecurityGroups (Service: AWSCodeBuild; Status Code: 400; Error Code: InvalidInputException;
My pipeline contains a stage with stacks described using PythonFunctions (python lambda built using Docker).
Using bit older version 1.89.0 and having same issue as described in above post. I was just lazy and tried it manually first and it does not allow me to put build project in VPC. I get same "The trusted principals don't belong to the service". This is frustrating because I am pulling docker image from docker hub. Since project is not updated that often It would work perfectly fine just being tied to IP address, but now that it uses random ip amazon gives it it occasionally fails pulling the image.

edit: i have admin privileges to account