/
index.ts
159 lines (138 loc) · 4.1 KB
/
index.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import * as path from 'path';
import * as iam from '@aws-cdk/aws-iam';
import * as lambda from '@aws-cdk/aws-lambda';
import * as logs from '@aws-cdk/aws-logs';
import * as cdk from '@aws-cdk/core';
import * as cr from '@aws-cdk/custom-resources';
/**
* Properties of the RemoteOutputs
*/
export interface RemoteOutputsProps {
/**
* The remote CDK stack to get the outputs from.
*/
readonly stack: cdk.Stack;
/**
* Indicate whether always update the custom resource to get the new stack output
* @default true
*/
readonly alwaysUpdate?: boolean;
}
/**
* Represents the RemoteOutputs of the remote CDK stack
*/
export class RemoteOutputs extends cdk.Construct {
/**
* The outputs from the remote stack.
*/
readonly outputs: cdk.CustomResource;
constructor(scope: cdk.Construct, id: string, props: RemoteOutputsProps) {
super(scope, id);
const onEvent = new lambda.Function(this, 'MyHandler', {
runtime: lambda.Runtime.PYTHON_3_8,
code: lambda.Code.fromAsset(path.join(__dirname, '../custom-resource-handler')),
handler: 'remote-outputs.on_event',
});
const myProvider = new cr.Provider(this, 'MyProvider', {
onEventHandler: onEvent,
logRetention: logs.RetentionDays.ONE_DAY,
});
onEvent.addToRolePolicy(new iam.PolicyStatement({
actions: ['cloudformation:DescribeStacks'],
resources: ['*'],
}));
this.outputs = new cdk.CustomResource(this, 'RemoteOutputs', {
serviceToken: myProvider.serviceToken,
properties: {
stackName: props.stack.stackName,
regionName: cdk.Stack.of(props.stack).region,
randomString: props.alwaysUpdate == false ? undefined : randomString(),
},
});
}
/**
* Get the attribute value from the outputs.
* @param key output key
*/
public get(key: string) {
return this.outputs.getAttString(key);
}
}
/**
* Properties of the RemoteParameters
*/
export interface RemoteParametersProps {
// /**
// * The remote CDK stack to get the parameters from.
// */
// readonly stack: cdk.Stack;
/**
* The region code of the remote stack.
*/
readonly region: string;
/**
* The assumed role used to get remote parameters.
*/
readonly role?: iam.IRole;
/**
* The parameter path.
*/
readonly path: string;
/**
* Indicate whether always update the custom resource to get the new stack output
* @default true
*/
readonly alwaysUpdate?: boolean;
}
/**
* Represents the RemoteParameters of the remote CDK stack
*/
export class RemoteParameters extends cdk.Construct {
/**
* The parameters in the SSM parameter store for the remote stack.
*/
readonly parameters: cdk.CustomResource;
constructor(scope: cdk.Construct, id: string, props: RemoteParametersProps) {
super(scope, id);
const onEvent = new lambda.Function(this, 'MyHandler', {
runtime: lambda.Runtime.PYTHON_3_8,
code: lambda.Code.fromAsset(path.join(__dirname, '../custom-resource-handler')),
handler: 'remote-parameters.on_event',
});
const myProvider = new cr.Provider(this, 'MyProvider', {
onEventHandler: onEvent,
logRetention: logs.RetentionDays.ONE_DAY,
});
onEvent.addToRolePolicy(new iam.PolicyStatement({
actions: ['ssm:GetParametersByPath'],
resources: ['*'],
}));
this.parameters = new cdk.CustomResource(this, 'SsmParameters', {
serviceToken: myProvider.serviceToken,
properties: {
stackName: cdk.Stack.of(this).stackName,
regionName: props.region,
parameterPath: props.path,
randomString: props.alwaysUpdate == false ? undefined : randomString(),
role: props.role?.roleArn,
},
});
if (props.role) {
myProvider.onEventHandler.addToRolePolicy(new iam.PolicyStatement({
actions: ['sts:AssumeRole'],
resources: [props.role.roleArn],
}));
}
}
/**
* Get the parameter.
* @param key output key
*/
public get(key: string) {
return this.parameters.getAttString(key);
}
}
function randomString() {
// Crazy
return Math.random().toString(36).replace(/[^a-z0-9]+/g, '');
}