Create Service Role

Create Service Role for CodeBuild

First, we need to create a custom IAM policy for CodeBuild, and then attach it to a new IAM role.

Step 1: Create IAM Policy for CodeBuild

  1. In the AWS Management Console search bar, enter IAM and select IAM from the results dropdown.

Service Role

  1. On the left navigation pane, choose Policies, then click Create policy.

Service Role

  1. In the Specify permissions screen:
    • Switch the policy editor to the JSON tab.
    • Paste the following JSON policy into the editor:

Be sure to replace [YOUR_BUCKET_NAME] with your actual Amazon S3 bucket name where the artifacts are stored.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "CloudWatchLogsAccess",
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": "*"
        },
        {
            "Sid": "S3ArtifactAccess",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:GetObjectVersion",
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::[YOUR_BUCKET_NAME]",
                "arn:aws:s3:::[YOUR_BUCKET_NAME]/*"
            ]
        }
    ]
}

This custom policy grants CodeBuild the necessary permissions to write build logs to Amazon CloudWatch and access artifacts stored in Amazon S3. Using a custom policy follows the principle of least privilege, which is safer than using full access managed policies.

Service Role

  1. Click Next.
  2. On the Review and create screen:
    • For Policy name, enter CodeBuildServicePolicy.
    • Click Create policy.

Service Role

Step 2: Create IAM Role for CodeBuild

  1. On the left navigation pane of the IAM console, choose Roles, then click Create role.

Service Role

  1. For Select trusted entity:
    • Trusted entity type: select AWS service.
    • Use case: select CodeBuild.
    • Click Next.

Service Role

  1. For Add permissions:
    • Search for the CodeBuildServicePolicy you just created and select it.
    • Click Next.

Service Role

  1. For Name, review, and create:
    • Role name: enter CodeBuildServiceRole.
    • Click Create role.

Service Role

Create Service Role for CodeDeploy

First, we need to create a custom IAM policy for CodeDeploy, and then attach it to a new IAM role.

Step 1: Create IAM Policy for CodeDeploy

  1. In the AWS Management Console search bar, enter IAM and select IAM from the results dropdown.

Service Role

  1. On the left navigation pane, choose Policies, then click Create policy.

Service Role

  1. In the Specify permissions screen:
    • Switch the policy editor to the JSON tab.
    • Paste the following JSON policy into the editor:

Be sure to replace [YOUR_BUCKET_NAME] with your actual Amazon S3 bucket name where the artifacts are stored, and change us-east-1 to your selected region.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "CloudWatchLogsPermissions",
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": "*"
        },
        {
            "Sid": "CodeDeployPipelinePermissions",
            "Effect": "Allow",
            "Action": [
                "codedeploy:CreateDeployment",
                "codedeploy:CreateDeploymentGroup",
                "codedeploy:GetDeploymentGroup",
                "codedeploy:BatchGetDeploymentGroups",
                "codedeploy:GetDeploymentConfig",
                "codedeploy:RegisterApplicationRevision",
                "codedeploy:GetApplicationRevision",
                "codedeploy:GetDeployment"
            ],
            "Resource": "*"
        },
        {
            "Sid": "CodeCommitPermissions",
            "Effect": "Allow",
            "Action": [
                "codecommit:GetBranch",
                "codecommit:GetCommit",
                "codecommit:UploadArchive",
                "codecommit:ListBranches",
                "codecommit:GetFile",
                "codecommit:GetFolder",
                "codecommit:GetRepository",
                "codecommit:GetTree",
                "codecommit:GitPull",
                "codecommit:GetUploadArchiveStatus"
            ],
            "Resource": "*"
        },
        {
            "Sid": "S3BucketPermissions",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:GetObjectVersion",
                "s3:ListBucket",
                "s3:ListAllMyBuckets"
            ],
            "Resource": "*"
        },
        {
            "Sid": "CodeBuildPermissions",
            "Effect": "Allow",
            "Action": [
                "codebuild:StartBuild",
                "codebuild:ListBuilds",
                "codebuild:ListProjects",
                "codebuild:StopBuild",
                "codebuild:PutResourcePolicy",
                "codebuild:BatchGetBuilds"
            ],
            "Resource": "*"
        },
        {
            "Sid": "KMSKKeyPermissions",
            "Effect": "Allow",
            "Action": [
                "kms:DescribeKey",
                "kms:GenerateDataKey*",
                "kms:Encrypt",
                "kms:Decrypt"
            ],
            "Resource": "*"
        }
    ]
}

This custom policy grants the CodeDeploy service permissions to access other AWS services that are used in the CodeDeploy and CodePipeline workflow, such as Amazon S3, CloudWatch Logs, and Amazon EC2.

Service Role

  1. Click Next.
  2. On the Review and create screen:
    • For Policy name, enter CodeDeployServicePolicy.
    • Click Create policy.

Service Role

Step 2: Create IAM Role for CodeDeploy

  1. On the left navigation pane of the IAM console, choose Roles, then click Create role.

Service Role

  1. For Select trusted entity:
    • Trusted entity type: select AWS service.
    • Use case: select CodeDeploy.
    • Click Next.

Service Role

  1. For Add permissions, the permission AWSCodeDeployRole auto selected
    • Click Next.

Service Role

  1. For Name, review, and create:
    • Role name: enter CodeDeployServiceRole.
    • Click Create role.

Service Role Service Role

The AWSCodeDeployRole managed policy provides the base permissions required for CodeDeploy to read tags on instances or identify instances by Auto Scaling group names.

Step 3: Attach Custom Policy to the Role

  1. On the left navigation pane of the IAM console, choose Roles, then search for and select your newly created CodeDeployServiceRole.

  2. Under the Permissions tab, click Add permissions and select Attach policies.

Service Role

  1. Search for the CodeDeployServicePolicy you created earlier.
  2. Select the policy and click Add permissions.

Service Role Service Role

Step 4: Edit Trust relationships

  1. Still in the CodeDeployServiceRole page, switch to the Trust relationships tab and click Edit trust policy.

Service Role

  1. Update the policy to include codepipeline.amazonaws.com as follows:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "",
            "Effect": "Allow",
            "Principal": {
                "Service": [
                    "codedeploy.amazonaws.com",
                    "codepipeline.amazonaws.com"
                ]
            },
            "Action": "sts:AssumeRole"
        }
    ]
}
  1. Click Update policy.

Service Role Service Role