title: Running two containers on Fargate using CDK
body: <p>I'd like to use Fargate to run two containers - one for the main project's backend, and another for the database (MongoDB). <a href="https://github.com/awslabs/aws-cdk/blob/master/examples/cdk-examples-typescript/hello-cdk-fargate/index.ts" rel="nofollow noreferrer">The basic example</a> included in the GitHub repo shows how to run a single container on Fargate using CDK, however I still have 2 issues:</p>
  
  <ol>
  <li>The example doesn't show how to run two containers.</li>
  <li>I'd like to scale the database containers but have them share the data storage (so that the data gets stored in a central place and stays synchronized between the different containers).</li>
  </ol>
  
  <p>I've figured out how to (sort of) fixed the first issue, similarly to how <a href="https://github.com/awslabs/aws-cdk/blob/master/packages/%40aws-cdk/aws-ecs/lib/load-balanced-fargate-service.ts" rel="nofollow noreferrer"><code>ecs.LoadBalancedFargateService</code></a> is implemented, however the second issue still remains.</p>
  
  <p>For reference, this is what I have so far in <code>stack.ts</code> (the rest is the basic boilerplate <code>cdk init app --language typescript</code> generates for you):</p>
  
  <pre class="lang-js prettyprint-override"><code>import cdk = require("@aws-cdk/cdk");
  import ec2 = require("@aws-cdk/aws-ec2");
  import ecs = require("@aws-cdk/aws-ecs");
  import elbv2 = require("@aws-cdk/aws-elasticloadbalancingv2");
  
  const {ApplicationProtocol} = elbv2;
  
  export class AppStack extends cdk.Stack {
      constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
          super(scope, id, props);
  
          // Create VPC and Fargate Cluster
          const vpc = new ec2.VpcNetwork(this, "FargateVPC", {
              maxAZs: 2
          });
          const cluster = new ecs.Cluster(this, "Cluster", {vpc});
  
          // Create task definition
          const fargateTaskDefinition = new ecs.FargateTaskDefinition(this, "FargateTaskDef", {
              memoryMiB: "512",
              cpu: "256"
          });
  
          // Create container from local `Dockerfile`
          const appContainer = fargateTaskDefinition.addContainer("Container", {
              image: ecs.ContainerImage.fromAsset(this, "Image", {
                  directory: ".."
              })
          });
          // Set port mapping
          appContainer.addPortMappings({
              containerPort: 5000
          });
  
          // Create container from DockerHub image
          const mongoContainer = fargateTaskDefinition.addContainer("MongoContainer", {
              image: ecs.ContainerImage.fromDockerHub("mongo")
          });
          // Set port mapping
          mongoContainer.addPortMappings({
              containerPort: 27017
          });
  
          // Create service
          const service = new ecs.FargateService(this, "Service", {
              cluster,
              taskDefinition: fargateTaskDefinition,
              desiredCount: 2
          });
  
          // Configure task auto-scaling      
          const scaling = service.autoScaleTaskCount({
              maxCapacity: 5
          });
          scaling.scaleOnCpuUtilization("CpuScaling", {
              targetUtilizationPercent: 70
          });
  
          // Create service with built-in load balancer
          const loadBalancer = new elbv2.ApplicationLoadBalancer(this, "AppLB", {
              vpc,
              internetFacing: true
          });
          // Allow incoming connections
          loadBalancer.connections.allowFromAnyIPv4(new ec2.TcpPort(5000), "Allow inbound HTTP");
  
          // Create a listener and listen to incoming requests
          const listener = loadBalancer.addListener("Listener", {
              port: 5000,
              protocol: ApplicationProtocol.Http
          });
          listener.addTargets("ServiceTarget", {
              port: 5000,
              protocol: ApplicationProtocol.Http,
              targets: [service]
          });
  
          // Output the DNS where you can access your service
          new cdk.Output(this, "LoadBalancerDNS", {
              value: loadBalancer.dnsName
          });
      }
  }
  </code></pre>
  
  <p>Thanks in advance.</p>