-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CodeBuild: Associating an existing IAM role to a CodeBuild project results in exception #2651
CodeBuild: Associating an existing IAM role to a CodeBuild project results in exception #2651
Comments
This was an attempt at a workaround for #2652 |
Thanks for the report @digitalsanctum. Confirming this is indeed a bug - I was able to reproduce it locally. I'll be working on a fix. |
A CodeBuild Project needs to have appropriate EC2 permissions on creation when it uses a VPC. However, the default Policy that a Project Role has depends on the Project itself (for CloudWatch Logs permissions). Because of that, add a dependency between the Policy containing the EC2 permissions and the Project. Also correctly handle the case when the Project's Role is imported. Fixes aws#2651 Fixes aws#2652
A CodeBuild Project needs to have appropriate EC2 permissions on creation when it uses a VPC. However, the default Policy that a Project Role has depends on the Project itself (for CloudWatch Logs permissions). Because of that, add a dependency between the Policy containing the EC2 permissions and the Project. Also correctly handle the case when the Project's Role is imported. BREAKING CHANGE: the method addToRoleInlinePolicy in CodeBuild's Project class has been removed. Fixes aws#2651 Fixes aws#2652 comment out the imported check to see if it works now
A CodeBuild Project needs to have appropriate EC2 permissions on creation when it uses a VPC. However, the default Policy that a Project Role has depends on the Project itself (for CloudWatch Logs permissions). Because of that, add a dependency between the Policy containing the EC2 permissions and the Project. BREAKING CHANGE: the method addToRoleInlinePolicy in CodeBuild's Project class has been removed. Fixes aws#2651 Fixes aws#2652
…#2662) A CodeBuild Project needs to have appropriate EC2 permissions on creation when it uses a VPC. However, the default Policy that a Project Role has depends on the Project itself (for CloudWatch Logs permissions). Because of that, add a dependency between the Policy containing the EC2 permissions and the Project. BREAKING CHANGE: the method addToRoleInlinePolicy in CodeBuild's Project class has been removed. Fixes #2651 Fixes #2652
I'm on 1.19.0 and this is still occurring. My code looks like:
And the error is:
If I change this code to use any IAM Role not in /service-role/ path, it works The problem seems to be in aws-iam/lib/policy as it is not cleaning the role path as needed for cloudformation to use only the last part of the path as role name CC @skinny85 to reopen the Issue if needed |
@pcolazurdo I just tried it with role=iam.Role.from_role_arn(
self,
"codebuild-EmbeddedMetrics",
"arn:aws:iam::xx:role/service-role/codebuild-EmbeddedMetrics-service-role"
)
cdk_build = codebuild.PipelineProject(self, "CdkBuild",
build_spec = codebuild.BuildSpec.from_source_filename("buildspec.yml"),
description = "EmbeddedMetrics Build",
role = role,
environment = codebuild.BuildEnvironment(build_image=codebuild.LinuxBuildImage.STANDARD_3_0),
) And it works: $ npx cdk synth
Resources:
ServiceCatalogPipelineCdkBuildB88770B6:
Type: AWS::CodeBuild::Project
Properties:
Artifacts:
Type: CODEPIPELINE
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/standard:3.0
PrivilegedMode: false
Type: LINUX_CONTAINER
ServiceRole: arn:aws:iam::xx:role/service-role/codebuild-EmbeddedMetrics-service-role
Source:
BuildSpec: buildspec.yml
Type: CODEPIPELINE
Description: EmbeddedMetrics Build
Metadata:
aws:cdk:path: hello-cdk-1/ServiceCatalogPipeline/CdkBuild/Resource
CDKMetadata:
Type: AWS::CDK::Metadata
Properties:
Modules: aws-cdk=1.19.0,@aws-cdk/assets=1.19.0,@aws-cdk/aws-apigateway=1.19.0,@aws-cdk/aws-applicationautoscaling=1.19.0,@aws-cdk/aws-autoscaling=1.19.0,@aws-cdk/aws-autoscaling-common=1.19.0,@aws-cdk/aws-autoscaling-hooktargets=1.19.0,@aws-cdk/aws-certificatemanager=1.19.0,@aws-cdk/aws-cloudformation=1.19.0,@aws-cdk/aws-cloudfront=1.19.0,@aws-cdk/aws-cloudwatch=1.19.0,@aws-cdk/aws-codebuild=1.19.0,@aws-cdk/aws-codecommit=1.19.0,@aws-cdk/aws-codedeploy=1.19.0,@aws-cdk/aws-codepipeline=1.19.0,@aws-cdk/aws-codepipeline-actions=1.19.0,@aws-cdk/aws-cognito=1.19.0,@aws-cdk/aws-dms=1.19.0,@aws-cdk/aws-ec2=1.19.0,@aws-cdk/aws-ecr=1.19.0,@aws-cdk/aws-ecr-assets=1.19.0,@aws-cdk/aws-ecs=1.19.0,@aws-cdk/aws-elasticloadbalancing=1.19.0,@aws-cdk/aws-elasticloadbalancingv2=1.19.0,@aws-cdk/aws-events=1.19.0,@aws-cdk/aws-events-targets=1.19.0,@aws-cdk/aws-glue=1.19.0,@aws-cdk/aws-iam=1.19.0,@aws-cdk/aws-kinesis=1.19.0,@aws-cdk/aws-kms=1.19.0,@aws-cdk/aws-lambda=1.19.0,@aws-cdk/aws-logs=1.19.0,@aws-cdk/aws-logs-destinations=1.19.0,@aws-cdk/aws-rds=1.19.0,@aws-cdk/aws-route53=1.19.0,@aws-cdk/aws-route53-targets=1.19.0,@aws-cdk/aws-s3=1.19.0,@aws-cdk/aws-s3-assets=1.19.0,@aws-cdk/aws-sam=1.19.0,@aws-cdk/aws-secretsmanager=1.19.0,@aws-cdk/aws-servicediscovery=1.19.0,@aws-cdk/aws-sns=1.19.0,@aws-cdk/aws-sns-subscriptions=1.19.0,@aws-cdk/aws-sqs=1.19.0,@aws-cdk/aws-ssm=1.19.0,@aws-cdk/aws-stepfunctions=1.19.0,@aws-cdk/core=1.19.0,@aws-cdk/cx-api=1.19.0,@aws-cdk/region-info=1.19.0,jsii-runtime=Python/3.6.5 (BTW, you're using the |
Thanks for the reply @skinny85 but when I try to do cdk deploy it fails with the error:
(exactly as before) Btw, thanks for the tip about BuildEnvironment - I was struggling to find the right syntax here and was reading through the docs |
I think that means you can't use service roles for CodeBuild projects, no? I mean, that error is the from the CodeBuild API I assume. |
i have same issues, const buildProjectRole = iam.Role.fromRoleArn(this, 'CodeBuildRole', 'arn:aws:iam::xxx:role/service-role/codebuild-role')
const project = new codebuild.Project(this, 'MyProject', {
role: buildProjectRole,
...
} error msg 9/11 | 4:18:47 AM | CREATE_FAILED | AWS::IAM::Policy | CodeBuildRole/Policy (CodeBuildRolePolicy0442214A) The specified value for roleName is invalid. It must contain only alphanumeric characters and/or the following: +=,.@_- (Service: AmazonIdentityManagement; Status Code: 400; Error Code: ValidationError; Request ID: c0688767-1078-415a-a062-6bb9061b928d) |
The problem seems to be in the code for IAM:
If I manually change this with:
it works totally fine |
Aaah, I get it now :). Thanks @pcolazurdo , I'll try to get this fixed! |
Service roles have a different ARN structure (they have a 'service-role/' segment after the 'role/' part). Explicitly check for that case when importing a role with such an ARN (you cannot pass 'service-role/RoleName' as a legal role name). Fixes aws#2651
Service roles have a different ARN structure (they have a 'service-role/' segment after the 'role/' part). Explicitly check for that case when importing a role with such an ARN (you cannot pass 'service-role/RoleName' as a legal role name). Fixes #2651 Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Describe the bug
Creating a new CodeBuild project and associating an existing IAM role results in:
Policy must be attached to at least one principal: user, group or role
during synthesis. My best guess is that the default policy generated by CDK is getting orphaned instead of disregarded in the case where an existing IAM role is specified?To Reproduce
Using the following code to make the association:
Expected behavior
Associating a valid existing role to a CodeBuild project should not result in exception.
Version:
The text was updated successfully, but these errors were encountered: