[AWS CDK]ECS FargateでNestJSで作成したRESTfull APIデプロイ

[AWS CDK]ECS FargateでNestJSで作成したRESTfull APIデプロイ

2022-05-2415 min read

目次

  1. 概要
  2. プロジェクト作成
  3. stack実装
  4. nestjsアプリケーションの実装
  5. デプロイ
  6. 参考にしたサイト

概要

NestJSで作成したRESTfull APIアプリケーションをECS Fargateで起動するスタックを、AWS CDKを利用して構築・デプロイした際のメモです。

プロジェクト作成

作業ディレクトリを作成しCDKプロジェクトとして初期化します。

mkdir ecs-fargate 
npx cdk init app --language=typescript
cd ecs-fargate

Stack実装

Stackを次のコードのように実装します。

ポイントは以下となります。

  • VPC構築を構築しALBをパブリックサブネットに配置
  • ECS Fargate を構築しALBからのリクエストを受け付ける
  • ACMを作成しALBで利用
  • Route53の設定でALBに対してAレコードを設定

lib/ecs-fargate.ts

import {
  aws_certificatemanager as acm,
  aws_ec2 as ec2,
  aws_ecs as ecs,
  aws_elasticloadbalancingv2 as elbv2,
  aws_logs as log,
  aws_route53 as route53,
  aws_route53_targets as route53Targets,
  Stack,
  StackProps,
} from 'aws-cdk-lib';
import { Construct } from 'constructs';

// todo:
// example.com は置き換えること
const domainName = `example.com`;

export class CdkEcsFargateStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    const vpc = new ec2.Vpc(this, 'VPC', {
      cidr: '10.1.0.0/16',
      enableDnsHostnames: true,
      enableDnsSupport: true,
      subnetConfiguration: [
        {
          cidrMask: 24,
          name: 'PublicSubnet',
          subnetType: ec2.SubnetType.PUBLIC,
        },
        {
          cidrMask: 24,
          name: 'PrivateIsolatedSubnet',
          subnetType: ec2.SubnetType.PRIVATE_ISOLATED,
        },
      ],
    });
    // SecurityGroup
    const securityGroupELB = new ec2.SecurityGroup(this, 'SecurityGroupELB', {
      vpc,
    });
    securityGroupELB.addIngressRule(
      ec2.Peer.ipv4('0.0.0.0/0'),
      ec2.Port.tcp(443),
    );

    const securityGroupApp = new ec2.SecurityGroup(this, 'SecurityGroupApp', {
      vpc,
    });

    const hostedZone = route53.HostedZone.fromLookup(this, 'HostedZone', {
      domainName: domainName,
    });

    const cert = new acm.DnsValidatedCertificate(this, 'Certificate', {
      domainName: domainName,
      hostedZone,
      // region: "us-east-1",
      region: 'ap-northeast-1', // ALBと同じリージョンに配置
    });

    // ALB
    const alb = new elbv2.ApplicationLoadBalancer(this, 'ALB', {
      vpc,
      securityGroup: securityGroupELB,
      internetFacing: true,
    });
    const listenerHTTP = alb.addListener('ListenerHTTP', {
      port: 443,
      certificates: [
        {
          certificateArn: cert.certificateArn,
        },
      ],
    });
    // Target Group
    const targetGroup = new elbv2.ApplicationTargetGroup(this, 'TargetGroup', {
      vpc: vpc,
      port: 3000,
      protocol: elbv2.ApplicationProtocol.HTTP,
      targetType: elbv2.TargetType.IP,
      healthCheck: {
        path: '/',
        healthyHttpCodes: '200',
      },
    });

    listenerHTTP.addTargetGroups('DefaultHTTPSResponse', {
      targetGroups: [targetGroup],
    });

    // ECS Cluster
    const cluster = new ecs.Cluster(this, 'Cluster', {
      vpc,
    });

    // Fargate
    const fargateTaskDefinition = new ecs.FargateTaskDefinition(
      this,
      'TaskDef',
      {
        memoryLimitMiB: 1024,
        cpu: 512,
      },
    );
    const container = fargateTaskDefinition.addContainer('AppContainer', {
      image: ecs.ContainerImage.fromAsset('src/ecs/app'),
      logging: ecs.LogDrivers.awsLogs({
        streamPrefix: 'nest-app',
        logRetention: log.RetentionDays.ONE_MONTH,
      }),
    });
    container.addPortMappings({
      containerPort: 3000,
      hostPort: 3000,
    });
    const service = new ecs.FargateService(this, 'Service', {
      cluster,
      taskDefinition: fargateTaskDefinition,
      desiredCount: 1,
      assignPublicIp: true,
      securityGroups: [securityGroupApp],
    });
    service.attachToApplicationTargetGroup(targetGroup);

    new route53.ARecord(this, `AliasRecord`, {
      zone: hostedZone,
      recordName: `ecs.${domainName}`,
      target: route53.RecordTarget.fromAlias(
        new route53Targets.LoadBalancerTarget(alb),
      ),
    });
  }
}

NestJSアプリケーションの実装

NestJSでRESTfullAPIアプリケーションを実装していきます。

プロジェクト作成

mkdir -p src/ecr
cd src/ecr
nest new app
cd app

サンプルAPI作成

デフォルトで作成されるcontroller・serviceを次のように実装します。

src/app.controller.ts

import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  getHello() {
    return this.appService.getHello();
  }
}

src/app.service.ts

import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
  getHello() {
    return {
      message: 'Hello World!',
    };
  }
}

npm run startを実行して http://localhost:3000{ "message": "Hello World!" }のレスポンスが返ってくることを確認します。

またビルドするためのDockerfileも作成します。

Dockerfile

FROM node:16-alpine

WORKDIR /app
COPY . /app
RUN npm install && \
    npm run build
ENV HOST 0.0.0.0
EXPOSE 3000
CMD ["npm", "run", "start:prod"]

デプロイ

cdk deployを実行しAWS環境へデプロイを行います。

https://ecs.example.com (自分が設定したFQDNに置き換える) に接続しレスポンスが正しいことを確認します。

参考にしたサイト

Tags
javascript(109)
linux(54)
node.js(53)
amazon%20aws(47)
typescript(44)
%E3%82%A2%E3%83%AB%E3%82%B4%E3%83%AA%E3%82%BA%E3%83%A0(36)
%E7%94%BB%E5%83%8F%E5%87%A6%E7%90%86(30)
html5(29)
php(24)
centos(24)
python(22)
%E7%AB%B6%E6%8A%80%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0(21)
mac(21)
mysql(20)
canvas(19)
opencv(17)
%E9%9B%91%E8%AB%87(16)
docker(16)
wordpress(15)
atcoder(14)
apache(12)
%E6%A9%9F%E6%A2%B0%E5%AD%A6%E7%BF%92(12)
%E3%83%87%E3%83%BC%E3%82%BF%E3%83%99%E3%83%BC%E3%82%B9(12)
amazon%20s3(12)
red%20hat(12)
prisma(12)
ubuntu(11)
github(10)
git(10)
vue.js(10)
%E7%94%BB%E5%83%8F%E5%87%A6%E7%90%86100%E6%9C%AC%E3%83%8E%E3%83%83%E3%82%AF(10)
mariadb(10)
react(9)
aws%20cdk(9)
css3(8)
%E5%8F%AF%E8%A6%96%E5%8C%96(8)
%E5%B0%8F%E3%83%8D%E3%82%BF(8)
nestjs(8)
amazon%20lightsail(7)
next.js(7)
%E3%83%96%E3%83%AD%E3%82%B0(6)
cms(6)
oracle(6)
perl(6)
gitlab(6)
iam(5)
amazon%20ec2(5)
%E8%B3%87%E6%A0%BC%E8%A9%A6%E9%A8%93(5)
aws%20amplify(5)
curl(4)
Author
githubzennqiita
ただの備忘録です。

※外部送信に関する公表事項