> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo IAM and Cross Service Permissions for CodeArtifact and S3

> Explains IAM, resource, KMS, and organization permission layers for cross-service and cross-account artifact access with troubleshooting for CodePipeline CodeBuild CodeArtifact S3.

In this lesson we review common IAM and resource-permission patterns for cross-service and cross-account artifact access: CodePipeline, CodeBuild, CodeDeploy, CodeArtifact, S3, and KMS. Permission-related build failures are a frequent real-world and certification-exam scenario, so it’s useful to understand the permission layers and the order in which to inspect them when access is denied.

## Permission model summary

* Identity-based policies: IAM policies attached to users, groups, or roles.
* Resource-based policies: S3 bucket policies, CodeArtifact repository/domain policies, SQS/SNS policies.
* KMS key policies (and grants): control who can encrypt/decrypt data keys for customer-managed keys.
* Organization-level controls: Service Control Policies (SCPs) and organization-wide resource settings (for example, S3 Block Public Access) that can restrict or override individual account permissions.

| Layer | What it controls | Typical places to inspect |
| - | - | - |
| Identity-based | What a principal (user/role) is allowed to call | IAM console: role/user policies, permission boundaries |
| Resource-based | Which principals a resource explicitly allows | S3 bucket policy, CodeArtifact repository/domain policy |
| KMS key policy | Which principals can use a customer-managed key | KMS console: key policy & grants |
| Organization-level controls | Policies and settings that apply across accounts/OUs | AWS Organizations: SCPs, organization S3 Block Public Access |
| Network-level controls | Endpoint-level restrictions | VPC endpoint policies |

Important distinction: SCPs limit what principals in an OU/account can do. Organization-level resource settings (for example, organization S3 Block Public Access) can also block resource-based permissions—so even a correct bucket policy might not take effect if an organization restriction prevents it.

Cross-account access typically requires both an identity-based policy (on the principal) and a resource-based policy (on the target resource such as S3 or CodeArtifact). If an encrypted artifact cannot be accessed, verify the KMS key policy as an additional permission layer.

## S3 artifacts used by CodePipeline

CodePipeline stores pipeline artifacts in S3 buckets during execution. For single-account pipelines, the pipeline service role plus appropriate IAM permissions often suffice. For cross-account pipelines or cross-account consumers, add an explicit S3 bucket policy to grant access to principals in other accounts.

When inspecting an artifact bucket, verify:

* Server-side encryption and whether a customer-managed KMS key is in use.
* The bucket policy for explicit cross-account grants.
* Block Public Access settings and any organization-wide restrictions.

