Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receive "Not authorized to perform DescribeSecurityGroups" when creating new Project in AWS CodeBuild

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.

like image 873
Jackson Avatar asked Oct 16 '18 20:10

Jackson


People also ask

What permissions does CodeBuild need?

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.

How do I assume AWS CodeBuild?

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.


1 Answers

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

like image 61
Lucas A Avatar answered Oct 07 '22 02:10

Lucas A