I am trying to create a new project in AWS CodeBuild. Every time I attempt to I receive the following error:
Not authorized to perform DescribeSecurityGroups
Any help would be greatly appreciated.
CodeBuild IAM Requirements Typically a CodeBuild project will require access to a limited set of AWS resources including CodeBuild, S3, and Cloudwatch logs. The following IAM permission set will create a role that has these default permissions and will be suitable to reuse in any new CodeBuild projects.
CodeBuild uses the CodeBuild service role as the default AWS credential in the build container and Docker runtime. Export the AssumeRole credentials as environment variables. Then, pass these variables into the Docker runtime by using the --build-arg parameter for docker build.
I had this same issue when using cloudformation. The issue was the IAM role was being created before CodeBuild started creation, but the Policy attached the IAM role was being created after CodeBuild was created.
The remedy for this was to add a DependsOn
to CodeBuild saying it needs the Policy to be created first.
Ex:
CodeBuildIamRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: 'CodeBuildAutomatedTestingRole'
AssumeRolePolicyDocument:
Statement:
- Action: 'sts:AssumeRole'
Effect: Allow
Principal:
Service: codebuild.amazonaws.com
Path: /
CodeBuildIamPolicy:
Type: 'AWS::IAM::Policy'
Properties:
PolicyName: !Sub 'CodeBuildServiceRolePolicy-${AWS::StackName}'
PolicyDocument:
Statement:
- Action:
- 's3:PutObject'
- 's3:GetObject'
- 's3:GetObjectVersion'
- 's3:ListBucket'
Effect: Allow
Resource: '*'
- Action:
- 'logs:CreateLogGroup'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
- 'ec2:CreateNetworkInterface'
- 'ec2:DescribeDhcpOptions'
- 'ec2:DescribeNetworkInterfaces'
- 'ec2:DeleteNetworkInterface'
- 'ec2:DescribeSubnets'
- 'ec2:DescribeSecurityGroups'
- 'ec2:DescribeVpcs'
- 'ec2:CreateNetworkInterfacePermission'
- 'ecr:*'
- ...
Effect: Allow
Resource:
- '*'
Roles:
- !Ref CodeBuildIamRole
CodeBuild:
DependsOn:
- CodeBuildIamPolicy
Type: "AWS::CodeBuild::Project"
Properties:
...
Hopefully this is helpful
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With