terraform-cdk로 AWS에 리소스 배포
                                            
                                                
                                                
                                                
                                                
                                                
                                                 6829 단어  CDKTypeScriptTerraform
                    
여러분 AWS CDK(Cloud Development Kit) 은 아시나요?
맞습니다. 그 CloudFormation과 SAM 템플릿 쓰기 지옥에 일근의 빛이 ...
개별 자원뿐만 아니라 인프라 구성의 모범 사례가 클래스화되어 있으며, 그 클래스를 커스터마이즈 해 사용할 수 있어, 일반적인 패턴의 인프라 구성이라면 일부러 종속성을 정리해 자원 정의 쓰지 않아도 좋아지는 아주 좋은 도구입니다.
TypeScript, JavaScript, Python, Java, C#에서 인프라 구성을 정의하여 cli에서 배포할 수 있습니다.
CI에도 심플하게 짜넣을 것 같습니다.
그리고 요전날 그 CDK에서 프로바이더로서 terraform을 사용할 수 있는 terraform-cdk 되는 것이 발표되어 주목하고 있었습니다.
CDK의 공급자로서 terraform을 사용하면 AWS뿐만 아니라 다른 클라우드 플랫폼의 리소스도 관리할 수 있게 되는 뛰어난 것입니다.
좋은 기회이므로 terraform-cdk 을 사용하여 실제로 AWS에 리소스를 배포하려고합니다.
절차
 전제 조건
OS: macOS
다음 설치
1. terraform-cdk-example 을 clone
terraform-cdk의 저장소 examples 을 그대로 사용하려고 했는데 아직 조금 버그가 있거나 문서가 부족한 곳이 있었기 때문에 본 기사용 템플릿 리포지토리를 만들어 공개했습니다.
2. 필요한 패키지 설치
cd terraform-cdk-examplenpm install3. cdktf.json에 공급자와 사용 언어 설정
샘플을 그대로 움직이는 경우는 편집 불필요합니다.
example 에서는
language 를 typescript 로 설정하고 terraformProviders 에는 aws 만 지정합니다.GCP 또는 Azure와 같은 다른 공급자를 사용하는 경우
terraformProviders에 추가하십시오.예 : htps : // 기주 b. 코 m/하시코 rp/테라후 rmcdk/bぉb/마s r/에ぁmpぇs/tyぺsc리pt/오오gぇ/cdktf. j 그런
4. 빌드
npm run build※ cdktf get 가 실행되어 cdk-terraform용의 aws 프로바이더등이 .gen 디렉토리에 저장됩니다.5. tf 파일 생성
npm run synth※ cdktf.out 안에 cdk.tf.json 가 만들어집니다.6. 배포용 액세스 키 만들기
AWS IAM에서 DynamoDB 테이블의 create/delete를 할 수 있는 권한을 부여하고 사용자 생성하여 액세스 키/시크릿 키 획득
(참고: 공식 Doc )
7. 배포용 액세스 키 설정
cdktf.out 의 아래의 부분(20행당)에 액세스 키를 추기합니다. (실제는 var 등에서 이용하도록 할 수 있으면 좋다)  "provider": {
    "aws": [
      {
        "region": "ap-northeast-1",
        "access_key": "[YOUR_ACCESS_KEY]", ←追加
        "secret_key": "[YOUR_SECRET_KEY]"  ←追加
      }
    ]
  }
}
8. Plan & Apply
npm run plannpm run apply※여기는
cd ./cdktf.out 하고 나서 terraform plan 와 terraform apply 하고 있을 뿐입니다.이런 로그가 터미널에 나옵니다.
> cd ./cdktf.out && terraform apply
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create
Terraform will perform the following actions:
  # aws_dynamodb_table.helloterra_Hello_69872235 will be created
  + resource "aws_dynamodb_table" "helloterra_Hello_69872235" {
      + arn              = (known after apply)
      + billing_mode     = "PAY_PER_REQUEST"
      + hash_key         = "id"
      + id               = (known after apply)
      + name             = "my-first-table-ap-northeast-1"
      + stream_arn       = (known after apply)
      + stream_label     = (known after apply)
      + stream_view_type = (known after apply)
      + attribute {
          + name = "id"
          + type = "S"
        }
      + point_in_time_recovery {
          + enabled = (known after apply)
        }
      + server_side_encryption {
          + enabled     = (known after apply)
          + kms_key_arn = (known after apply)
        }
    }
Plan: 1 to add, 0 to change, 0 to destroy.
Changes to Outputs:
  ~ helloterra_tablename_E066AD25 = "my-first-table-eu-central-1" -> "my-first-table-ap-northeast-1"
Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.
  Enter a value: yes
aws_dynamodb_table.helloterra_Hello_69872235: Creating...
aws_dynamodb_table.helloterra_Hello_69872235: Creation complete after 7s [id=my-first-table-ap-northeast-1]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
helloterra_tablename_E066AD25 = "my-first-table-ap-northeast-1"
AWS Management console에서 확인해 보면...?

DynamoDB 테이블이 배포되었습니다.
요약
CDK를 TypeScript등의 형태가 있는 언어로 사용하면, IDE의 지원도 받으면서 IAC가 쓸 수 있어 매우 쾌적하다고 느낍니다. (CloudFormation으로 쓰고 있을 때는 Resource나 Parameter의 형태를 조사하면서 자원 정의해 가는 작업이 되어 매우 시간과 노력이 걸려 버렸습니다). 또한 CloudFormation보다 구성이 추상화·템플릿화되어 있고 기술량이 줄어드는 것도 장점입니다.
그 CDK가 terraform에서도 사용할 수 있다고 듣고 실제로 시험해 보았습니다.
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(terraform-cdk로 AWS에 리소스 배포), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/Banchi0123/items/b99d8d5c0e66a663663f
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                 우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
Reference
이 문제에 관하여(terraform-cdk로 AWS에 리소스 배포), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/Banchi0123/items/b99d8d5c0e66a663663f텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)