🐮

AWS Fargate 複数サービス立ち上げる

2021/12/30に公開

前回の続き
https://zenn.dev/marumarumeruru/articles/129dd85f7f598e

1つのクラスターに複数サービス立ち上げます
ALB配下のECS FargateのコンテナにECRのイメージをそれぞれホストします
新規のVPCを利用します

lib/work-stack.ts
import * as cdk from '@aws-cdk/core';
import * as ecs from '@aws-cdk/aws-ecs';
import * as ecsp from '@aws-cdk/aws-ecs-patterns';
import * as elb from '@aws-cdk/aws-elasticloadbalancingv2';
import * as ecr from '@aws-cdk/aws-ecr';
import * as route53 from '@aws-cdk/aws-route53';

type serviceProps = {
  hostedZoneId: string,
  zoneName: string,
  domainNameforFront: string,
  domainNameforApi: string
}
export class WorkStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, serviceProps: serviceProps , props?: cdk.StackProps){
    super(scope, id, props);

    const ecrRepoApi = ecr.Repository.fromRepositoryName(this,'EcrApi','chat-server');
    const ecrRepoFront = ecr.Repository.fromRepositoryName(this,'EcrFront','nginx-react');

    const hostedZone = route53.HostedZone.fromHostedZoneAttributes(this,'HostedZone',{
      hostedZoneId: serviceProps.hostedZoneId,
      zoneName: serviceProps.zoneName
    });

    // API server
    new ecsp.ApplicationLoadBalancedFargateService(this, 'APIService', {
      protocol: elb.ApplicationProtocol.HTTPS,
      domainName: serviceProps.domainNameforApi,
      domainZone: hostedZone,
      taskImageOptions: {
        image: ecs.ContainerImage.fromEcrRepository(ecrRepoApi),
        containerPort: 3001
      }
    });

    // Web server
    new ecsp.ApplicationLoadBalancedFargateService(this, 'FrontService', {
      protocol: elb.ApplicationProtocol.HTTPS,
      domainName: serviceProps.domainNameforFront,
      domainZone: hostedZone,
      taskImageOptions: {
        image: ecs.ContainerImage.fromEcrRepository(ecrRepoFront),
      }
    });
  }
}

install

vpcが変更になるような大掛かりな変更の場合、
cdk deployすると失敗してしまう
一旦削除して作り直すしかないみたい

cdk destroy
cdk deploy

Discussion