LoginSignup
2
3

More than 3 years have passed since last update.

AWS CDKをTypeScriptで使い始める単純な例

Posted at

AWS CDKとTypeScriptのインストール

sudo npm install -g aws-cdk
sudo npm install -g typescript
  • macOSの人はsudoは不要
  • typescriptがインストール済みの場合typescriptは不要

作業用ディレクトリ作成

mkdir my-cdk && cd my-cdk

テンプレートプロジェクトの作成

cdk init app --language typescript

EC2パッケージを入れる

npm install @aws-cdk/aws-ec2

@aws-cdk_aws-ec2.Instance

サンプルのファイルを開く

ファイルは、lib/tutorial-stack.tsにある。

import * as cdk from '@aws-cdk/core';

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

    // The code that defines your stack goes here
  }
}

サンプルファイルを編集する

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

    const vpc = new ec2.Vpc(this, 'TheVPC', {
      cidr: "10.0.0.0/16"
    })

    const i = new ec2.Instance(this, id, {
      instanceType: new ec2.InstanceType("t2.medium"),
      machineImage: new ec2.AmazonLinuxImage,
      vpc: vpc,
    })
  }
}

デプロイする

cdk deploy

参考

2
3
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
2
3