Search code examples
jsontypescriptaws-cdkgrafana-loki

Can I create 2 or more ECS task in the same CDK deploy code for Loki and Grafana?


I am new to AWS CDK using typescript. I want to know can I create 2 or more ECS tasks in the same CDK deploy code or do I need to create separate app codes and different ECS tasks for each?

Furthermore, any idea about Loki and Grafana should they be in the same ecs task or different or it doesn't matter if I create 2 separate ecs tasks for each? As I will be using common ALB which has different listener rules for Loki and Grafana on ports 3100 and 3000 respectively.

my CDK.json

my CDK.json

I am getting the following error:

Parse error on line 192:
... }      }    }  }],[ {  "appGrafan
--------------------^
Expecting '}', ',', got ']'

tried this: tried thise

but still getting errors:

Parse error on line 190:
...     }    }  }, {"appGrafana": "npx 
-------------------^
Expecting 'STRING', got '{'

Also, tried in IDE: Error in IDE

ashishkarpe in ~/code/docker-loki/deploy/cdk on branch pre > cdk ls
cdk.json: Unexpected token ; in JSON at position 5660
ashishkarpe in ~/code/docker-loki/deploy/cdk on branch pre > cdk diff
cdk.json: Unexpected token ; in JSON at position 5660
ashishkarpe in ~/code/docker-loki/deploy/cdk on branch pre > 

Solution

  • I am not sure why you are modifying cdk.json. You should not modify it.

    I think you are mixing several concepts. Lets first start with CDK. CDK can be divided in 3 major components(Apps, Stacks and Constructs).

    Apps is the main component, think about it as the main from a java class. This is the entry point of your application and will handle the creation of all your stacks that are inside your application.

    Next we have a stack, this is considered as a group of elements within a scope. Everythig inside the stack is consider a unit. A good example of this is the visualizaztion of a pipeline. You can have in a pipeline with a Beta stack and a Prod stack. This can become more complex depending how you configure. You can have Alpha, Beta, Gamma, Prod as part of different stages but also have NA, EU, CN and FE as part of your region and based on that create a lot combinations.

    Last but not least we have Construct, construct should consist of your aws resources that you need as part of your application. Some can be as simple as creating an S3 bucket, while others one can be complex as creating and S3 bucket which publishes a notification to a Topic and only certain people are allow to read and write to the bucket(S3 Policy).

    import { App, Stack, StackProps } from '@aws-cdk/core';
    import * as s3 from '@aws-cdk/aws-s3';
    
    class HelloCdkStack extends Stack {
      constructor(scope: App, id: string, props?: StackProps) {
        super(scope, id, props);
    
        new s3.Bucket(this, 'MyFirstBucket', {
          versioned: true
        });
      }
    }
    
    const app = new App();
    new HelloCdkStack(app, "HelloCdkStack");
    

    Using the above example. We are creating an application which has only 1 stack and in this case the S3 is a construct.

    I dont have context about grafana-loki so i will assume that they are 2 components that you want to use separately. In your example, you want to create an application that have 2 different services/constructs(I would not recommend this as everytime that you need to deploy both services will deploy at the same time, if you configure something incorrectly both will fail to deploy). One construct is the Grafana and the other is Loki each one have their one ecs component using different port.

    So how would you application look after the above. You will need to create 2 construct. One for Grafana and another one for Loki. This construct will be call by your stack so it can create the resources of each construct and then it will synthentize them.

    class GrafanaConstruct extends Construct {
      constructor(scope: App, id: string, props?: StackProps) {
        super(scope, id, props);
        // Create Fargate component
      }
    }
    
    class LokiConstruct extends Construct {
      constructor(scope: App, id: string, props?: StackProps) {
        super(scope, id, props);
        // Create Fargate component    
      }
    }
    
    class GrafranaAndLokiCdkStack extends Stack {
      constructor(scope: App, id: string, props?: StackProps) {
        super(scope, id, props);
    
        new LokiConstruct(this, "LokiConstruct", props);
        new GrafanaConstruct(this, "GrafanaConstruct", props);
      }
    }
    
    const app = new App();
    new GrafranaAndLokiCdkStack(app, "GrafranaAndLokiCdkStack");
    app.synth();