🎄

AWS FargateをCDKで立ち上げる

2021/12/30に公開

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

今回は、ECRに登録してあるイメージを、
ALB配下のECS Fargateのコンテナ2台にホストします
既存のVPC、既存のパブリックサブネット上にデプロイします。

VSCodeの 拡張
https://marketplace.visualstudio.com/items?itemName=mklein.cdk-snippets

install

dependenciesを変更 バージョンは揃えています

package.json
{
  "name": "work",
  "version": "0.1.0",
  "bin": {
    "work": "bin/work.js"
  },
  "scripts": {
    "build": "tsc",
    "watch": "tsc -w",
    "test": "jest",
    "cdk": "cdk"
  },
  "devDependencies": {
    "@aws-cdk/assertions": "1.134.0",
    "@types/jest": "^26.0.10",
    "@types/node": "10.17.27",
    "aws-cdk": "1.134.0",
    "jest": "^26.4.2",
    "ts-jest": "^26.2.0",
    "ts-node": "^9.0.0",
    "typescript": "~3.9.7"
  },
  "dependencies": {
    "@aws-cdk/aws-ecr": "^1.134.0",
    "@aws-cdk/aws-ecs": "^1.134.0",
    "@aws-cdk/aws-ecs-patterns": "^1.134.0",
    "@aws-cdk/core": "^1.134.0",
    "@aws-cdk/aws-elasticloadbalancingv2": "^1.134.0",
    "@aws-cdk/aws-ec2": "^1.134.0",
    "@aws-cdk/aws-route53": "^1.134.0"
  }
}
work $ npm install

ALB→Fargateの構成

ECRに登録してあるport3001で動くアプリを、既存の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';
import * as ec2 from '@aws-cdk/aws-ec2';

type serviceProps = {
  vpcId: string,
  hostedZoneId: string,
  zoneName: string,
  domainName: string
}
export class WorkStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, serviceProps: serviceProps , props?: cdk.StackProps){
    super(scope, id, props);
    const vpc = ec2.Vpc.fromLookup(this, 'Vpc', {
      vpcId: serviceProps.vpcId
    });

    const vpcSubnets = vpc.selectSubnets({
      subnetType: ec2.SubnetType.PUBLIC
   });

    const ecrRepo = ecr.Repository.fromRepositoryName(this,'Ecr','chat-server');

    new ecsp.ApplicationLoadBalancedFargateService(this, 'Service', {
      vpc: vpc,
      taskSubnets: vpcSubnets,
      desiredCount: 2,           // 2台構成
      memoryLimitMiB: 512,
      cpu: 256,                  // 256=0.25vCPU
      protocol: elb.ApplicationProtocol.HTTPS,
      domainName: serviceProps.domainName,
      domainZone: route53.HostedZone.fromHostedZoneAttributes(this,'HostedZone',{
        hostedZoneId: serviceProps.hostedZoneId,
        zoneName: serviceProps.zoneName
      }),
      taskImageOptions: {
        image: ecs.ContainerImage.fromEcrRepository(ecrRepo),
        containerPort: 3001
      },
      publicLoadBalancer: true,
      assignPublicIp: true,  // trueにしないとdeployに失敗する
      deploymentController:{
        type: ecs.DeploymentControllerType.ECS
        // ECS=rolling update
        // CODE_DEPLOY=blue/green
      }
    });
  }
}

assignPublicIp:trueにしないとdeploy中に止まってしまい、進まなくなってしまいました。。
VPCを新規作成する場合はfalseのままでうまくいくので、既存のVPCの状態が悪いのか。。。
原因わかってないです。。。

bin/work.js
#!/usr/bin/env node
import * as cdk from '@aws-cdk/core';
import { WorkStack } from '../lib/work-stack';

const app = new cdk.App();
new WorkStack(app, 'dev',
  {
    vpcId: 'vpc-xxxxxxxxx',
    hostedZoneId: 'Zxxxxxxxxxxx',
    zoneName: 'xxxxxxxxx',
    domainName: 'xxxxxxx'
  },
  {
    env: {
      account: 'xxxxxxxxxxx',
      region: 'ap-northeast-1'
    }
  }
);
new WorkStack(app, 'production',
  {
    vpcId: 'vpc-xxxxxxxxx',
    hostedZoneId: 'Zxxxxxxxxxxx',
    zoneName: 'xxxxxxxxx',
    domainName: 'xxxxxxx'
  },
  {
    env: {
      account: 'xxxxxxxxxxx',
      region: 'ap-northeast-1'
    }
  }
);

環境ごとに異なる部分は、work.tsの方に外出ししました
こうすることで、

work $ cdk list
dev
production

のように選択できるので、環境を指定してデプロイすることができます

work $ cdk deploy dev

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-ec2.Vpc.html

https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-route53.HostedZone.html

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

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

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

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

Discussion