cli로 kms 키 만들고 사용해보자. 3 # 키 생성(기본값) - 키 생성하고 변수로 등록하자. # aws kms create-key --description "my text encrypt descript demo" CREATE_KEY_JSON=$(aws kms create-key --description "my text encrypt descript demo") echo $CREATE_KEY_JSON | jq [root@ip-172-31-61-209 ~]# echo $CREATE_KEY_JSON | jq { "KeyMetadata": { "Origin": "AWS_KMS", "KeyId": "b44d1", "Description": "my text encrypt descript demo", "KeyManager": "CUSTOMER", "EncryptionAlgorithms": [ "SYMMETRIC_DEFAULT" ], "Enabled": true, "CustomerMasterKeySpec": "SYMMETRIC_DEFAULT", 콘솔에서 확인 해당 리전으로 이동 > KMS로 이동 >고객 관리형키가 만들어져 있다. 4 # 키 ID를 뽑아낸다. KEY_ID=$(echo $CREATE_KEY_JSON | jq -r ".KeyMetadata.KeyId") echo $KEY_ID 5 # 키 alias 생성 export ALIAS_SUFFIX=<각자 닉네임> export ALIAS_SUFFIX=masterseo aws kms create-alias --alias-name alias/$ALIAS_SUFFIX --target-key-id $KEY_ID aws kms list-aliases # 별칭 확인 "AliasName": "alias/masterseo", # 콘솔에서 별칭 확인 6 CMK 평문 실습해보기 !!! # CMK로 평문을 암호화해보기, 파일로 만들기 echo "Hello 123123" > secrect.txt aws kms encrypt --key-id alias/$ALIAS_SUFFIX --cli-binary-format raw-in-base64-out --plaintext file://secrect.txt --output text --query CiphertextBlob | base64 --decode > secrect.txt.encrypted # 파일 암호문 확인 cat secrect.txt.encrypted # 파일 복호화 aws kms decrypt --ciphertext-blob fileb://secrect.txt.encrypted --output text --query Plaintext | base64 --decode Hello 123123 7 AWS CMK(KMS Key) 생성을 위해 Key policy 생성 현재 사용자가 CMK 권한을 가질 수 있게 코드 설정 # aws cli 로 현재 사용자 정보 확인 aws sts get-caller-identity | jq { "Account": "45xxxxxxxxxxx83", "UserId": "AIxxxxxxxxxxxxxxUYL", "Arn": "arn:aws:iam::45xxxxxxx83:user/aws111" } 8 13 실습 DB계정 정보가 포함된 db-creds.yml 파일을 생성 cat < db-creds.yml username: admin password: password EOT 14 암호화 aws kms encrypt --key-id alias/$ALIAS_SUFFIX --cli-binary-format raw-in-base64-out --plaintext file://db-creds.yml --output text --query CiphertextBlob | base64 --decode | tee db-creds.yml.encrypted _▒7▒7FH▒3▒4U▒B(@'(▒j▒▒[▒▒ aws kms encrypt --key-id alias/$ALIAS_SUFFIX --cli-binary-format raw-in-base64-out --plaintext file://db-creds.yml --output text --query CiphertextBlob | tee db-creds.yml.encrypted2 AQICAHgSmCTHsrGP27rx/drzj4pFwzYY7U0/Z // 이 타입만 됨 # 암호문 확인 cat db-creds.yml.encrypted cat db-creds.yml.encrypted2 # 복호화해보기 cat db-creds.yml.encrypted = 복호화 됨 cat db-creds.yml.encrypted2 = 복호화 안됨 aws kms decrypt --ciphertext-blob fileb://db-creds.yml.encrypted --output text --query Plaintext | base64 --decode username: admin password: password 19 rds.tf 코드 파일 생성 cat < rds.tf provider "aws" { region = "ap-northeast-2" } data "aws_kms_secrets" "creds" { secret { name = "db" payload = file("\${path.module}/db-creds.yml.encrypted2") } } locals { db_creds = yamldecode(data.aws_kms_secrets.creds.plaintext["db"]) } resource "aws_db_instance" "example" { identifier_prefix = "terraform-up-and-running" engine = "mysql" allocated_storage = 10 instance_class = "db.t2.micro" skip_final_snapshot = true db_name = var.db_name # Pass the secrets to the resource username = local.db_creds.username password = local.db_creds.password } EOT 20 variables.tf 코드 파일 생성 cat < variables.tf variable "db_name" { description = "The name to use for the database" type = string default = "example" } EOT 21 outputs.tf 코드 파일 생성 cat < outputs.tf output "address" { value = aws_db_instance.example.address description = "Connect to the database at this endpoint" } output "port" { value = aws_db_instance.example.port description = "The port the database is listening on" } EOT cat db-creds.yml.encrypted2 Q3RkiGMwGLX2TJ/WrPQJntcHP3CEQjAAAAgTB/BgkqhkiG9w0BBwa 22 확인 terraform init terraform plan terraform apply -auto-approve 23 확인 aws rds describe-db-instances --query "*[].[DBInstanceIdentifier,MasterUsername,Endpoint.Address,Endpoint.Port]" --output text