constructor()

in spec/fixtures/cdk/multi_service/rails-foo-fargate-stack.ts [20:100]


    constructor(scope: cdk.App, id: string, props: RailsFooFargateStackProps) {
        super(scope, id);

        // import resources
        const cluster = props.cluster;
        const vpc = props.vpc;

        // Create secret from SecretsManager
        const username = 'RailsFooDBAdminU';
        const secret = new secretsmanager.Secret(this, 'Secret', {
            generateSecretString: {
                excludePunctuation: true
            }
        });
        const password = secret.secretValue;

        // Import DB cluster ParameterGroup
        const parameterGroup = rds.ParameterGroup.fromParameterGroupName(
            this, 'DBClusterPG', 'aws-rails-provisioner-default-aurora-mysql');
        // Create DB Cluster
        const db = new rds.DatabaseCluster(this, 'DBCluster', {
            engine: rds.DatabaseClusterEngine.AURORA_MYSQL,
            masterUser: {
                username: username,
                password: password
            },
            instanceProps: {
                instanceType: new ec2.InstanceType('r5.large'),
                vpc: vpc,
                vpcSubnets: {
                  subnetType: ec2.SubnetType.ISOLATED
                }
            },
            defaultDatabaseName: 'app_development',
            removalPolicy: cdk.RemovalPolicy.RETAIN,
            instances: 2,
            parameterGroup: parameterGroup
        });
        const dbUrl = "mysql2://" + username + ":" + password + "@" + db.clusterEndpoint.socketAddress + "/app_development";
        this.dbUrl = dbUrl;

        const asset = new ecr_assets.DockerImageAsset(this, 'ImageAssetBuild', {
            directory: '/absolute/path/to/dir/path/to/rails_foo'
        });

        // compute repo name from asset image
        const parts = asset.imageUri.split("@")[0].split("/");
        const repoName = parts.slice(1, parts.length).join("/").split(":")[0];
        this.repoName = repoName;

        const image = ecs.ContainerImage.fromDockerImageAsset(asset);

        // Fargate service
        const lbFargate = new ecs_patterns.ApplicationLoadBalancedFargateService(this, 'LBFargate', {
            serviceName: 'RailsFoo',
            cluster: cluster,
            taskImageOptions: {
              image: image,
              containerName: 'FargateTaskContainer',
              containerPort: 80,
              environment: {
                  'DATABASE_URL': dbUrl,
                  'PORT': '80',
              },
              enableLogging: true,
            },
            memoryLimitMiB: 512,
            cpu: 256,
            desiredCount: 1,
            publicLoadBalancer: true,
            assignPublicIp: true
        });
        db.connections.allowDefaultPortFrom(lbFargate.service, 'From Fargate');
        this.db = db;

        const scaling = lbFargate.service.autoScaleTaskCount({
            maxCapacity: 2,
        });

        this.service = lbFargate.service;
    }