Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Commit

Permalink
feat: Add cross-account subdomain constructs
Browse files Browse the repository at this point in the history
  • Loading branch information
usirin committed Nov 1, 2022
1 parent 0d4aa50 commit 7ab855c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 10 deletions.
35 changes: 35 additions & 0 deletions src/cross-account-delegation-role.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Stack } from 'aws-cdk-lib';
import { IRole, Role } from 'aws-cdk-lib/aws-iam';
import { Construct } from 'constructs';

export interface CrossAccountDelegationRoleProps {
readonly organizationAccountID: string;
readonly delegationResourceName: string;
}

export interface ICrossAccountDelegationRole {
readonly role: IRole;
}

export class CrossAccountDelegationRole extends Construct {
public readonly roleArn: string;
public readonly role: IRole;

constructor(
scope: Construct,
id: string,
props: CrossAccountDelegationRoleProps
) {
super(scope, id);

this.roleArn = Stack.of(this).formatArn({
region: '',
service: 'iam',
account: props.organizationAccountID,
resource: 'role',
resourceName: props.delegationResourceName,
});

this.role = Role.fromRoleArn(this, 'DelegationRole', this.roleArn);
}
}
48 changes: 48 additions & 0 deletions src/cross-account-subdomain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
CertificateValidation,
Certificate,
} from 'aws-cdk-lib/aws-certificatemanager';
import {
PublicHostedZone,
CrossAccountZoneDelegationRecord,
} from 'aws-cdk-lib/aws-route53';
import { Construct } from 'constructs';
import { ICrossAccountDelegationRole } from './cross-account-delegation-role';

export interface CrossAccountSubdomainProps {
readonly organizationAccountID: string;
readonly subdomain: string;
readonly rootDomain: string;

readonly delegationRole: ICrossAccountDelegationRole;
}

export class CrossAccountSubdomain extends Construct {
public readonly hostedZone: PublicHostedZone;
public readonly certificate: Certificate;

constructor(scope: Construct, id: string, props: CrossAccountSubdomainProps) {
super(scope, id);

const domainName = `${props.subdomain}.${props.rootDomain}`;

this.hostedZone = new PublicHostedZone(this, 'HostedZone', {
zoneName: domainName,
});

new CrossAccountZoneDelegationRecord(
this,
'CrossAccountZoneDelegationRecord',
{
delegatedZone: this.hostedZone,
parentHostedZoneName: props.rootDomain,
delegationRole: props.delegationRole.role,
}
);

this.certificate = new Certificate(this, 'DomainCertificate', {
domainName: domainName,
validation: CertificateValidation.fromDns(this.hostedZone),
});
}
}
7 changes: 2 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
export class Hello {
public sayHello() {
return 'hello, world!';
}
}
export * from './cross-account-subdomain';
export * from './cross-account-delegation-role';
5 changes: 5 additions & 0 deletions test/dummy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('dummy tests', () => {
it('works', () => {
expect(5).toBe(5);
});
});
5 changes: 0 additions & 5 deletions test/hello.test.ts

This file was deleted.

0 comments on commit 7ab855c

Please sign in to comment.