-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcdkv2.ts
67 lines (59 loc) · 2.49 KB
/
cdkv2.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import * as iam from 'aws-cdk-lib/aws-iam';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as cdk from 'aws-cdk-lib';
import * as customResource from 'aws-cdk-lib/custom-resources';
import * as path from 'path';
import { Construct } from 'constructs';
import { SesSmtpCredentialsProps } from './common';
export { SesSmtpCredentialsProps };
export class SesSmtpCredentialsProvider extends Construct {
public readonly provider: customResource.Provider;
public static getOrCreate(scope: Construct): customResource.Provider {
const stack = cdk.Stack.of(scope);
const id = 'com.isotoma.cdk.custom-resources.ses-smtp-credentials';
const x = (stack.node.tryFindChild(id) as SesSmtpCredentialsProvider) || new SesSmtpCredentialsProvider(stack, id);
return x.provider;
}
constructor(scope: Construct, id: string) {
super(scope, id);
this.provider = new customResource.Provider(this, 'ses-smtp-credentials-provider', {
onEventHandler: new lambda.Function(this, 'ses-smtp-credentials-event', {
code: lambda.Code.fromAsset(path.join(__dirname, 'provider')),
runtime: lambda.Runtime.NODEJS_18_X,
handler: 'main.onEvent',
timeout: cdk.Duration.minutes(5),
initialPolicy: [
new iam.PolicyStatement({
resources: ['*'],
actions: ['iam:CreateUser', 'iam:PutUserPolicy', 'iam:CreateAccessKey', 'iam:DeleteUser', 'iam:DeleteUserPolicy', 'iam:DeleteAccessKey'],
}),
],
}),
});
}
}
export class SesSmtpCredentials extends Construct {
public readonly region: string;
private resource: cdk.CustomResource;
constructor(scope: Construct, id: string, props: SesSmtpCredentialsProps) {
super(scope, id);
if (!props.region) {
throw new Error('No region specified');
}
this.region = props.region;
const provider = SesSmtpCredentialsProvider.getOrCreate(this);
this.resource = new cdk.CustomResource(this, 'Resource', {
serviceToken: provider.serviceToken,
resourceType: 'Custom::SesSmtpCredentials',
properties: {
Region: this.region,
},
});
}
public username(): string {
return this.resource.getAttString('Username');
}
public password(): string {
return this.resource.getAttString('Password');
}
}