0

I'm using CDK to create a stack in AWS. What I need is an infrastructure to be ready to have future containers inside. A lambda behind an API Gateway will fire a Fargate task, creating this new container, as the user demands it, but I am not able to deploy it successfully because the load balancer needs to pass health checks and obviously no container is up at that moment. Examples like this one show how to bring up the LB with at least 1 container. Is it able to do what I am trying to do?

This is what I have at the moment:

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as ecr from 'aws-cdk-lib/aws-ecr';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as ecs from "aws-cdk-lib/aws-ecs";
import * as lambda from "aws-cdk-lib/aws-lambda";
import * as ecsPatterns from "aws-cdk-lib/aws-ecs-patterns";
import * as elb2 from "aws-cdk-lib/aws-elasticloadbalancingv2";
import * as path from 'path';
import * as assets from 'aws-cdk-lib/aws-s3-assets';

export class MyStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const repository = new ecr.Repository(this, 'ecr-repository', {
      imageScanOnPush: true,
      removalPolicy: cdk.RemovalPolicy.DESTROY,
    });

    const vpc = new ec2.Vpc(this, 'public-vpc', {
      maxAzs: 3,
      subnetConfiguration: [
        {
          name: 'public-vpc',
          subnetType: ec2.SubnetType.PUBLIC,
        },
      ],
    });

    const cluster = new ecs.Cluster(this, 'ecs-cluster', {
      containerInsights: true,
      capacity: {
        instanceType: new ec2.InstanceType('t2.nano'),
        minCapacity: 0,
        maxCapacity: 10,
      },
      vpc: vpc
    });

    // This task will create the new container when the lambda runs it
    const task = new ecs.TaskDefinition(this, 'fargate-task', {
      compatibility: ecs.Compatibility.EC2_AND_FARGATE,
      cpu: '256',
      memoryMiB: '512',
    });
    task.addContainer('container-image', {
      image: ecs.ContainerImage.fromEcrRepository(repository, 'latest'),
      cpu: 1,
      memoryReservationMiB: 512,
      portMappings: [
        {
          containerPort: 8080,
          protocol: ecs.Protocol.TCP,
        },
      ],
    });

    // The load balancer over the cluster _should_ not expect any container to exist at this point (that's what I am trying to achieve) 
    const ecsLoadBalancer = new ecsPatterns.ApplicationLoadBalancedEc2Service(this, 'ecs-load-balancer', {
      cluster: cluster,
      taskDefinition: task,
      cpu: 256,
      memoryLimitMiB: 512,
      publicLoadBalancer: true,
      targetProtocol: elb2.ApplicationProtocol.HTTP,
      listenerPort: 8080,
    });

    const lambdaAsset = new assets.Asset(this, 'lambda-handler.zip', {
      path: path.join(__dirname, '../resources/lambda-handler.zip'),
    });

    const fn = new lambda.Function(this, 'lambda-handler', {
      allowPublicSubnet: true,
      handler: 'index.handler',
      code: lambda.Code.fromBucket(lambdaAsset.bucket, lambdaAsset.s3ObjectKey),  
      runtime: lambda.Runtime.NODEJS_16_X,
      environment: {
        TASK_DEFINITION_ARN: task.taskDefinitionArn,
        CLUSTER_ARN: cluster.clusterArn,
        SUBNET_ID: vpc.publicSubnets[0].subnetId,
        SECURITY_GROUP_ID: vpc.vpcDefaultSecurityGroup,
      },
      vpc,
    });
  }
}

This way, when I run cdk deploy it takes forever because it keeps waiting for health check to pass and no one is responding. If I add the following to the load balancer:

    ecsLoadBalancer.targetddGroup.configureHealthCheck({
      enabled: false,
    });

It says that it is invalid for this instance type:

1:02:01 AM | CREATE_FAILED        | AWS::ElasticLoadBalancingV2::TargetGroup  | ecsloadbalanc...erECSGroupE1CFCFA4
Resource handler returned message: "Health check enabled must be true for target groups with target type 'instance' (Service: ElasticLoadBalancingV2, Status C
ode: 400, Request ID: xxxxx)" (RequestToken: xxxxx, HandlerErrorCode: InvalidRequest)

Also I've tried to add desiredCount: 0 at the LB but CDK does not allow me even to synth it since minimum value is 1.

Thank you for the help!

ariel17
  • 155
  • 1
  • 6

0 Answers0