쿠키 기본 설정 선택

당사는 사이트와 서비스를 제공하는 데 필요한 필수 쿠키 및 유사한 도구를 사용합니다. 고객이 사이트를 어떻게 사용하는지 파악하고 개선할 수 있도록 성능 쿠키를 사용해 익명의 통계를 수집합니다. 필수 쿠키는 비활성화할 수 없지만 '사용자 지정' 또는 ‘거부’를 클릭하여 성능 쿠키를 거부할 수 있습니다.

사용자가 동의하는 경우 AWS와 승인된 제3자도 쿠키를 사용하여 유용한 사이트 기능을 제공하고, 사용자의 기본 설정을 기억하고, 관련 광고를 비롯한 관련 콘텐츠를 표시합니다. 필수가 아닌 모든 쿠키를 수락하거나 거부하려면 ‘수락’ 또는 ‘거부’를 클릭하세요. 더 자세한 내용을 선택하려면 ‘사용자 정의’를 클릭하세요.

권한 및 AWS CDK

포커스 모드
권한 및 AWS CDK - AWS Cloud Development Kit (AWS CDK) v2

v AWS CDK 2 개발자 안내서입니다. 이전 CDK v1은 2022년 6월 1일에 유지 관리에 들어갔으며 2023년 6월 1일에 지원이 종료되었습니다.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

v AWS CDK 2 개발자 안내서입니다. 이전 CDK v1은 2022년 6월 1일에 유지 관리에 들어갔으며 2023년 6월 1일에 지원이 종료되었습니다.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

AWS Construct Library는 널리 구현된 몇 가지 공통 관용어를 사용하여 액세스 및 권한을 관리합니다. IAM 모듈은 이러한 관용어를 사용하는 데 필요한 도구를 제공합니다.

AWS CDK 는 AWS CloudFormation 를 사용하여 변경 사항을 배포합니다. 모든 배포에는 AWS CloudFormation 배포를 시작하는 액터(개발자 또는 자동 시스템)가 포함됩니다. 이 작업을 수행하는 과정에서 액터는 하나 이상의 IAM 자격 증명(사용자 또는 역할)을 수임하고 선택적으로 역할을에 전달합니다 AWS CloudFormation.

AWS IAM Identity Center 를 사용하여 사용자로 인증하는 경우 Single Sign-On 공급자는 사전 정의된 IAM 역할로 작업할 수 있는 권한을 부여하는 수명이 짧은 세션 보안 인증을 제공합니다. 가 IAM Identity Center 인증에서 AWS 자격 증명을 AWS CDK 얻는 방법을 알아보려면 SDK 및 도구 참조 안내서의 IAM Identity Center 인증 이해를 참조하세요. AWS SDKs

보안 주체

IAM 보안 주체는 API를 호출할 수 있는 사용자, 서비스 또는 애플리케이션을 나타내는 인증된 AWS 엔터티입니다. AWS APIs AWS Construct Library는 AWS 리소스에 대한 액세스 권한을 부여하는 여러 가지 유연한 방법으로 보안 주체를 지정할 수 있도록 지원합니다.

보안 컨텍스트에서 ‘위탁자’라는 용어는 사용자와 같은 인증된 엔터티를 구체적으로 나타냅니다. 그룹 및 역할과 같은 객체는 사용자(및 기타 인증된 엔터티)를 나타내는 것이 아니라 권한을 부여할 목적으로 간접적으로 식별합니다.

예를 들어 IAM 그룹을 생성하는 경우 그룹(및 해당 멤버)에 Amazon RDS 테이블에 대한 쓰기 액세스 권한을 부여할 수 있습니다. 그러나 그룹 자체는 단일 엔터티를 나타내지 않으므로 위탁자가 아닙니다(그룹에 로그인할 수도 없음).

