2
2

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 5 years have passed since last update.

AWS Cloud Development Kitをためしてみる (Javascript)

Posted at

AWS Cloud Development Kitを試してみます。

##参考
https://github.com/awslabs/aws-cdk
https://awslabs.github.io/aws-cdk/
https://awslabs.github.io/aws-cdk/getting-started.html
https://awslabs.github.io/aws-cdk/reference.html

##Getting startedをベースに試してみる

$ npm install -g aws-cdk
/Users/atsum/.nodebrew/node/v9.11.1/bin/cdk -> /Users/atsum/.nodebrew/node/v9.11.1/lib/node_modules/aws-cdk/bin/cdk
+ aws-cdk@0.18.1
added 265 packages in 30.324
$ cdk --version
0.18.1 (build 9f7af21)

プロジェクトを作成する

mkdir hello-cdk
cd hello-cdk
git init
npm init -y

$ npm init -y
Wrote to /Users/atsum/Work/cdk/hello-cdk/package.json:

{
  "name": "hello-cdk",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

依存関係の追加

npm install @aws-cdk/cdk

npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN hello-cdk@1.0.0 No description
npm WARN hello-cdk@1.0.0 No repository field.

+ @aws-cdk/cdk@0.18.1
added 10 packages in 3.275s

AWS CDK Appの定義

$ emacs index.js

const cdk = require('@aws-cdk/cdk');

class MyStack extends cdk.Stack {
    constructor(parent, id, props) {
        super(parent, id, props);
    }
}

class MyApp extends cdk.App {
    constructor(argv) {
        super(argv);

        new MyStack(this, 'hello-cdk');
    }
}

new MyApp().run();

クレデンシャル設定

$ aws configure
AWS Access Key ID [****************XHSQ]: ************
AWS Secret Access Key [****************f4/2]: **************
Default region name [ap-northeast-1]: 
Default output format [None]: 

デフォルトで実行するappを指定する

$ emacs cdk.json
{
  "app": "node index.js"
}

stackの確認

$ cdk ls -l
- name: hello-cdk
  environment:
    name: 793493288704/ap-northeast-1
    account: "793493288704"
    region: ap-northeast-1

テンプレートの元となるスクリプトを記載する

emacs index.js
const cdk = require('@aws-cdk/cdk');

class MyStack extends cdk.Stack {
    constructor(parent, id, props) {
        super(parent, id, props);
    }
}

class MyApp extends cdk.App {
    constructor(argv) {
        super(argv);

        new MyStack(this, 'hello-cdk');
    }
}

new MyApp().run();

jsからテンプレートに変換

$ cdk synth hello-cdk
Resources:
  CDKMetadata:
    Type: AWS::CDK::Metadata
    Properties:
      Modules: "@aws-cdk/cdk=0.18.1,@aws-cdk/cx-api=0.18.1,hello-cdk=1.0.0"

デプロイする

$ cdk deploy
hello-cdk: deploying...
hello-cdk: creating CloudFormation changeset...
 0/2 | 16:17:37 | CREATE_IN_PROGRESS   | AWS::CloudFormation::Stack | hello-cdk User Initiated
 0/2 | 16:17:42 | CREATE_IN_PROGRESS   | AWS::CDK::Metadata | CDKMetadata 
 0/2 | 16:17:45 | CREATE_IN_PROGRESS   | AWS::CDK::Metadata | CDKMetadata Resource creation Initiated
 1/2 | 16:17:45 | CREATE_COMPLETE      | AWS::CDK::Metadata | CDKMetadata 
 2/2 | 16:17:47 | CREATE_COMPLETE      | AWS::CloudFormation::Stack | hello-cdk 

 ✅  hello-cdk

Stack ARN:
arn:aws:cloudformation:ap-northeast-1:793493288704:stack/hello-cdk/ad103a90-ee26-11e8-a16d-0a5da605f772

image.png

##S3バケットを作成

$ cat index.js 
const cdk = require('@aws-cdk/cdk');
const s3 = require('@aws-cdk/aws-s3');

class MyStack extends cdk.Stack {
    constructor(parent, id, props) {
        super(parent, id, props);

        new s3.Bucket(this, 'MyFirstBucket', {
		versioned: true
		    });
    }
}


class MyApp extends cdk.App {
    constructor(argv) {
        super(argv);

        new MyStack(this, 'hello-cdk');
    }
}

new MyApp().run();
$ npm install @aws-cdk/aws-s3
npm WARN hello-cdk@1.0.0 No description
npm WARN hello-cdk@1.0.0 No repository field.

+ @aws-cdk/aws-s3@0.18.1
added 6 packages in 3.993s
$ cdk synth hello-cdk
Resources:
  MyFirstBucketB8884501:
    Type: AWS::S3::Bucket
    Properties:
      VersioningConfiguration:
        Status: Enabled
    Metadata:
      aws:cdk:path: hello-cdk/MyFirstBucket/Resource
  CDKMetadata:
    Type: AWS::CDK::Metadata
    Properties:
      Modules: "@aws-cdk/aws-codepipeline-api=0.18.1,@aws-cdk/aws-events=0.18.1,@aws-c\
        dk/aws-iam=0.18.1,@aws-cdk/aws-kms=0.18.1,@aws-cdk/aws-s3=0.18.1,@aws-c\
        dk/aws-s3-notifications=0.18.1,@aws-cdk/cdk=0.18.1,@aws-cdk/cx-api=0.18\
        .1,hello-cdk=1.0.0"

image.png

##約束

投稿内容は私個人の意見であり、所属企業・部門見解を代表するものではありません。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?