AWS CDK

 

aws cdk 이미지 검색결과

CDK (Cloud Development Kit)

익숙한 프로그래밍 언어를 사용하여 AWS 클라우드 애플리케이션 리소스를 모델링 및 프로비저닝할 수 있는 오픈 소스 소프트웨어 개발 프레임워크입니다.

AWS CloudFormation을 통해 리소스를 안전하고 반복 가능한 방식으로 프로비저닝합니다.

aws-cdk 는 typescript 로 개발 되었습니다.

typescript, python, java, c# 개발언어를 지원합니다.

관련 사이트들은 아래에 있습니다.

  1. 공식 사이트 : https://aws.amazon.com/ko/cdk/
  2. cdk document : https://docs.aws.amazon.com/cdk/index.html
  3. cdk api document : https://docs.aws.amazon.com/cdk/api/latest/index.html
  4. cdk github repository : https://github.com/aws/aws-cdk
  5. cdk workshop : https://cdkworkshop.com/
  6. cdk example repository : https://github.com/aws-samples/aws-cdk-examples
  7. cdk toolkit  정보 : https://docs.aws.amazon.com/cdk/latest/guide/tools.html
  8. cdk gitter channel : https://gitter.im/awslabs/aws-cdk

Why CDK

CDK vs CloudFormation Template   :    "7 Lines VS 700 Lines"


준비

  1. 반듯이 Node.js ( >= 10.3.0 ) 가 설치되어 있어야 합니다.
  2. typescript 를 사용하지 않더라도 Node.js ( >= 10.3.0 ) 가 설치되어 있어야 합니다.
  3. AWS CDK ToolKit 은 typescript로 개발되어 node.js에서 실행되어야 하기 때문입니다.
  4. 개발언어는 각각 typescript ( >= 2.7 ), python ( >=3.6 ), java ( maven 3.5.4 or later and java 8 ), c# ( .NET core >= 3.0, .NET framework >= 4.6.1 or Mono >= 5.4 ) 필요합니다. 

설치

아래 command로 설치해줍니다.

npm install -g aws-cdk

워크 플로우

  1. Create the app directory
  2. Initialize the app
  3. Add code to the app
  4. Compile the app
  5. Synthesizing an AWS CloudFormation Template
  6. Deploy the resources defined in the app
  7. Modifying the app
  8. Preparing for Deployment
  9. Deploy the resources defined in the app
  10. Destroying the App's Resources

Create the app directory

mkdir hello-cdk
cd hello-cdk

initialize the app

여기서는 typescript 로 진행하겠습니다.

cdk init --language typescript

Add code to the app

여기서는 s3 bucket 1개를 생성합니다.

 프로젝트에 aws-cdk/aws-s3 패키지를 설치합니다.

npm install @aws-cdk/aws-s3

lib/hello-cdk-stack.ts 파일에  s3bucket 리소스 정보를 입력해줍니다.

아래 내용 중  'MyFirstBucket' 은 Amazon S3 버킷의 실제 이름이 아닌 버킷 구성 ID 입니다.

import core = require('@aws-cdk/core');
import s3 = require('@aws-cdk/aws-s3');
 
export class HelloCdkStack extends core.Stack {
  constructor(scope: core.App, id: string, props?: core.StackProps) {
    super(scope, id, props);
 
    new s3.Bucket(this'MyFirstBucket', {
      versioned: true
    });
  }
}

Compile the app

npm run build

Synthesizing an AWS CloudFormation Template

아래 명령으로 HelloCdkStack 에 대한 AWS CloudFomation Template 가 만들어 집니다.

cdk synth

아래와 같은 결과값이 출력됩니다.

Resources:
  MyFirstBucketB8884501:
    Type: AWS::S3::Bucket
    Properties:
      VersioningConfiguration:
        Status: Enabled
    Metadata:
      aws:cdk:path: HelloCdkStack/MyFirstBucket/Resource
  CDKMetadata:
    Type: AWS::CDK::Metadata
    Properties:
      Modules: "@aws-cdk/aws-codepipeline-api=VERSION,@aws-cdk/aws-events=VERSION,@aws-c\
        dk/aws-iam=VERSION,@aws-cdk/aws-kms=VERSION,@aws-cdk/aws-s3=VERSION,@aws-c\
        dk/aws-s3-notifications=VERSION,@aws-cdk/cdk=VERSION,@aws-cdk/cx-api=VERSION\
        .0,hello-cdk=0.1.0"

Deploy the resources defined in the app

cdk deploy

Modifying the app

아래와 같이 s3에서 aws Key Management Service (KMS)를 사용하게 변경합니다.

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

아래 명렁어로 컴파일 합니다.

npm run build

Preparing for Deployment

앱을 배포하기 전에 배포된 앱과의 차이점을 확인할 수 있습니다.

cdk diff

아래와 같은 결과값이 출력됩니다.

Stack HelloCdkStack
Resources
[~] AWS::S3::Bucket MyFirstBucket MyFirstBucketB8884501
 |- [+] BucketEncryption
     |- {"ServerSideEncryptionConfiguration":[{"ServerSideEncryptionByDefault":{"SSEAlgorithm":"aws:kms"}}]}

Deploy the resources defined in the app

cdk deploy

Destroying the App's Resources

cdk destroy


댓글

이 블로그의 인기 게시물

dailyFocus - Frontend

CDK - Assets

About VPC