kaonmir
시리즈
SAA
DOP
System Design Interview
Linux
ETC
Sign In
Home
Kaonmir (손성훈)
Copy & Translate
시리즈
SAA
DOP
System Design Interview
Linux
ETC
AI를 더 잘 쓰기 위한 IT 용어
구독

CloudFormation - DOP Level

SSM에서 파라미터 값 가져오기

Parameters:
	InstanceType:
		Type: 'AWS::SSM::Parameter::Value<String>'
		Default: /EC2/InstanceType
•
비밀번호처럼 값을 숨겨야 하는 경우
•
여러 곳에 쓰이는 값을 중앙에서 관리하고 싶을 경우
SSM Parameter Store을 통해 관리할 수 있다.
AWS에서 미리 정의해 놓은 public parameter도 있다.

DependsOn

DependsOn 옵션이 걸려있는 리소스는 타겟 리소스가 만들어져야지 생성을 시작할 수 있다. 리소스가 삭제될 때는 반대로 진행된다.

Lambda 함수 배포

Deploying AWS Lambda functions using AWS CloudFormation (the portable way) | Amazon Web Services
AWS Lambda requires its source code to be hosted in an Amazon S3 bucket in the same AWS Region as the Lambda function, which poses challenges when writing AWS CloudFormation templates that are portable across different regions. This post discusses three patterns to address that at stack.
aws.amazon.com
•
Inline function : Lambda 코드를 CFN 안에 직접 넣는다. Lambda를 업데이트하려면 CFN을 수정해야 한다.
•
Regional bucket : Lambda 코드를 S3에 넣고 참조한다.

Custom Resource

Custom Resource를 정의하면 CFN은 대응하는 Lambda 함수를 호출한다. Lambda를 사용해서 CFN이 할 수 없는 추가적인 일들을 할 수 있다. 아직 CFN이 지원하지 않는 리소스를 생성하거나, 온프레미스에 리소스를 생성할 수도 있다.
Parameters:
	cleanupbucket_arn:
		Type: 'AWS::SSM::Parameter::Value<String>'
		Default: /CloudFormation/Lambda/cleanupbucket_arn
Resources:
	myBucketResource:
		Type: AWS::S3::Bucket
	LambdaUsedToCleanUp:
		Type: Custom::cleanupbucket
		Properties:
			ServiceToken: !Ref cleanupbucket_arn # Lambda 함수의 arn
			BucketName: !Ref myBucketResource

Drift Detection

CloudFormation으로 만든 리소스를 콘솔에서 임의로 변경하면 어떻게 알 수 있을까? drift detection을 실행하면 CFN이 의도하지 않은 구성 변경이 발생했는지 확인할 수 있다.

Stack Status Code

Describe and list your stacks with the AWS CLI - AWS CloudFormation
Learn how to describe and list your CloudFormation stacks with the AWS CLI.
docs.aws.amazon.com
생소한 거 몇 개만 적는다.
•
REVIEW_IN_PROGRESS : stack id는 있지만 실제 만들어지는 리소스는 없음.
•
UPDATE_ROLLBACK_FAILED : 스택 업데이트에 실패해서 롤백하려 했지만, 롤백 마저 실패해 답이 없는 상태. 스택을 삭제하거나, 오류를 수정한 후 롤백을 다시 진행하거나, 아니면 AWS Support에 연락해 status code를 복구해달라고 해야 한다.

InsufficientCapabilitiesException

CFN을 이용해 리소스를 만드려면 CFN에 적절한 IAM 권한을 부여해야 한다. 위 사진은 자동으로 부여한다는 문구이고 만약 체크하지 않으면, InsufficientCapabilitiesException 에러가 나온다.

cfn-hub & cfn-metadata

CFN의 리소스 설정을 변경하면, 대부분은 리소스를 새로 만든다. 하지만 EC2의 AWS::CloudFormation::Init를 변경하면 리소스는 새로 만들어지지 않는다. 그러면 어떻게 변경 사항을 EC2 인스턴스에 적용할까?
이 변경을 감지하는 것이 cfn-hub 데몬이다. cfn-hub 데몬은 파일에 설정해 놓은 값에 따라 n분에 한 번씩 CFN init의 변경을 확인하고 인스턴스를 변경한다. CFN Init 설정을 확인하고 싶으면 cfn-metadata 명령을 실행하면 된다.
•
/etc/cfn/cfn-hub.conf : 몇 분에 한 번씩 어느 CFN 스택을 감시할 것인지 설정
•
/etc/cfn/hoooks.d/cfn-auto-reloader.conf : CFN 스택이 변경되면 어떤 명령을 실행할 것인지 설정

Stack Polices

IAM 정책과 비슷하게 stack 정책을 만들어서 원하는대로 리소스의 생성/변경/삭제를 제어할 수 있다. CFN YAML 파일과는 별도로 JSON 파일을 만들어야 한다.
Made with Slashpage