CDK의 IAM 라이브러리에서 위탁자를 직접 또는 간접적으로 식별하는 클래스는 IPrincipal 인터페이스를 구현하여 이러한 객체를 액세스 정책에서 상호 교환적으로 사용할 수 있도록 합니다. 그러나 보안 측면에서는 이 모든 것이 위탁자가 아닙니다. 이러한 객체에는 다음이 포함됩니다.

  1. Role, User, Group 등의 IAM 리소스

  2. 서비스 위탁자(new iam.ServicePrincipal('service.amazonaws.com'))

  3. 페더레이션 위탁자(new iam.FederatedPrincipal('cognito-identity.amazonaws.com'))

  4. 계정 위탁자(new iam.AccountPrincipal('0123456789012'))

  5. 정식 사용자 위탁자(new iam.CanonicalUserPrincipal('79a59d[...]7ef2be'))

  6. AWS Organizations 보안 주체(new iam.OrganizationPrincipal('org-id'))

  7. 임의 ARN 위탁자(new iam.ArnPrincipal(res.arn))

  8. 여러 위탁자를 신뢰하기 위한 iam.CompositePrincipal(principal1, principal2, ...)

권한 부여

Amazon S3 버킷 또는 Amazon DynamoDB 테이블과 같이 액세스할 수 있는 리소스를 나타내는 모든 구문에는 다른 엔터티에 대한 액세스 권한을 부여하는 메서드가 있습니다. 이러한 모든 메서드에는 grant로 시작하는 이름이 있습니다.

예를 들어 Amazon S3 버킷에는 엔터티에서 버킷으로 각각 읽기 및 읽기/쓰기 액세스를 활성화하는 메서드 grantReadgrantReadWrite(Python: grant_read, grant_read_write)가 있습니다. 엔터티는 이러한 작업을 수행하는 데 필요한 Amazon S3 IAM 권한을 정확히 알 필요가 없습니다.

grant 메서드의 첫 번째 인수는 항상 IGrantable 유형입니다. 이 인터페이스는 권한을 부여할 수 있는 엔터티를 나타냅니다. 즉, IAM 객체 Role, User, Group과 같은 역할이 있는 리소스를 나타냅니다.

다른 엔터티에도 권한을 부여할 수 있습니다. 예를 들어 이 주제의 후반부에서는 Amazon S3 버킷에 대한 CodeBuild 프로젝트 액세스 권한을 부여하는 방법을 보여줍니다. 일반적으로 연결된 역할은 액세스 권한이 부여된 엔터티의 role 속성을 통해 얻습니다.

lambda.Function과 같은 실행 역할을 사용하는 리소스도 IGrantable을 구현하므로 역할에 대한 액세스 권한을 부여하는 대신 직접 액세스할 수 있습니다. 예를 들어 bucket이 Amazon S3 버킷이고 function이 Lambda 함수인 경우 다음 코드는 함수에 버킷에 대한 읽기 액세스 권한을 부여합니다.

TypeScript
bucket.grantRead(function);
JavaScript
bucket.grantRead(function);
Python
bucket.grant_read(function)
Java
bucket.grantRead(function);
C#
bucket.GrantRead(function);
bucket.grantRead(function);

스택을 배포하는 동안 권한을 적용해야 하는 경우가 있습니다. 이러한 경우 중 하나는 AWS CloudFormation 사용자 지정 리소스에 다른 리소스에 대한 액세스 권한을 부여하는 경우입니다. 사용자 지정 리소스는 배포 중 간접적으로 호출되므로 배포 시 지정된 권한이 있어야 합니다.

또 다른 경우는 서비스가 전달한 역할에 올바른 정책이 적용되었는지 확인하는 경우입니다. (여러 AWS 서비스가 이렇게 하여 정책을 설정하는 것을 잊지 않도록 합니다.) 이러한 경우 권한이 너무 늦게 적용되는 경우 배포가 실패할 수 있습니다.

다른 리소스가 생성되기 전에 권한 부여의 권한을 강제로 적용하려면 다음과 같이 권한 부여 자체에 대한 종속성을 추가할 수 있습니다. grant 메서드의 반환 값은 일반적으로 삭제되지만 모든 grant 메서드는 실제로 iam.Grant 객체를 반환합니다.

TypeScript
const grant = bucket.grantRead(lambda); const custom = new CustomResource(...); custom.node.addDependency(grant);
JavaScript
const grant = bucket.grantRead(lambda); const custom = new CustomResource(...); custom.node.addDependency(grant);
Python
grant = bucket.grant_read(function) custom = CustomResource(...) custom.node.add_dependency(grant)
Java
Grant grant = bucket.grantRead(function); CustomResource custom = new CustomResource(...); custom.node.addDependency(grant);
C#
var grant = bucket.GrantRead(function); var custom = new CustomResource(...); custom.node.AddDependency(grant);
const grant = bucket.grantRead(lambda); const custom = new CustomResource(...); custom.node.addDependency(grant);

