Search code examples
amazon-web-servicesaws-lambdacloudaws-cdk

AWS cdk stack hangs at last steps without error message, but works anyway. I don't know why


I'm new to cdk/aws and don't really know much about it. I'm just building a node-express app to ecr, and then deploying it. This appears to work fine and I can access to endpoints and everything, however cdk is getting stuck. Doesn't matter if I deploy from pipeline or locally using cdk.

import * as cdk from 'aws-cdk-lib';
    import { CfnOutput, Duration } from 'aws-cdk-lib';
    import * as ec2 from 'aws-cdk-lib/aws-ec2';
    import * as ecr from 'aws-cdk-lib/aws-ecr';
    import * as ecs from 'aws-cdk-lib/aws-ecs';
    import * as elbv2 from 'aws-cdk-lib/aws-elasticloadbalancingv2';
    import * as iam from 'aws-cdk-lib/aws-iam';
    import { Construct } from 'constructs';
    import { Networking } from '../imports/Networking';
    
    interface DatabaseProps extends cdk.StackProps {
      databaseName: string,
      databasePass: string
    }
    
    export class WebApiStack extends cdk.Stack {
    
      constructor(scope: Construct, id: string, props: DatabaseProps) {
        super(scope, id, props);
    
        const vpc = Networking.createVpc(this);
    
        const executionRolePolicy = new iam.PolicyStatement({
          effect: iam.Effect.ALLOW,
          resources: ['*'],
          actions: [
            "ecr:GetAuthorizationToken",
            "ecr:BatchCheckLayerAvailability",
            "ecr:GetDownloadUrlForLayer",
            "ecr:BatchGetImage",
            "logs:CreateLogStream",
            "logs:PutLogEvents"
          ]
        });
    
        const repository = ecr.Repository.fromRepositoryName(this, 'nodejs-repo', 'api')
    
        //Find a way to make it a generic object
        const cluster = new ecs.Cluster(this, 'Cluster', { vpc });
    
        // Add capacity to it
        cluster.addCapacity('DefaultAutoScalingGroupCapacity', {
          instanceType: new ec2.InstanceType("t2.micro"),
        });
    
        const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef');
    
        const container = taskDefinition.addContainer('fullstack', {
          image: new ecs.EcrImage(repository, 'latest'),
          cpu: 128,
          memoryReservationMiB: 256,
          logging: ecs.LogDrivers.awsLogs({ streamPrefix: 'nodejs' }),
          environment: {
            DatabasePassword: props.databasePass,
            DatabaseName: props.databaseName,
            ME_CONFIG_MONGODB_URL: "mongodb+srv://fake.mongodb.net/",
            SECRET_API:"fakeszzz",
            SECRET_COOKIE:'fakezz'
          }
        });
    
        // Instantiate an Amazon ECS Service
        const ecsService = new ecs.Ec2Service(this, 'Service', {
          cluster,
          taskDefinition,
        });
    
        taskDefinition.addToExecutionRolePolicy(executionRolePolicy);
        container.addPortMappings({
          containerPort: 3000
        });
    
        // Setup AutoScaling policy
        const scaling = ecsService.autoScaleTaskCount({ maxCapacity: 3, minCapacity: 1 });
        scaling.scaleOnCpuUtilization('CpuScaling', {
          targetUtilizationPercent: 90,
          scaleInCooldown: Duration.seconds(60),
          scaleOutCooldown: Duration.seconds(60)
        });
    
        const lb = new elbv2.ApplicationLoadBalancer(this, 'ALB', {
          vpc,
          internetFacing: true
        });
        
        const listener = lb.addListener('Listener', {
          port: 80,
        });
    
        listener.addTargets('Target', {
          port: 80,
          targets: [ecsService],
          healthCheck: { path: '/api/' }
        });
    
        listener.connections.allowDefaultPortFromAnyIpv4('Open to the world');
    
        new CfnOutput(this, 'webApi', { value: container.imageName });
      }
    }

It fails with the following, without errors.

WebApiStack | 48/53 | 3:27:26 AM | CREATE_COMPLETE      | AWS::SNS::Subscription                      | Cluster/DefaultAutoScalingGroupCapacity/DrainECSHook/Function/Topic (ClusterDefaultAutoScalingGroupCapacityDrainECSHookFunctionTopicC6667F16) 
WebApiStack | 49/53 | 3:27:35 AM | CREATE_COMPLETE      | AWS::Lambda::Permission                     | Cluster/DefaultAutoScalingGroupCapacity/DrainECSHook/Function/AllowInvoke:WebApiStackClusterDefaultAutoScalingGroupCapacityLifecycleHookDrainHookTopic27A34C51 (ClusterDefaultAutoScalingGroupCapacityDrainECSHookFunctionAllowInvokeWebApiStackClusterDefaultAutoScalingGroupCapacityLifecycleHookDrainHookTopic27A34C51E27DA75C) 
49/53 Currently in progress: WebApiStack, ServiceD69D759B

Solution

  • Solved it by removing the healthcheck path on that ecsService since that endpoint doesnt exist.