管理 AWS 组织、组织单位 (OU)、账户和服务控制策略 (SCP)。
项目描述
CDK 组织
管理 AWS 组织、组织单位 (OU)、账户和服务控制策略 (SCP)。
特征:
安装
打字稿
npm install @pepperize/cdk-organizations
或者
yarn add @pepperize/cdk-organizations
Python
pip install pepperize.cdk-organizations
C#/.Net
dotnet add package Pepperize.CDK.Organizations
爪哇
<dependency>
<groupId>com.pepperize</groupId>
<artifactId>cdk-organizations</artifactId>
<version>${cdkOrganizations.version}</version>
</dependency>
贡献
欢迎各种贡献:火箭:查看我们的贡献者指南。
要快速入门,请查看开发环境:
git clone git@github.com:pepperize/cdk-organizations
cd cdk-organizations
# install dependencies
yarn
# build with projen
yarn build
入门
-
创建一个新账户
-
准备一个 IAM 用户
AdministratorAccess要部署新组织,您必须使用 AccessKey 创建管理员
-
使用projen创建一个新的 CDK TypeScript App 项目
mkdir my-project cd my-project git init -b main npx projen new awscdk-app-ts -
添加
@pepperize/cdk-organizations到您的依赖项.projenrc.jsconst project = new awscdk.AwsCdkTypeScriptApp({ //... deps: ["@pepperize/cdk-organizations"], });
-
安装依赖
npx projen
-
创建堆栈
import { Account, Organization, OrganizationalUnit } from "@pepperize/cdk-organizations"; import { Stack } from "aws-cdk-lib"; export class OrganizationStack extends Stack { constructor(scope: Construct, id: string, props: StackProps = {}) { super(scope, id, props); // Create your organization const organization = new Organization(stack, "Organization", {}); // Create an organizational unit (OU) const organizationUnit = new OrganizationalUnit(stack, "OrganizationalUnit", { organizationalUnitName: "MyFirstOU", parent: organization.root, }); // Create an account const account = new Account(stack, "Account", { accountName: "MyFirstAccount", email: "<your email for the member account>", parent: organizationUnit, }); } }
-
配置您的 AWS CLI 以进行部署
最简单的方法是导出您的访问密钥
export AWS_ACCESS_KEY_ID=<your created access key id> export AWS_SECRET_ACCESS_KEY=<your created secret access key>
-
部署您的第一个 AWS 组织
export CDK_DEFAULT_REGION=<your AWS region> export CDK_DEFAULT_ACCOUNT=<your AWS account id>
yarn deploy
用法
组织
要创建新组织或导入现有组织,请将以下构造添加到您的堆栈中:
const organization = new Organization(stack, "Organization", {
featureSet: FeatureSet.ALL, // (default) required later on to enable SCPs, enable AWS services or delegate an adminsitrator account
});
organization.root; // The organization's root is automatically created
FeatureSet.ALL是服务控制策略 (SCP) 等高级功能所必需的,并且是使用 AWS Organizations 的首选方式- 部署堆栈的帐户将自动成为新组织的管理帐户。
- 如果一个组织已经存在,它将被自动导入。
importOnDuplicate: false您可以通过传入道具来禁用此行为。 - 如果从堆栈中删除该构造,则该组织将保留并且必须手动删除。要删除组织,您必须事先从组织中删除所有成员账户、OU 和策略。通过删除主账户来删除组织
- 创建新组织时,系统会自动为您创建组织根。
见组织
组织负责人
要检索成员账户中的 AWS IAM 组织委托人,请将以下内容添加到任何构造:
const organization = Organization.of(scope, "Organization");
organization.principal; // The AWS IAM organization principal
- 此帮助器构造可用于组织中的任何成员帐户。
请参阅AWS 组织 API 参考 - DescribeOrganization
组织单位 (OU)
要创建新的组织单位 (OU),请将以下构造添加到您的堆栈中:
const organizationUnit = new OrganizationalUnit(stack, "Organization", {
organizationalUnitName: "Project2",
parent: organization.root,
});
- 组织单位 (OU) 的父级可以是组织的根,也可以是组织内的另一个 OU。
- 无法移动组织单位 (OU)。您必须先创建一个新的 OU,移动所有帐户,然后删除旧的 OU。
- 要删除组织单位 (OU),您必须先将所有帐户移出 OU 和任何子 OU,然后才能删除子 OU。删除组织单位
组织单位 (OU) 属性
importOnDuplicate如果父级中存在同名的组织单位 (OU),则会将其导入。removalPolicy默认RemovalPolicy.Retain如果您设置removalPolicy为RemovalPolicy.destroy,组织单位 (OU) 将在 Cloudformation 删除事件时被删除。
帐户
要创建一个新帐户,请将以下构造添加到您的堆栈中:
new Account(stack, "Account", {
accountName: "MyAccount",
email: "info@pepperize.com",
parent: organization.root,
});
- 电子邮件地址不得已与其他 AWS 账户关联。您可以为电子邮件地址添加后缀,即
info+account-123456789012@pepperize.com. - AWS Organizations 仅支持创建一个账户
IN_PROGRESS。确保使用account2.node.addDependency(account1)依赖关系创建帐户。 - 如果父级是组织单位 (OU),则将创建一个帐户并将其移至父级。
- 只能从主账户中创建账户。
帐户属性
importOnDuplicate如果组织中存在具有相同电子邮件地址的帐户,它将被导入。removalPolicy默认RemovalPolicy.Retain如果您设置removalPolicy为RemovalPolicy.destroy,帐户将被关闭。关闭 AWS 账户iamUserAccessToBilling默认IamUserAccessToBilling.ALLOW如果您设置iamUserAccessToBilling为ALLOW,则具有适当权限的 IAM 用户和角色可以查看账户的账单信息。roleName默认OrganizationAccountAccessRole是在新创建的账户中进行预配置,并在新成员账户中授予主账户中的用户管理员权限。
委派管理员
兼容的 AWS 服务(可信服务)可以代表您将组织中的 AWS 成员账户注册为组织中的管理员。要启用 AWS 账户作为您组织中受信任的账户的管理员,请调用delegateAdministrator您的账户:
const account = new Account(stack, "Account", {
accountName: "StackSetsDelegatedAdministrator",
email: "info@pepperize.com",
});
account.delegateAdministrator("stacksets.amazonaws.com");
- 支持 Delegated Administrator 的 AWS 服务
- 为了能够使用 Delegated Administrator,您的组织必须启用所有功能。
启用 AWS 服务(可信服务)
要为受支持的 AWS 服务(受信任的服务)启用可信访问,该服务代表您在您的组织及其账户中执行任务,请致电enableAwsService您的组织:
const organization = new Organization(stack, "Organization", {
featureSet: FeatureSet.ALL, // (default) the organization must be created with all features enabled
});
organization.enableAwsServiceAccess("ssm.amazonaws.com");
- 要启用可信访问,您必须启用所有功能。
- 建议仅使用受信任服务的控制台如何启用或禁用受信任访问
- 您可以与 AWS Organizations 一起使用的 AWS 服务
启用策略类型
enablePolicyType在您的组织上启用策略类型调用。
const organization = new Organization(stack, "Organization", {
featureSet: FeatureSet.ALL, // (default) the organization must be created with all features enabled
});
organization.enablePolicyType(PolicyType.SERVICE_CONTROL_POLICY);
organization.enablePolicyType(PolicyType.TAG_POLICY);
organization.enablePolicyType(PolicyType.BACKUP_POLICY);
organization.enablePolicyType(PolicyType.AISERVICES_OPT_OUT_POLICY);
请参阅EnablePolicyType、PolicyType。
政策
要创建新策略,请将以下构造添加到您的堆栈中:
new Policy(stack, "Policy", {
content: '{\n"Version":"2012-10-17","Statement":{\n"Effect":"Allow","Action":"s3:*"\n}\n}',
description: "Enables admins of attached accounts to delegate all S3 permissions",
policyName: "AllowAllS3Actions",
policyType: PolicyType.SERVICE_CONTROL_POLICY,
});
见政策
政策附件
要将策略附加到根、组织单位 (OU) 或带有要附加的策略的个人帐户调用attachPolicy:
organization.enablePolicyType(PolicyType.TAG_POLICY);
const policy = new Policy(stack, "Policy", {
content: '{\n"tags":{\n"CostCenter":{\n"tag_key":{\n"@@assign":"CostCenter"\n}\n}\n}\n}',
description: "Defines the CostCenter tag key",
policyName: "CostCenterTag",
policyType: PolicyType.TAG_POLICY,
});
organization.attachPolicy(policy);
organizationalUnit.attachPolicy(policy);
account.attachPolicy(policy);
标记资源
要标记资源,您可以遵循AWS CDK 开发人员指南 - 标记:
您可以向 AWS Organizations 中的以下资源添加一个或多个标签。
- 帐户
- 组织根
- 组织单位 (OU)
- 政策
请参阅标记 AWS 组织资源、ITaggableResource
标记组织的根
import { Tags } from "aws-cdk-lib";
const organization = new Organization();
Tags.of(organization.root).add("key", "value");
标记组织单位 (OU)
import { Tags } from "aws-cdk-lib";
const organizationalUnit = new OrganizationalUnit();
Tags.of(organizationalUnit).add("key", "value");
标记帐户
import { Tags } from "aws-cdk-lib";
const account = new Account();
Tags.of(account).add("key", "value");
标记策略
import { Tags } from "aws-cdk-lib";
const policy = new Policy();
Tags.of(policy).add("key", "value");
限制
AWS Organizations 有一些限制:
- 堆栈的帐户必须是现有组织的管理帐户。
- 堆栈的帐户成为新组织的管理帐户。
- 一个帐户仅属于单个根中的一个组织。
- AWS 组织的配额
AWS Organizations 是一项全球服务,其服务终端节点位于
us-east-1和us-gov-west-1中cn-northwest-1。另请阅读 Endpoint to call 使用 AWS CLI 或 AWS SDK 时。目前这个库的所有自定义资源都很难使用us-east-1。
例子
查看示例
import { App, Stack } from "aws-cdk-lib/core";
import {
Account,
DelegatedAdministrator,
EnableAwsServiceAccess,
EnablePolicyType,
FeatureSet,
IamUserAccessToBilling,
Organization,
OrganizationalUnit,
Policy,
PolicyAttachment,
PolicyType,
} from "@pepperize/cdk-organizations";
const app = new App();
const stack = new Stack(app);
// Create an organization
const organization = new Organization(stack, "Organization", {
featureSet: FeatureSet.ALL,
});
// Enable AWS Service Access (requires FeatureSet: ALL)
organization.enableAwsServiceAccess("service-abbreviation.amazonaws.com");
// Create an account
const account1 = new Account(stack, "SharedAccount", {
accountName: "SharedAccount",
email: "info+shared-account@pepperize.com",
roleName: "OrganizationAccountAccessRole",
iamUserAccessToBilling: IamUserAccessToBilling.ALLOW,
parent: organization.root,
});
// Enable a delegated admin account
account1.delegateAdministrator("service-abbreviation.amazonaws.com");
// Create an OU in the current organizations root
const projects = new OrganizationalUnit(stack, "ProjectsOU", {
organizationalUnitName: "Projects",
parent: organization.root,
});
const account2 = new Account(stack, "Project1Account", {
accountName: "SharedAccount",
email: "info+project1@pepperize.com",
parent: projects,
});
account2.node.addDependency(account1);
// Create a nested OU and attach two accounts
const project2 = new OrganizationalUnit(stack, "Project2OU", {
organizationalUnitName: "Project2",
parent: projects,
});
const account3 = new Account(stack, "Project2DevAccount", {
accountName: "Project 2 Dev",
email: "info+project2-dev@pepperize.com",
parent: project2,
});
account3.node.addDependency(account2);
const account4 = new Account(stack, "Project2ProdAccount", {
accountName: "Project 2 Prod",
email: "info+project2-prod@pepperize.com",
parent: project2,
});
account4.node.addDependency(account3);
// Enable the service control policy (SCP) type within the organization
organization.enablePolicyType(PolicyType.SERVICE_CONTROL_POLICY);
// Create and attach and Service Control Policy (SCP)
const policy = new Policy(stack, "Policy", {
content: '{\n"Version":"2012-10-17","Statement":{\n"Effect":"Allow","Action":"s3:*"\n}\n}',
description: "Enables admins of attached accounts to delegate all S3 permissions",
policyName: "AllowAllS3Actions",
policyType: PolicyType.SERVICE_CONTROL_POLICY,
});
organization.attachPolicy(policy);
// Tagging AWS organization resources of this stack
Tags.of(stack).add("tagKey", "tagValue");