|
| 1 | +import * as cdk from "aws-cdk-lib"; |
| 2 | +import * as cdkContructs from 'constructs'; |
| 3 | +import * as cr from 'aws-cdk-lib/custom-resources'; |
| 4 | +import * as configTypes from "./config.interface"; |
| 5 | +import * as nag from "cdk-nag"; |
| 6 | + |
| 7 | +export interface SingleNodeAMBEthereumConstructCustomProps { |
| 8 | + instanceType: string, |
| 9 | + availabilityZone: string, |
| 10 | + ethNetworkId: configTypes.AMBEthereumNodeNetworkId, |
| 11 | + } |
| 12 | + |
| 13 | +export class SingleNodeAMBEthereumConstruct extends cdkContructs.Construct { |
| 14 | + public nodeId: string; |
| 15 | + public rpcUrl: string; |
| 16 | + public billingToken: string; |
| 17 | + public rpcUrlWithBillingToken: string; |
| 18 | + |
| 19 | + |
| 20 | + constructor(scope: cdkContructs.Construct, id: string, props: SingleNodeAMBEthereumConstructCustomProps) { |
| 21 | + super(scope, id); |
| 22 | + |
| 23 | + const REGION = cdk.Stack.of(this).region; |
| 24 | + const { |
| 25 | + instanceType, |
| 26 | + availabilityZone, |
| 27 | + ethNetworkId, |
| 28 | + } = props; |
| 29 | + |
| 30 | + const createNode = new cr.AwsCustomResource(this, 'createNode', { |
| 31 | + onCreate: { // will be called for a CREATE event |
| 32 | + service: 'ManagedBlockchain', |
| 33 | + action: 'createNode', |
| 34 | + parameters: { |
| 35 | + NetworkId: `n-ethereum-${ethNetworkId}`, |
| 36 | + NodeConfiguration: { |
| 37 | + AvailabilityZone: availabilityZone, |
| 38 | + InstanceType: instanceType |
| 39 | + } |
| 40 | + }, |
| 41 | + physicalResourceId: cr.PhysicalResourceId.of(Date.now().toString()), // Update physical id to always fetch the latest version |
| 42 | + }, |
| 43 | + installLatestAwsSdk:true, |
| 44 | + policy: cr.AwsCustomResourcePolicy.fromSdkCalls({ |
| 45 | + resources: cr.AwsCustomResourcePolicy.ANY_RESOURCE, |
| 46 | + }), |
| 47 | + }); |
| 48 | + |
| 49 | + const createAccessor = new cr.AwsCustomResource(this, 'createAccessor', { |
| 50 | + onCreate: { // will be called for a CREATE event |
| 51 | + service: 'ManagedBlockchain', |
| 52 | + action: 'createAccessor', |
| 53 | + parameters: { |
| 54 | + AccessorType: 'BILLING_TOKEN' |
| 55 | + }, |
| 56 | + physicalResourceId: cr.PhysicalResourceId.of(Date.now().toString()), // Update physical id to always fetch the latest version |
| 57 | + }, |
| 58 | + installLatestAwsSdk:true, |
| 59 | + policy: cr.AwsCustomResourcePolicy.fromSdkCalls({ |
| 60 | + resources: cr.AwsCustomResourcePolicy.ANY_RESOURCE, |
| 61 | + }), |
| 62 | + }); |
| 63 | + |
| 64 | + this.nodeId = createNode.getResponseField('NodeId'); |
| 65 | + this.rpcUrl = `https://${this.nodeId}.t.ethereum.managedblockchain.${REGION}.amazonaws.com`; |
| 66 | + this.billingToken=createAccessor.getResponseField('BillingToken'); |
| 67 | + this.rpcUrlWithBillingToken = `${this.rpcUrl}?billingtoken=${this.billingToken}`; |
| 68 | + |
| 69 | + const deleteAccessor = new cr.AwsCustomResource(this, 'deleteAccessor', { |
| 70 | + onDelete: { |
| 71 | + service: 'ManagedBlockchain', |
| 72 | + action: 'deleteAccessor', |
| 73 | + parameters: { |
| 74 | + AccessorId: createAccessor.getResponseField('AccessorId'), |
| 75 | + }, |
| 76 | + }, |
| 77 | + installLatestAwsSdk:true, |
| 78 | + policy: cr.AwsCustomResourcePolicy.fromSdkCalls({ |
| 79 | + resources: cr.AwsCustomResourcePolicy.ANY_RESOURCE, |
| 80 | + }), |
| 81 | + }); |
| 82 | + |
| 83 | + const deleteNode = new cr.AwsCustomResource(this, 'deleteNode', { |
| 84 | + onDelete: { |
| 85 | + service: 'ManagedBlockchain', |
| 86 | + action: 'deleteNode', |
| 87 | + parameters: { |
| 88 | + NetworkId: `n-ethereum-${ethNetworkId}`, |
| 89 | + NodeId: this.nodeId, |
| 90 | + }, |
| 91 | + }, |
| 92 | + installLatestAwsSdk:true, |
| 93 | + policy: cr.AwsCustomResourcePolicy.fromSdkCalls({ |
| 94 | + resources: cr.AwsCustomResourcePolicy.ANY_RESOURCE, |
| 95 | + }), |
| 96 | + }); |
| 97 | + |
| 98 | + // nag.NagSuppressions.addResourceSuppressions( |
| 99 | + // this, |
| 100 | + // [ |
| 101 | + // { |
| 102 | + // id: "AwsSolutions-EC29", |
| 103 | + // reason: "Its Ok to terminate this instance as long as we have the data in the snapshot", |
| 104 | + |
| 105 | + // }, |
| 106 | + // ], |
| 107 | + // true |
| 108 | + // ); |
| 109 | + } |
| 110 | + } |
0 commit comments