Roles

IAM 패키지에는 IAM 역할을 나타내는 Role 구문이 포함되어 있습니다. 다음 코드는 Amazon EC2 서비스를 신뢰하는 새 역할을 생성합니다.

TypeScript
import * as iam from 'aws-cdk-lib/aws-iam'; const role = new iam.Role(this, 'Role', { assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com'), // required });
JavaScript
const iam = require('aws-cdk-lib/aws-iam'); const role = new iam.Role(this, 'Role', { assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com') // required });
Python
import aws_cdk.aws_iam as iam role = iam.Role(self, "Role", assumed_by=iam.ServicePrincipal("ec2.amazonaws.com")) # required
Java
import software.amazon.awscdk.services.iam.Role; import software.amazon.awscdk.services.iam.ServicePrincipal; Role role = Role.Builder.create(this, "Role") .assumedBy(new ServicePrincipal("ec2.amazonaws.com")).build();
C#
using Amazon.CDK.AWS.IAM; var role = new Role(this, "Role", new RoleProps { AssumedBy = new ServicePrincipal("ec2.amazonaws.com"), // required });
import * as iam from 'aws-cdk-lib/aws-iam'; const role = new iam.Role(this, 'Role', { assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com'), // required });

역할의 addToPolicy 메서드(Python: add_to_policy)를 직접적으로 호출하고 추가할 규칙을 정의하는 PolicyStatement를 전달하여 역할에 권한을 추가할 수 있습니다. 문이 역할의 기본 정책에 추가됩니다. 문이 없는 경우 문이 생성됩니다.

다음 예에서는 권한 있는 서비스가 AWS CodeBuild라는 조건하에 리소스 bucketotherRole(Python: other_role)에 대한 ec2:SomeActions3:AnotherAction 작업에 대한 Deny 정책 문을 역할에 추가합니다.

TypeScript
role.addToPolicy(new iam.PolicyStatement({ effect: iam.Effect.DENY, resources: [bucket.bucketArn, otherRole.roleArn], actions: ['ec2:SomeAction', 's3:AnotherAction'], conditions: {StringEquals: { 'ec2:AuthorizedService': 'codebuild.amazonaws.com', }}}));
JavaScript
role.addToPolicy(new iam.PolicyStatement({ effect: iam.Effect.DENY, resources: [bucket.bucketArn, otherRole.roleArn], actions: ['ec2:SomeAction', 's3:AnotherAction'], conditions: {StringEquals: { 'ec2:AuthorizedService': 'codebuild.amazonaws.com' }}}));
Python
role.add_to_policy(iam.PolicyStatement( effect=iam.Effect.DENY, resources=[bucket.bucket_arn, other_role.role_arn], actions=["ec2:SomeAction", "s3:AnotherAction"], conditions={"StringEquals": { "ec2:AuthorizedService": "codebuild.amazonaws.com"}} ))
Java
role.addToPolicy(PolicyStatement.Builder.create() .effect(Effect.DENY) .resources(Arrays.asList(bucket.getBucketArn(), otherRole.getRoleArn())) .actions(Arrays.asList("ec2:SomeAction", "s3:AnotherAction")) .conditions(java.util.Map.of( // Map.of requires Java 9 or later "StringEquals", java.util.Map.of( "ec2:AuthorizedService", "codebuild.amazonaws.com"))) .build());
C#
role.AddToPolicy(new PolicyStatement(new PolicyStatementProps { Effect = Effect.DENY, Resources = new string[] { bucket.BucketArn, otherRole.RoleArn }, Actions = new string[] { "ec2:SomeAction", "s3:AnotherAction" }, Conditions = new Dictionary<string, object> { ["StringEquals"] = new Dictionary<string, string> { ["ec2:AuthorizedService"] = "codebuild.amazonaws.com" } } }));
role.addToPolicy(new iam.PolicyStatement({ effect: iam.Effect.DENY, resources: [bucket.bucketArn, otherRole.roleArn], actions: ['ec2:SomeAction', 's3:AnotherAction'], conditions: {StringEquals: { 'ec2:AuthorizedService': 'codebuild.amazonaws.com', }}}));

앞의 예에서는 addToPolicy(Python: add_to_policy) 직접 호출로 새 PolicyStatement 인라인을 생성했습니다. 기존 정책 문 또는 수정한 정책 문을 전달할 수도 있습니다. PolicyStatement 객체에는 위탁자, 리소스, 조건 및 작업을 추가하는 다양한 메서드가 있습니다.

역할이 올바르게 작동해야 하는 구문을 사용하는 경우 다음 중 하나를 수행할 수 있습니다.

  • 구문 객체를 인스턴스화할 때 기존 역할을 전달합니다.

  • 적절한 서비스 위탁자를 신뢰하여 구문이 새 역할을 생성하도록 합니다. 다음 예에서는 CodeBuild 프로젝트와 같은 구문을 사용합니다.

TypeScript
import * as codebuild from 'aws-cdk-lib/aws-codebuild'; // imagine roleOrUndefined is a function that might return a Role object // under some conditions, and undefined under other conditions const someRole: iam.IRole | undefined = roleOrUndefined(); const project = new codebuild.Project(this, 'Project', { // if someRole is undefined, the Project creates a new default role, // trusting the codebuild.amazonaws.com service principal role: someRole, });
JavaScript
const codebuild = require('aws-cdk-lib/aws-codebuild'); // imagine roleOrUndefined is a function that might return a Role object // under some conditions, and undefined under other conditions const someRole = roleOrUndefined(); const project = new codebuild.Project(this, 'Project', { // if someRole is undefined, the Project creates a new default role, // trusting the codebuild.amazonaws.com service principal role: someRole });
Python
import aws_cdk.aws_codebuild as codebuild # imagine role_or_none is a function that might return a Role object # under some conditions, and None under other conditions some_role = role_or_none(); project = codebuild.Project(self, "Project", # if role is None, the Project creates a new default role, # trusting the codebuild.amazonaws.com service principal role=some_role)
Java
import software.amazon.awscdk.services.iam.Role; import software.amazon.awscdk.services.codebuild.Project; // imagine roleOrNull is a function that might return a Role object // under some conditions, and null under other conditions Role someRole = roleOrNull(); // if someRole is null, the Project creates a new default role, // trusting the codebuild.amazonaws.com service principal Project project = Project.Builder.create(this, "Project") .role(someRole).build();
C#
using Amazon.CDK.AWS.CodeBuild; // imagine roleOrNull is a function that might return a Role object // under some conditions, and null under other conditions var someRole = roleOrNull(); // if someRole is null, the Project creates a new default role, // trusting the codebuild.amazonaws.com service principal var project = new Project(this, "Project", new ProjectProps { Role = someRole });
import * as codebuild from 'aws-cdk-lib/aws-codebuild'; // imagine roleOrUndefined is a function that might return a Role object // under some conditions, and undefined under other conditions const someRole: iam.IRole | undefined = roleOrUndefined(); const project = new codebuild.Project(this, 'Project', { // if someRole is undefined, the Project creates a new default role, // trusting the codebuild.amazonaws.com service principal role: someRole, });

객체가 생성되면 역할(역할이 전달되었는지 또는 구문에서 생성한 기본 역할인지 여부)을 속성 role로 사용할 수 있습니다. 하지만 이 속성은 외부 리소스에서는 사용할 수 없습니다. 따라서 이러한 구문에는 addToRolePolicy(Python: add_to_role_policy) 메서드가 있습니다.

구문이 외부 리소스인 경우 메서드는 아무것도 수행하지 않으며, 그렇지 않으면 role 속성의 addToPolicy(Python: add_to_policy) 메서드를 직접적으로 호출합니다. 이렇게 하면 정의되지 않은 사례를 명시적으로 처리하는 데 따른 문제를 줄일 수 있습니다.

다음 예에서 다음을 보여줍니다.

TypeScript
// project is imported into the CDK application const project = codebuild.Project.fromProjectName(this, 'Project', 'ProjectName'); // project is imported, so project.role is undefined, and this call has no effect project.addToRolePolicy(new iam.PolicyStatement({ effect: iam.Effect.ALLOW, // ... and so on defining the policy }));
JavaScript
// project is imported into the CDK application const project = codebuild.Project.fromProjectName(this, 'Project', 'ProjectName'); // project is imported, so project.role is undefined, and this call has no effect project.addToRolePolicy(new iam.PolicyStatement({ effect: iam.Effect.ALLOW // ... and so on defining the policy }));
Python
# project is imported into the CDK application project = codebuild.Project.from_project_name(self, 'Project', 'ProjectName') # project is imported, so project.role is undefined, and this call has no effect project.add_to_role_policy(iam.PolicyStatement( effect=iam.Effect.ALLOW, # ... and so on defining the policy )
Java
// project is imported into the CDK application Project project = Project.fromProjectName(this, "Project", "ProjectName"); // project is imported, so project.getRole() is null, and this call has no effect project.addToRolePolicy(PolicyStatement.Builder.create() .effect(Effect.ALLOW) // .. and so on defining the policy .build();
C#
// project is imported into the CDK application var project = Project.FromProjectName(this, "Project", "ProjectName"); // project is imported, so project.role is null, and this call has no effect project.AddToRolePolicy(new PolicyStatement(new PolicyStatementProps { Effect = Effect.ALLOW, // ... and so on defining the policy }));
// project is imported into the CDK application const project = codebuild.Project.fromProjectName(this, 'Project', 'ProjectName'); // project is imported, so project.role is undefined, and this call has no effect project.addToRolePolicy(new iam.PolicyStatement({ effect: iam.Effect.ALLOW, // ... and so on defining the policy }));

리소스 정책

Amazon S3 버킷 및 IAM 역할과 AWS같은의 일부 리소스에도 리소스 정책이 있습니다. 이러한 구문에는 인수로 PolicyStatement를 사용하는 addToResourcePolicy 메서드(Python: add_to_resource_policy)가 있습니다. 리소스 정책에 추가된 모든 정책 문은 하나 이상의 위탁자를 지정해야 합니다.

다음 예에서 Amazon S3 버킷 bucket은 자신에게 s3:SomeAction 권한이 있는 역할을 부여합니다.

TypeScript
bucket.addToResourcePolicy(new iam.PolicyStatement({ effect: iam.Effect.ALLOW, actions: ['s3:SomeAction'], resources: [bucket.bucketArn], principals: [role] }));
JavaScript
bucket.addToResourcePolicy(new iam.PolicyStatement({ effect: iam.Effect.ALLOW, actions: ['s3:SomeAction'], resources: [bucket.bucketArn], principals: [role] }));
Python
bucket.add_to_resource_policy(iam.PolicyStatement( effect=iam.Effect.ALLOW, actions=["s3:SomeAction"], resources=[bucket.bucket_arn], principals=role))
Java
bucket.addToResourcePolicy(PolicyStatement.Builder.create() .effect(Effect.ALLOW) .actions(Arrays.asList("s3:SomeAction")) .resources(Arrays.asList(bucket.getBucketArn())) .principals(Arrays.asList(role)) .build());
C#
bucket.AddToResourcePolicy(new PolicyStatement(new PolicyStatementProps { Effect = Effect.ALLOW, Actions = new string[] { "s3:SomeAction" }, Resources = new string[] { bucket.BucketArn }, Principals = new IPrincipal[] { role } }));
bucket.addToResourcePolicy(new iam.PolicyStatement({ effect: iam.Effect.ALLOW, actions: ['s3:SomeAction'], resources: [bucket.bucketArn], principals: [role] }));

외부 IAM 객체 사용

AWS CDK 앱 외부에서 IAM 사용자, 보안 주체, 그룹 또는 역할을 정의한 경우 AWS CDK 앱에서 해당 IAM 객체를 사용할 수 있습니다. 이렇게 하려면 ARN 또는 이름을 사용하여 참조를 생성합니다. (사용자, 그룹 및 역할의 이름을 사용합니다.) 그런 다음 반환된 참조를 사용하여 권한을 부여하거나 앞서 설명한 대로 정책 문을 구성할 수 있습니다.

정책(관리형 정책 포함)은 다음 방법을 사용하여 유사한 방식으로 사용할 수 있습니다. IAM 정책이 필요한 모든 곳에서 이러한 객체에 대한 참조를 사용할 수 있습니다.

참고

외부 AWS 리소스에 대한 모든 참조와 마찬가지로 CDK 앱에서 외부 IAM 객체를 수정할 수 없습니다.

이 페이지에서

프라이버시사이트 이용 약관쿠키 기본 설정
© 2025, Amazon Web Services, Inc. 또는 계열사. All rights reserved.