🐶

AWS CDK デプロイ速度向上

2021/12/30に公開
1

前回の続き
https://zenn.dev/marumarumeruru/articles/9d4a38c604b732

いままで我慢していましたが、20分以上とか、あまりにもデプロイに時間がかかるので、設定値を調べてみたところ、ターゲットグループの設定値が原因であることがわかりました。

before



開発中はとりあえずこの値を短くして、デプロイ時間の短縮を図りたいと思います

after



3分ぐらいでデプロイできるようになりました!

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';
import { Duration } from '@aws-cdk/core';

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
    const apiService:ecsp.ApplicationLoadBalancedFargateService =
       new ecsp.ApplicationLoadBalancedFargateService(this, 'APIService', {
      protocol: elb.ApplicationProtocol.HTTPS,
      domainName: serviceProps.domainNameforApi,
      domainZone: hostedZone,
      taskImageOptions: {
        image: ecs.ContainerImage.fromEcrRepository(ecrRepoApi,'v1.5.0'),
        containerPort: 3001
      },
      healthCheckGracePeriod: Duration.seconds(10), // 60 -> 10
    });
    apiService.targetGroup.setAttribute(
      'deregistration_delay.timeout_seconds',
      '10'                              //300 -> 10
    )
    apiService.targetGroup.configureHealthCheck({
      healthyThresholdCount: 3,         // 5 -> 3
      interval: Duration.seconds(10)    // 30 -> 10
      });

    // Web server
    const frontService:ecsp.ApplicationLoadBalancedFargateService =
        new ecsp.ApplicationLoadBalancedFargateService(this, 'FrontService', {
      protocol: elb.ApplicationProtocol.HTTPS,
      domainName: serviceProps.domainNameforFront,
      domainZone: hostedZone,
      taskImageOptions: {
        image: ecs.ContainerImage.fromEcrRepository(ecrRepoFront,'v1.8.0')
      },
      healthCheckGracePeriod: Duration.seconds(10), // 60 -> 10
    });
    frontService.targetGroup.setAttribute(
      'deregistration_delay.timeout_seconds',
      '10'                               // 300 -> 10
    )
    frontService.targetGroup.configureHealthCheck({
      healthyThresholdCount: 3,          // 5 -> 3
      interval: Duration.seconds(10)     // 30 -> 10
    });
  }
}

link

https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-ecs-patterns.ApplicationLoadBalancedFargateService.html

https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-elasticloadbalancingv2.ApplicationTargetGroup.html

https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-elasticloadbalancingv2.HealthCheck.html

https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html

Discussion