forked from aws-samples/aws-stf-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
65 lines (49 loc) · 2.09 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
import { Construct } from "constructs";
import { azlist } from "./azlist"
import { CfnOutput, CustomResource, Duration, Stack } from "aws-cdk-lib";
import { Code, Runtime, Function } from "aws-cdk-lib/aws-lambda";
import { PolicyStatement } from "aws-cdk-lib/aws-iam";
import { Provider } from "aws-cdk-lib/custom-resources";
export interface StfUtilProps {}
export class Utils extends Construct {
public readonly az1: string
public readonly az2: string
constructor(scope: Construct, id: string, props?: StfUtilProps) {
super(scope, id)
if(Stack.of(this).region.startsWith('$')){
throw new Error('Please type a valid region in the parameter.ts file')
}
if(!azlist[`${Stack.of(this).region}`]){
throw new Error('The stack is not yet available in the region selected')
}
const compatible_azs = azlist[`${Stack.of(this).region}`]
const get_az_func_path = `${__dirname}/lambda/getAzs`
const get_az_func = new Function(this, 'AzFunction', {
runtime: Runtime.NODEJS_16_X,
code: Code.fromAsset(get_az_func_path),
handler: 'index.handler',
timeout: Duration.seconds(50),
environment: {
COMPATIBLE_AZS: JSON.stringify(compatible_azs)
}
})
get_az_func.addToRolePolicy(new PolicyStatement({
actions: ["ec2:DescribeAvailabilityZones"],
resources: ['*']
}))
const get_az_provider = new Provider(this, 'cleanup_provider', {
onEventHandler: get_az_func
})
const get_az = new CustomResource(this, 'get_az_custom', {
serviceToken: get_az_provider.serviceToken
})
this.az1 = get_az.getAtt('az1').toString()
this.az2 = get_az.getAtt('az2').toString()
new CfnOutput(this, 'AZ1', {
value: this.az1
})
new CfnOutput(this, 'AZ2', {
value: this.az2
})
}
}