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
IAM and select IAM from the results dropdown.

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.

CodeBuildServicePolicy.
Step 2: Create IAM Role for CodeBuild


CodeBuildServicePolicy you just created and select it.
CodeBuildServiceRole.
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
IAM and select IAM from the results dropdown.

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.

CodeDeployServicePolicy.
Step 2: Create IAM Role for CodeDeploy



CodeDeployServiceRole.

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
On the left navigation pane of the IAM console, choose Roles, then search for and select your newly created CodeDeployServiceRole.
Under the Permissions tab, click Add permissions and select Attach policies.

CodeDeployServicePolicy you created earlier.

Step 4: Edit Trust relationships

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": [
"codedeploy.amazonaws.com",
"codepipeline.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}
