constructor()

in infra/ecs-service/construct/ecs-infra-const.ts [36:110]


    constructor(scope: cdk.Construct, id: string, props: EcsInfraProps) {
        super(scope, id, props);

        if (props.tableName != undefined) {
            this.table = new ddb.Table(this, 'table', {
                tableName: `${props.stackName}-${props.tableName}`,
                partitionKey: {
                    name: 'id',
                    type: ddb.AttributeType.STRING
                },
                removalPolicy: cdk.RemovalPolicy.DESTROY // not recommended for Prod
            });
        }

        const alb = new loadBalancer.ApplicationLoadBalancer(this, `alb`, {
            loadBalancerName: `${props.stackName}`.substr(0, 32),
            vpc: props.vpc,
            internetFacing: props.internetFacing
        });

        let targetServiceStackName = undefined;
        if (this.stackConfig.TargetStack != undefined) {
            targetServiceStackName = this.stackConfig.TargetStack;
        }

        const baseName = props.stackName;
        this.containerName = `${baseName}Container`
        const albFargateService = new ecsPatterns.ApplicationLoadBalancedFargateService(this, 'Service', {
            loadBalancer: alb,
            cluster: props.cluster,

            desiredCount: props.desiredTasks,
            cpu: props.cpu,
            memoryLimitMiB: props.memory,
            taskImageOptions: {
                image: this.getContainerImage(props),
                containerName: this.containerName,
                environment: {
                    APP_NAME: props.stackName,
                    INFRA_VERSION: props.infraVersion,
                    CONTAINER_SERVICE: 'AWS ECS',
                    DDB_TABLE: props.tableName != undefined ? this.table.tableName : 'no-table',
                    PORT_IN: `${props.containerPort}`,
                    Namespace: `${props.projectPrefix}-NS`,
                    TargetServiceName: targetServiceStackName != undefined ? targetServiceStackName : 'not-defined'
                },
                logDriver: new ecs.AwsLogDriver({
                    streamPrefix: `${baseName}Log`
                }),
                enableLogging: true,
                containerPort: props.containerPort,
                taskRole: this.createTaskRole(baseName),
                executionRole: this.createExecutionRole(baseName)
            },
            cloudMapOptions: {
                name: props.stackName,
            },
            circuitBreaker: {
                rollback: true
            },
        });
        this.service = albFargateService.service;
        this.alb = albFargateService.loadBalancer;

        this.putParameter(`${this.stackConfig.ShortStackName}AlbDnsName`, albFargateService.loadBalancer.loadBalancerDnsName);
        this.putParameter(`${this.stackConfig.ShortStackName}ServiceSecurityGroupId`, this.service.connections.securityGroups[0].securityGroupId);
        this.putVariable(`${this.stackConfig.ShortStackName}PortNumber`, this.stackConfig.PortNumber);

        // if (targetServiceStackName != undefined) {
        //     const serviceSecurityGroup = this.service.connections.securityGroups[0];
        //     const targetSecurityGroupId = this.getParameter(`${targetServiceStackName}ServiceSecurityGroupId`)
        //     const targetSecurityGroup = ec2.SecurityGroup.fromSecurityGroupId(this, 'target-security-group', targetSecurityGroupId);
        //     targetSecurityGroup.addIngressRule(serviceSecurityGroup, ec2.Port.tcp(parseInt(this.getVariable(`${targetServiceStackName}PortNumber`))));
        // }
    }