AWS CloudFormation

박재현·2023년 6월 17일
0

🔎 IaC(Infrastructure As Code)

  • YAML, JSON Teplmate 기반으로 AWS 자원 생성

🔎 장점

  • 일관성
  • 속도 & 간편성(코드 기반)
  • Version Control
  • 사용 비용 무료
  • 쉬운 업데이트
  • 용이한 이전 버전 롤백

=> AWS 자원(람다, DB 등)을 생성하기 위해 일일이 스펙을 설정하고 선택(Click)할 필요 없이 Template만 명시하면 자동으로 생성
=> 팀 단위로 AWS 자원을 관리할 때 용이

🔎 작동원리

🔎 실습

  • CloudFormation으로 ec2 인스턴스 생성 및 ssh 접근
AWSTemplateFormatVersion: 2010-09-09

Description: Template to create an EC2 instance and enable SSH

# Resouces 생성에 필요한 Prameter 명시(CloudFormation 생성 시 설정해줌)
Parameters:
  KeyName:
    Description: Name of SSH KeyPair
    Type: 'AWS::EC2::KeyPair::KeyName'
    ConstraintDescription: Provide the name of an existing SSH key pair

# 만들어줄 AWS Resources
Resources:
  MyEC2Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      InstanceType: t2.micro
      ImageId: ami-0a0064415cdedc552 # Resource ID
      KeyName: !Ref KeyName  # 참조할 때, !Ref 이름
      SecurityGroups:
       - Ref: InstanceSecurityGroup
      Tags:
        - Key: Name
          Value: My CF Instance
  InstanceSecurityGroup:
    Type: 'AWS::EC2::SecurityGroup'
    Properties:
      GroupDescription: Enable SSH access via port 22
      SecurityGroupIngress:
        IpProtocol: tcp
        FromPort: 22
        ToPort: 22
        CidrIp: 0.0.0.0/0

# 결과물
Outputs: 
  InstanceID:
    Description: The Instance ID
    Value: !Ref MyEC2Instance

ResourceId

  1. 템플릿 지정

  2. 스택 이름 지정 및 파라미터로 설정했던 키페어 설정

  3. 생성 결과

  1. EC2 ssh 를 통해 접근


🔎 SAM(Serverless Application Model)

  • Cloudformation 기반으로 동작하고, SAM CLI로 간단한 Syntac 제공
  • CloudFormation + Serverless
  • Nested Stack: 중첩스택 사용 가능
    • 공통적으로 쓰이는 Security Group, Load Balancer와 같은 것들을 먼저 만들고 다른 자원에서 사용

0개의 댓글

관련 채용 정보