|
| 1 | +// This CDK L3 example creates a MongoDB Atlas project, cluster, databaseUser, and projectIpAccessList |
| 2 | +import * as cdk from 'aws-cdk-lib'; |
| 3 | +import { Construct } from 'constructs'; |
| 4 | +import { AtlasServerlessBasic, ServerlessInstanceProviderSettingsProviderName } from 'awscdk-resources-mongodbatlas'; |
| 5 | + |
| 6 | +interface AtlasStackProps { |
| 7 | + readonly orgId: string; |
| 8 | + readonly profile: string; |
| 9 | + readonly region: string; |
| 10 | + readonly ip: string; |
| 11 | + readonly continuousBackupEnabled: string; |
| 12 | + readonly terminationProtectionEnabled: string; |
| 13 | +} |
| 14 | + |
| 15 | +export class CdkTestingStack extends cdk.Stack { |
| 16 | + constructor(scope: Construct, id: string, props?: cdk.StackProps) { |
| 17 | + super(scope, id, props); |
| 18 | + |
| 19 | + const atlasProps = this.getContextProps(); |
| 20 | + const atlasBasic = new AtlasServerlessBasic(this, 'AtlasServerlessBasic', { |
| 21 | + serverlessProps: { |
| 22 | + projectId: atlasProps.orgId, |
| 23 | + profile: atlasProps.profile, |
| 24 | + continuousBackupEnabled: atlasProps.continuousBackupEnabled, |
| 25 | + providerSettings: { |
| 26 | + providerName: ServerlessInstanceProviderSettingsProviderName.SERVERLESS |
| 27 | + }, |
| 28 | + terminationProtectionEnabled: atlasProps.terminationProtectionEnabled |
| 29 | + }, |
| 30 | + projectProps: { |
| 31 | + orgId: atlasProps.orgId, |
| 32 | + }, |
| 33 | + |
| 34 | + ipAccessListProps: { |
| 35 | + accessList:[ |
| 36 | + { ipAddress: atlasProps.ip, comment: 'My first IP address' } |
| 37 | + ] |
| 38 | + }, |
| 39 | + profile: atlasProps.profile, |
| 40 | + }); |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + getContextProps(): AtlasStackProps { |
| 45 | + const orgId = this.node.tryGetContext('orgId'); |
| 46 | + if (!orgId){ |
| 47 | + throw "No context value specified for orgId. Please specify via the cdk context." |
| 48 | + } |
| 49 | + const profile = this.node.tryGetContext('profile') ?? 'default'; |
| 50 | + const terminationProtectionEnabled = this.node.tryGetContext('terminationProtectionEnabled'); |
| 51 | + const continuousBackupEnabled = this.node.tryGetContext('continuousBackupEnabled'); |
| 52 | + const region = this.node.tryGetContext('region') ?? "US_EAST_1"; |
| 53 | + const ip = this.node.tryGetContext('ip'); |
| 54 | + if (!ip){ |
| 55 | + throw "No context value specified for ip. Please specify via the cdk context." |
| 56 | + } |
| 57 | + |
| 58 | + return { |
| 59 | + orgId, |
| 60 | + terminationProtectionEnabled, |
| 61 | + continuousBackupEnabled, |
| 62 | + profile, |
| 63 | + region, |
| 64 | + ip |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments