13
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWSCDKでVPCをデプロイする

Posted at

はじめに

AWSCDKはTypeScriptやPythonといったプログラミング言語でCloudFormationのテンプレートを生成できるフレームワークです。
今回はAWSCDK(TypeScript)でVPCをデプロイする書き方を紹介していきます。

必要なパッケージ

VPC関係の定義は @aws-cdk/aws-ec2 にあります。

$ npm i -D @aws-cdk/aws-ec2

Stackファイルを書く

import cdk = require("@aws-cdk/core")
import {
  DefaultInstanceTenancy,
  RouterType,
  Vpc,
  Subnet,
  CfnInternetGateway,
  CfnVPCGatewayAttachment
} from "@aws-cdk/aws-ec2"

export class CreateVPCStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props)

    const vpc = new Vpc(this, "cdk-vpc", {
      cidr: "10.0.0.0/16",
      defaultInstanceTenancy: DefaultInstanceTenancy.DEFAULT,
      enableDnsSupport: true,
      enableDnsHostnames: true,
      subnetConfiguration: []
    })

    const pubSubnet = new Subnet(this, "PublicSubnet1c", {
      availabilityZone: "us-west-2c",
      vpcId: vpc.vpcId,
      cidrBlock: "10.0.0.0/24"
    })
    new Subnet(this, "PrivateSubnet1c", {
      availabilityZone: "us-west-2c",
      vpcId: vpc.vpcId,
      cidrBlock: "10.0.1.0/24"
    })
    new Subnet(this, "PrivateSubnet1d", {
      availabilityZone: "us-west-2c",
      vpcId: vpc.vpcId,
      cidrBlock: "10.0.2.0/24"
    })

    const internetGateway = new CfnInternetGateway(this, "InternetGateway", {})
    new CfnVPCGatewayAttachment(this, "gateway", {
      vpcId: vpc.vpcId,
      internetGatewayId: internetGateway.ref
    })

    pubSubnet.addRoute("PubSubnetRoute", {
      routerType: RouterType.GATEWAY,
      routerId: internetGateway.ref
    })
  }
}

Stackファイルの説明

VPC

各パラメータを渡してVPCを構築します。
subnetConfiguration を指定しなければいくつかSubnetが用意されます。
今回はSubnetを自分で構築したいので、 subnetConfiguration[] を指定します。

const vpc = new Vpc(this, "cdk-vpc", {
  cidr: "10.0.0.0/16",
  defaultInstanceTenancy: DefaultInstanceTenancy.DEFAULT,
  enableDnsSupport: true,
  enableDnsHostnames: true,
  subnetConfiguration: []
})

Subnets

ここではPublicのSubnetを一つとPrivateのSubnetを2つ作っています。

const pubSubnet = new Subnet(this, "PublicSubnet1c", {
  availabilityZone: "us-west-2c",
  vpcId: vpc.vpcId,
  cidrBlock: "10.0.0.0/24"
})
new Subnet(this, "PrivateSubnet1c", {
  availabilityZone: "us-west-2c",
  vpcId: vpc.vpcId,
  cidrBlock: "10.0.1.0/24"
})
new Subnet(this, "PrivateSubnet1d", {
  availabilityZone: "us-west-2c",
  vpcId: vpc.vpcId,
  cidrBlock: "10.0.2.0/24"
})

PublicのSubnetに外からアクセスするためのインターフェースを定義します。

const internetGateway = new CfnInternetGateway(this, "InternetGateway", {})
new CfnVPCGatewayAttachment(this, "gateway", {
  vpcId: vpc.vpcId,
  internetGatewayId: internetGateway.ref
})

pubSubnet.addRoute("PubSubnetRoute", {
  routerType: RouterType.GATEWAY,
  routerId: internetGateway.ref
})

さいごに

これでVPCをCDKでデプロイすることができるようになりました。
次回はこのVPCを利用してRDSのデプロイをしていこうと思います。
ではまた!!!

13
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?