CloudFormation에서 Parameter Store 사용해 보기

CloudFormation에서 Parameter Store를 사용하는 방법에 대해서 정리해 봤습니다.
2022.08.12

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

안녕하세요 클래스메소드 김재욱(Kim Jaewook) 입니다. 이번에는 CloudFormation에서 Parameter Store를 사용하는 방법에 대해서 정리해 봤습니다.

Parameter Store에 관한 내용은 아래 블로그를 참고해 주세요.

CloudFormation에서 Parameter 불러오기

먼저 2개의 파라미터를 준비합니다.

각각 유형을 String, SecureString으로 생성합니다.

SecureString 값이 제대로 불러와지는지 테스트를 하기 위해서, Directory Service를 만들 생각이기 때문에 SecureString의 text는 Directory Service의 관리자 암호 규정을 따라주세요.

  • 암호에 “admin”이라는 말을 포함할 수 없습니다.
  • 디렉터리 관리자 암호는 대소문자를 구분하며 길이가 8~64자 사이여야 합니다. 또한 다음 네 범주 중 세 개에 해당하는 문자를 1자 이상 포함해야 합니다.
  • 소문자(a-z)
  • 대문자(A-Z)
  • 숫자(0-9)
  • 영숫자 외의 특수 문자(~!@#$%^&*_-+=`|\(){}[]:;“’<>,.?/)

보다 자세한 사항은 아래 링크를 참고해 주세요.

AWSTemplateFormatVersion: "2010-09-09"
Description: Create Directory Service
Metadata:
"AWS::CloudFormation::Interface":
ParameterGroups:
- Label:
default: "Directory Service Configuration"
Parameters:
- DnsName
- NetBiosName
- Edition
- StringParam
ParameterLabels:
DnsName:
default: "DNS Name of Directory Service"
NetBiosName:
default: "NetBios Name of Directory Service"
Edition:
default: "Edition of Directory Service"
StringParam:
default: "Test StringParam"

# Input Directory Service Parameters
Parameters:
DnsName:
Type: String
Default: "aws.test.com"

NetBiosName:
Type: String
Default: "AWS"

Edition:
Type: String
Default: "Standard"

StringParam:
Description: "test-string-param"
Default: test-string-param
Type: AWS::SSM::Parameter::Value

# Directory Service
Resources:
DirectoryService:
Type: AWS::DirectoryService::MicrosoftAD
Properties:
Name:
Ref : DnsName
Password: '{{resolve:ssm-secure:test-string-secure-param:1}}'
ShortName:
Ref: NetBiosName
Edition:
Ref : Edition
VpcSettings:
SubnetIds:
- { "Fn::ImportValue": !Sub "private-subnet-a" }
- { "Fn::ImportValue": !Sub "private-subnet-c" }
VpcId: { "Fn::ImportValue": !Sub "VPC" }

# DHCP Option set
DHCPOptionSet:
Type: AWS::EC2::DHCPOptions
Properties:
DomainName: !Ref DnsName
DomainNameServers:
- !Select [ '0', !GetAtt DirectoryService.DnsIpAddresses ]
- !Select [ '1', !GetAtt DirectoryService.DnsIpAddresses ]
Tags:
- Key: Name
Value: test-dopt

DHCPOptionAssociation:
Type: AWS::EC2::VPCDHCPOptionsAssociation
Properties:
DhcpOptionsId: !Ref DHCPOptionSet
VpcId: { "Fn::ImportValue": !Sub "VPC" }

# OutPut
Outputs:
# test-string-param
StringParam:
Value: !Ref StringParam
Export:
Name: !Sub "StringParam"

코드 자체는 CloudFormation으로 Directory Service를 생성하는 코드입니다.

여기서 주의 깊게 봐야할 부분은 아래와 같습니다.

Parameters:
StringParam:
Description: "test-string-param"
Default: test-string-param
Type: AWS::SSM::Parameter::Value

Password: '{{resolve:ssm-secure:test-string-secure-param:1}}'

CloudFormation의 Parameters에서 타입을 AWS::SSM::Parameter::Value로 선언해서 Parameter Store의 값을 불러올 수 있습니다.

이어서, Directory Service의 관리자 암호는 SecureString 유형의 파라미터를 불러왔기 때문에 아래와 같은 형식으로 불러올 필요가 있습니다.

  • ‘{{resolve:ssm-secure:parameter-name:version}}’
  • ‘{{resolve:ssm-secure:[a-zA-Z0-9_.-/]+:\\d+}}’

보다 자세한 사항은 아래 링크를 참고해 주세요.

이제 CloudFormation에서 스택을 생성할 때, 파라미터에서 test-string-param를 확인하고 스택을 생성합니다.

스택 생성이 끝나고 출력 결과를 살펴보면 문제 없이 파라미터에서 값을 불러와서 test-string-param을 출력하고 있는 것을 확인할 수 있습니다.

SecureString도 문제없이 적용되서 Directory Service가 생성된 것을 확인할 수 있습니다.

실제로 관리자 암호에 문제가 있다면 스택 생성시 에러가 발생하기 때문에, Directory Service가 생성이 됐다는 것은 SecureString에서 값을 불러와서 관리자 암호로 설정했다는 것을 알 수 있습니다.

Parameters:
WindowsLatestAmi:
Description: Latest Windows 2022 AMI
Type : AWS::SSM::Parameter::Value
Default: /aws/service/ami-windows-latest/Windows_Server-2022-Japanese-Full-Base
AllowedValues:
- /aws/service/ami-windows-latest/Windows_Server-2022-Japanese-Full-Base

그 외에서 다음과 같은 코드로 AWS에서 설정한 퍼블릭 AMI 파라미터를 가져올 수 있습니다.

그 외 퍼블릭 파라미터에 관해서는 아래 링크를 참고해 주세요.

본 블로그 게시글을 보시고 문의 사항이 있으신 분들은 클래스메소드코리아 (info@classmethod.kr)로 연락 주시면 빠른 시일 내 담당자가 회신 드릴 수 있도록 하겠습니다 !

참고