|
| 1 | +import * as cdk from 'aws-cdk-lib'; |
| 2 | +import { Construct } from 'constructs'; |
| 3 | +import * as ec2 from 'aws-cdk-lib/aws-ec2'; |
| 4 | +import * as opensearch from 'aws-cdk-lib/aws-opensearchservice'; |
| 5 | +import * as secretsmanager from 'aws-cdk-lib/aws-secretsmanager'; |
| 6 | + |
| 7 | +export class AOSStack extends cdk.Stack { |
| 8 | + _vpc; |
| 9 | + _securityGroup; |
| 10 | + public readonly endpoint: string; |
| 11 | + |
| 12 | + constructor(scope: Construct, id: string, props: cdk.StackProps) { |
| 13 | + super(scope, id, props); |
| 14 | + |
| 15 | + this._vpc = ec2.Vpc.fromLookup(this, "VPC", { |
| 16 | + isDefault: true, |
| 17 | + }); |
| 18 | + // Lookup a VPC |
| 19 | + // this._vpc = props.vpc; |
| 20 | + |
| 21 | + // Create a Security Group for OpenSearch |
| 22 | + this._securityGroup = new ec2.SecurityGroup(this, 'GenBIOpenSearchSG', { |
| 23 | + vpc: this._vpc, |
| 24 | + description: 'Allow access to OpenSearch', |
| 25 | + allowAllOutbound: true |
| 26 | + }); |
| 27 | + const secretName = 'GenBIAOSSecret'; // Add the secret name here |
| 28 | + const templatedSecret = new secretsmanager.Secret(this, 'TemplatedSecret', { |
| 29 | + secretName: secretName, |
| 30 | + description: 'Templated secret used for OpenSearch master user password', |
| 31 | + generateSecretString: { |
| 32 | + excludePunctuation: false, |
| 33 | + includeSpace: false, |
| 34 | + generateStringKey: 'password', |
| 35 | + passwordLength: 12, |
| 36 | + requireEachIncludedType: true, |
| 37 | + secretStringTemplate: JSON.stringify({ username: 'master-user' }) |
| 38 | + }, |
| 39 | + removalPolicy: cdk.RemovalPolicy.DESTROY |
| 40 | + }); |
| 41 | + |
| 42 | + // Allow inbound HTTP and HTTPS traffic |
| 43 | + this._securityGroup.addIngressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(80), 'Allow HTTP access'); |
| 44 | + this._securityGroup.addIngressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(443), 'Allow HTTPS access'); |
| 45 | + |
| 46 | + // Find subnets in different availability zones |
| 47 | + const subnets = this._vpc.selectSubnets({ |
| 48 | + subnetType: ec2.SubnetType.PUBLIC, |
| 49 | + }).subnets; |
| 50 | + |
| 51 | + // if (subnets.length < 3) { |
| 52 | + // throw new Error('The VPC must have at least two public subnets in different availability zones.'); |
| 53 | + // } |
| 54 | + |
| 55 | + // Create the OpenSearch domain |
| 56 | + const domain = new opensearch.Domain(this, 'GenBiOpenSearchDomain', { |
| 57 | + version: opensearch.EngineVersion.OPENSEARCH_2_9, |
| 58 | + vpc: this._vpc, |
| 59 | + securityGroups: [this._securityGroup], |
| 60 | + vpcSubnets: [ |
| 61 | + { subnets: [subnets[0]] }, |
| 62 | + ], |
| 63 | + // vpcSubnets: SubnetSelection(one_per_az=True, subnet_type=aws_ec2.SubnetType.PUBLIC), |
| 64 | + capacity: { |
| 65 | + dataNodes: 1, |
| 66 | + dataNodeInstanceType: 'm5.large.search', |
| 67 | + multiAzWithStandbyEnabled: false |
| 68 | + }, |
| 69 | + ebs: { |
| 70 | + volumeType: ec2.EbsDeviceVolumeType.GP3, |
| 71 | + volumeSize: 20, |
| 72 | + }, |
| 73 | + zoneAwareness: { |
| 74 | + enabled: false |
| 75 | + }, |
| 76 | + nodeToNodeEncryption: true, |
| 77 | + encryptionAtRest: { |
| 78 | + enabled: true |
| 79 | + }, |
| 80 | + enforceHttps: true, |
| 81 | + fineGrainedAccessControl: { |
| 82 | + masterUserName: 'master-user', |
| 83 | + masterUserPassword: cdk.SecretValue.secretsManager(templatedSecret.secretArn, { |
| 84 | + jsonField: 'password' |
| 85 | + } |
| 86 | + ), |
| 87 | + }, |
| 88 | + }); |
| 89 | + this.endpoint = domain.domainEndpoint; |
| 90 | + |
| 91 | + new cdk.CfnOutput(this, 'AOSDomainEndpoint', { |
| 92 | + value: this.endpoint, |
| 93 | + description: 'The endpoint of the OpenSearch domain' |
| 94 | + }); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// const app = new cdk.App(); |
| 99 | +// new AOSStack(app, 'AOSStack', { |
| 100 | +// env: { |
| 101 | +// account: process.env.CDK_DEFAULT_ACCOUNT, |
| 102 | +// region: process.env.CDK_DEFAULT_REGION |
| 103 | +// } |
| 104 | +// }); |
0 commit comments