![A screenshot of the Amazon S3 console open to a bucket's Permissions page. It shows a green banner saying Block Public Access settings were edited and the "Block all public access" setting turned on, with sections for bucket policy and permissions overview.](https://mintcdn.com/kodekloud-c4ac6d9a/Kx09awxrzT0QVPGP/images/AWS-Certified-DevOps-Engineer-Professional/Domain-1-SDLC-Automation/Demo-IAM-and-Cross-Service-Permissions-for-CodeArtifact-and-S3/s3-permissions-block-all-public-access.jpg?fit=max\&auto=format\&n=Kx09awxrzT0QVPGP\&q=85\&s=fb895f33c81f59b289e4c35b8a40d3f4)

A bucket policy is where you explicitly allow a role or account to access objects. A minimal cross-account grant (conceptual) can look like:

```json theme={null}
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowCrossAccountPipeline",
      "Effect": "Allow",
      "Principal": { "AWS": "arn:aws:iam::123456789012:role/CrossAccountPipelineRole" },
      "Action": [
        "s3:GetObject",
        "s3:PutObject",
        "s3:ListBucket"
      ],
      "Resource": [
        "arn:aws:s3:::my-pipeline-artifacts-bucket",
        "arn:aws:s3:::my-pipeline-artifacts-bucket/*"
      ]
    }
  ]
}
```

Be mindful that organization-level controls (SCPs or organization S3 Block Public Access) can prevent adding or enabling such bucket policies. Use least privilege—grant only the actions and resources required.

## CodePipeline service role and referencing it

When you create a pipeline, you select or create a service role that CodePipeline assumes to perform actions (start builds, read/write S3 artifacts, invoke CodeDeploy, etc.). Inspect the role in the IAM console to review its attached policies and trust relationship.

![A screenshot of the AWS CodePipeline "Create new pipeline" settings page showing a pipeline named "test", the execution mode set to "Queued", and the option to use an existing service role with a Role ARN dropdown open. The page includes navigation steps on the left and Next/Previous buttons at the bottom.](https://mintcdn.com/kodekloud-c4ac6d9a/Kx09awxrzT0QVPGP/images/AWS-Certified-DevOps-Engineer-Professional/Domain-1-SDLC-Automation/Demo-IAM-and-Cross-Service-Permissions-for-CodeArtifact-and-S3/aws-codepipeline-create-pipeline-queued-role.jpg?fit=max\&auto=format\&n=Kx09awxrzT0QVPGP\&q=85\&s=f50a98d77bfd67f39ce2ecfe0f08c6b1)

You can inspect that role in IAM to see which policies are attached and its trust relationship.

![A screenshot of the AWS Identity and Access Management (IAM) console showing details for a role named "devops-020-350-pipeline-825f61a8," including its ARN, creation date, and a list of attached permissions policies (base-permissions, codebuild-access, codedeploy-access, codestar-connections). The left sidebar shows IAM navigation items like Roles, Users, and Policies.](https://mintcdn.com/kodekloud-c4ac6d9a/Kx09awxrzT0QVPGP/images/AWS-Certified-DevOps-Engineer-Professional/Domain-1-SDLC-Automation/Demo-IAM-and-Cross-Service-Permissions-for-CodeArtifact-and-S3/aws-iam-role-devops-pipeline-825f61a8.jpg?fit=max\&auto=format\&n=Kx09awxrzT0QVPGP\&q=85\&s=b6cd5baa402ea42ca077a986e248c630)

Example trust relationship that allows CodePipeline to assume the role:

```json theme={null}
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": { "Service": "codepipeline.amazonaws.com" },
      "Action": "sts:AssumeRole"
    }
  ]
}
```

The pipeline role (and any downstream service roles) should have policies that enable CodePipeline to:

* Read/write to the S3 artifact bucket(s).
* Start CodeBuild builds and batch builds.
* Create and manage CodeDeploy deployments.
* Pass roles to downstream services using `iam:PassRole` if required.

## Common role permission examples

These example policy snippets illustrate typical permissions you’ll see attached to build/deploy roles. They are for clarity, not production-ready complete policies.

ECR push/pull permissions (for building or pushing container images):

```json theme={null}
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "ECRAuth",
      "Effect": "Allow",
      "Action": "ecr:GetAuthorizationToken",
      "Resource": "*"
    },
    {
      "Sid": "ECRPushPull",
      "Effect": "Allow",
      "Action": [
        "ecr:BatchCheckLayerAvailability",
        "ecr:GetDownloadUrlForLayer",
        "ecr:BatchGetImage",
        "ecr:PutImage",
        "ecr:InitiateLayerUpload",
        "ecr:UploadLayerPart",
        "ecr:CompleteLayerUpload"
      ],
      "Resource": "arn:aws:ecr:us-east-1:598274344262:repository/devops-020-350-*"
    }
  ]
}
```

Systems Manager Parameter Store access:

```json theme={null}
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "SSMParameterAccess",
      "Effect": "Allow",
      "Action": [
        "ssm:GetParameter",
        "ssm:GetParameters",
        "ssm:GetParametersByPath"
      ],
      "Resource": "arn:aws:ssm:us-east-1:598274344262:parameter/devops-020-350/*"
    }
  ]
}
```

Deployment role actions (Auto Scaling, ELB, EC2, CloudWatch, SNS, etc.) are commonly granted to deployment/service roles (truncated for clarity):

```json theme={null}
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "DeploymentActions",
      "Effect": "Allow",
      "Action": [
        "autoscaling:CreateAutoScalingGroup",
        "autoscaling:UpdateAutoScalingGroup",
        "autoscaling:PutLifecycleHook",
        "autoscaling:CompleteLifecycleAction",
        "elasticloadbalancing:DescribeLoadBalancers",
        "elasticloadbalancing:RegisterTargets",
        "elasticloadbalancing:DeregisterTargets",
        "ec2:TerminateInstances",
        "cloudwatch:PutMetricAlarm",
        "sns:Publish",
        "tag:GetResources"
      ],
      "Resource": "*"
    }
  ]
}
```

## CodeArtifact repository and domain policies

CodeArtifact supports repository and domain policies as resource-based policies. For cross-account publishing or reading, add statements in the repository or domain policy to grant another account’s role the ability to read or publish packages.

Example repository policy (conceptual placeholders for other-account roles):

```json theme={null}
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "ReadAccess",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::DEV_ACCOUNT_ID:role/CodeBuildServiceRole"
      },
      "Action": [
        "codeartifact:ReadFromRepository",
        "codeartifact:GetRepositoryEndpoint",
        "codeartifact:ListPackages",
        "codeartifact:ListPackageVersions",
        "codeartifact:DescribePackageVersion",
        "codeartifact:GetPackageVersionReadme",
        "codeartifact:GetPackageVersionAssets"
      ],
      "Resource": "*"
    },
    {
      "Sid": "PublishAccess",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::CENTRAL_ACCOUNT_ID:role/PackagePublisherRole"
      },
      "Action": [
        "codeartifact:PublishPackageVersion",
        "codeartifact:PutPackageMetadata"
      ],
      "Resource": "*"
    }
  ]
}
```

## KMS key policy considerations

If S3 artifacts or CodeArtifact repositories use a customer-managed KMS key, the key policy (and any grants) must include the principals (roles) that need to Encrypt/Decrypt and generate data keys. A missing KMS policy entry will cause access to fail, even when IAM and resource policies appear correct.

Example key policy statement granting CI/CD roles usage permissions (conceptual):

```json theme={null}
{
  "Sid": "AllowBuildAndPipelineRoles",
  "Effect": "Allow",
  "Principal": {
    "AWS": [
      "arn:aws:iam::598274344262:role/devops-020-350-build-825f61a8",
      "arn:aws:iam::598274344262:role/devops-020-350-pipeline-825f61a8"
    ]
  },
  "Action": [
    "kms:Encrypt",
    "kms:Decrypt",
    "kms:ReEncrypt*",
    "kms:GenerateDataKey*",
    "kms:DescribeKey"
  ],
  "Resource": "*"
}
```

## Troubleshooting checklist

When you encounter permission errors for artifact access or build/deploy actions, check these layers in order:

1. IAM policy (identity-based) — does the role/user have the required actions?
2. Resource policy (S3 bucket policy, CodeArtifact repository/domain policy) — is the resource allowing the principal?
3. KMS key policy and grants — if encrypted with a customer-managed key, does the key allow the principal to decrypt/generate data keys?
4. SCPs (Service Control Policies) — are organization-level denies preventing the action?
5. Organization-level resource settings — are organization-wide restrictions (e.g., S3 Block Public Access) blocking changes?
6. VPC endpoint policies — if using VPC endpoints, does the endpoint policy restrict access?
7. Permission boundaries — is a permissions boundary limiting what the role can do?

Use the following quick-reference table to decide where to look first depending on the symptom:

| Symptom | Likely first place to check |
| - | - |
| "AccessDenied" when calling an AWS API | IAM role policy / permission boundary |
| Can't read/write S3 objects across accounts | S3 bucket policy and bucket ACL (also check organization Block Public Access) |
| Encrypted object access failing | KMS key policy and grants |
| Cross-account package publish/read failing | CodeArtifact repository/domain policy |
| API blocked despite correct policies | SCPs or organization-level settings |

When cross-service or cross-account artifact access fails, it’s common to miss the resource-based policy or KMS key policy. Start with the IAM policy, then move outward through resource policies, KMS, VPC endpoints, and organization policies.

## Other considerations

* Principle of least privilege: grant only the actions and resources required.
* PassRole: If one AWS service invokes another (for example, CodePipeline invokes CodeBuild), ensure the invoking service has `iam:PassRole` permission to pass the downstream execution role.
* Managed vs. custom policies: AWS managed policies are convenient, but for production CI/CD pipelines scoped custom policies improve security and clarity.
* Auditing: Use CloudTrail and service-specific logs (CodeBuild logs, CodePipeline execution history) to identify which principal and API call failed.

## Summary

Multiple permission layers affect artifact retrieval and deployments: identity policies, resource policies, KMS key policies, and organization controls (SCPs and organization-level resource settings). When diagnosing permission issues, inspect pipeline and build/deploy roles, trust relationships, attached IAM policies, resource policies (S3/CodeArtifact), KMS key policies, and organization settings in that order.

## Links and references

* [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/iam/)
* [Amazon S3 bucket policies and ACLs](https://docs.aws.amazon.com/AmazonS3/latest/userguide/using-iam-policies.html)
* [AWS CodePipeline user guide](https://docs.aws.amazon.com/codepipeline/latest/userguide/welcome.html)
* [AWS CodeArtifact documentation](https://docs.aws.amazon.com/codeartifact/latest/ug/what-is-codeartifact.html)
* [AWS KMS key policies and grants](https://docs.aws.amazon.com/kms/latest/developerguide/key-policies.html)
* [AWS Organizations: Service Control Policies](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies_scps.html)

> **[Watch Video](https://learn.kodekloud.com/user/courses/aws-certified-devops-engineer-professional/module/199ecdf9-a58f-4c2d-b9c9-eac36de111cd/lesson/343c5eb5-95f3-49dd-b757-f199ce34b112)**

> **[Practice Lab](https://learn.kodekloud.com/user/courses/aws-certified-devops-engineer-professional/module/199ecdf9-a58f-4c2d-b9c9-eac36de111cd/lesson/4efa9ac3-c56f-4223-add8-e519659e5e21)**


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.