[AWS CloudFormation] 22. CloudFormation

2020. 4. 20. 09:16AWS

반응형

ㅇ CloudFormation?

    > AWS 리소스 설정을 JSON 또는 YAML 형식으로 기재한 템플릿을 만들고, 템플릿을 통해 AWS 리소스를 자동으로 구축하는 서비스

 

ㅇ CloudFormation 지원 AWS 서비스

  • Auto Scaling

  • RDS

  • CloudFront

  • Redshift

  • CloudWatch

  • Route53

  • DynamoDB

  • S3

  • EC2

  • SimpleDB

  • ElastiCache

  • SNS

  • Elastic Beanstalk

  • SQS

  • Elastic Load Balancing

  • VPC

  • IAM

 

ㅇ Stack?

    > CloudFormation은 JSON 또는 ymal 형식으로 작성된 템플릿 파일을 바탕으로 VPC, EC2 등의 리소스를 생성하고 구축하는데, 이렇게 생성된 것을 Stack이라고 함

 

ㅇ CloudFormation 사용

    > [CloudFormation 서비스] - [스택] - [스택 생성] 선택

 

ㅇ Stack을 생성하는 두 가지 방법

    1. S3 URL로 생성

    > [CloudFormation 서비스] - [Designer] 선택

 

    > 원하는 리소스를 드래그 & 드랍으로 제작한 이후, 좌측 상단의 스택 생성 선택

    > 해당 템플릿이 저장된 S3 URL이 자동으로 기입됨

 

    2. 직접 코드 제작

    > [준비된 템플릿] - [템플릿 파일 업로드] - [파일 선택] 을 통해 자신이 제작한 템플릿 파일 업로드

        * 만약 파일에 문제가 있을 경우 다음을 눌러도 에러 표시와 함께 다음으로 넘어가지 않음

 

ㅇ Stack 등록 이후

    > Stack 이름 입력

 

    > 태그와 권한을 설정할 수 있지만 간단한 테스트만 수행할 것이므로 패스

 

    > Stack과 관련된 여러 옵션이 존재하지만 지금은 패스

        * 스택 정책: Stack 업데이트 중 의도치 않게 업데이트되지 않도록 하려는 리소스 정의

        * 롤백 구성: CloudFormation이 Stack 생성 및 업데이트 시 경보 지정

        * 알림 옵션: SNS 주제 설정

        * 스택 생성 옵션: Stack 생성 실패 시 롤백해야 할지에 대한 여부 지정

 

    > 마지막으로 Stack이름과 CREATE_COMPLETE 상태가 출력되면 구성이 모두 완료되었음을 의미

 


ㅇ 기본틀

{
    "Description" : "Test",
    "Resources" :
    {
        ... (Code) ...
    }
}

 

ㅇ VPC 생성

"VPC" :
{
    "Type" : "AWS::EC2::VPC",
    "Properties" :
    {
        "CidrBlock": "10.0.0.0/16",
        "EnableDnsHostnames": true,
        "Tags":    [{ "Key": "Name", "Value": "LAB VPC" }]
    }    
}

 

EC2 생성

"PublicSubnet1":
{
    "Type": "AWS::EC2::Subnet",
    "Properties":
    {
        "VpcId": {"Ref": "VPC"},
        "CidrBlock": "10.0.0.0/24",
        "AvailabilityZone":    {"Fn::Select": ["0",{"Fn::GetAZs": ""}]},
        "Tags":    [{ "Key": "Name", "Value": "Public Subnet 1" }]
    }
}

 

ㅇ 인터넷 게이트웨이 생성 & VPC 접합

"InternetGateway":
{
    "Type": "AWS::EC2::InternetGateway",
    "Properties":
    {
        "Tags":    [{ "Key": "Name", "Value": "Lab Internet Gateway" }]
    }
},
        
"AttachGateway":
{
    "Type": "AWS::EC2::VPCGatewayAttachment",
    "Properties":
    {
        "VpcId": {"Ref": "VPC"},
        "InternetGatewayId": {"Ref": "InternetGateway"}
    }
}

 

ㅇ 라우팅 테이블 생성 & 인터넷 게이트웨이 추가

"PublicRouteTable":
{
    "Type": "AWS::EC2::RouteTable",
    "Properties":
    {
        "VpcId": {"Ref": "VPC"},
        "Tags":    [{ "Key": "Name", "Value": "Public Route Table" }]
    }
},

"PublicRoute":
{
    "Type": "AWS::EC2::Route",
    "Properties":
    {
        "RouteTableId": {"Ref": "PublicRouteTable"},
        "DestinationCidrBlock": "0.0.0.0/0",
        "GatewayId": {"Ref": "InternetGateway"}
    }
}

 

ㅇ 예제

{
    "Description" : "Test",
    "Resources" :
    {
        "VPC" :
        {
            "Type" : "AWS::EC2::VPC",
            "Properties" :
            {
                "CidrBlock": "10.0.0.0/16",
                "EnableDnsHostnames": true,
                "Tags":[{ "Key": "Name", "Value": "VPC" }]
            }    
        },
        
        "InternetGateway":
        {
            "Type": "AWS::EC2::InternetGateway",
            "Properties":
            {
                "Tags":    [{ "Key": "Name", "Value": "igw" }]
            }
        },
        
        "AttachGateway":
        {
            "Type": "AWS::EC2::VPCGatewayAttachment",
            "Properties":
            {
                "VpcId": {"Ref": "VPC"},
                "InternetGatewayId": {"Ref": "InternetGateway"}
            }
        },
    
        "PublicSubnet":
        {
            "Type": "AWS::EC2::Subnet",
            "Properties":
            {
                "VpcId": {"Ref": "VPC"},
                "CidrBlock": "10.0.0.0/24",
                "AvailabilityZone":    {"Fn::Select": ["0",{"Fn::GetAZs": ""}]},
                "Tags":    [{ "Key": "Name", "Value": "public-1" }]
            }
        },
        
        "PublicRouteTable":
        {
            "Type": "AWS::EC2::RouteTable",
            "Properties":
            {
                "VpcId": {"Ref": "VPC"},
                "Tags":    [{ "Key": "Name", "Value": "public-rt" }]
            }
        },
        
        "PublicSubnetRouteTableAssociation1":
        {
            "Type": "AWS::EC2::SubnetRouteTableAssociation",
            "Properties":
            {
                "SubnetId": {"Ref": "PublicSubnet"},
                "RouteTableId": {"Ref": "PublicRouteTable"}
            }
        },
        
        "PublicRoute":
        {
            "Type": "AWS::EC2::Route",
            "Properties":
            {
                "RouteTableId": {"Ref": "PublicRouteTable"},
                "DestinationCidrBlock": "0.0.0.0/0",
                "GatewayId": {"Ref": "InternetGateway"}
            }
        },
        
        "PrivateSubnet":
        {
            "Type": "AWS::EC2::Subnet",
            "Properties":
            {
                "VpcId": {"Ref": "VPC"},
                "CidrBlock": "10.0.1.0/24",
                "AvailabilityZone":    {"Fn::Select": ["0",{"Fn::GetAZs": ""}]},
                "Tags":    [{ "Key": "Name", "Value": "private-1" }]
            }
        },
        
        "PrivateRouteTable":
        {
            "Type": "AWS::EC2::RouteTable",
            "Properties":
            {
                "VpcId": {"Ref": "VPC"},
                "Tags":    [{ "Key": "Name", "Value": "private-rt" }]
            }
        },
        
        "PrivateSubnetRouteTableAssociation1":
        {
            "Type": "AWS::EC2::SubnetRouteTableAssociation",
            "Properties":
            {
                "SubnetId": {"Ref": "PrivateSubnet"},
                "RouteTableId": {"Ref": "PrivateRouteTable"}
            }
        }
    }
}

> 위의 코드를 통해 VPC와 Public Subnet, Private Subnet 그리고 각각의 라우팅 테이블이 생성됨

 

 


Q1. VPC 없이 서브넷 생성이 가능할까?

    > VPC가 없다는 문구와 함께 에러 발생

 

Q2. CloudFormation Stack을 지워도 기존 리소스를 유지할까?

    Stack을 지우면 해당 Stack과 관련된 리소스는 모두 삭제됨

 

Q3. CloudFormation으로 구성한 리소스 중 하나를 지우면 Stack도 사라질까?

    > Stack을 지우면 리소스들이 사라지는 것과는 달리, 리소스만 지워도 Stack은 해당 리소스가 없는 상태를 유지함

반응형

'AWS' 카테고리의 다른 글

[AWS Route53] 24. Route53  (0) 2020.04.29
[AWS VPC] 23. 라우팅 테이블 - Edge Associations  (0) 2020.04.24
[AWS Lambda] 21. Lambda  (0) 2020.04.14
[AWS AppStream] 20-2. AppStream 2.0  (0) 2020.04.13
[AWS AppStream] 20-1. AppStream 2.0  (0) 2020.04.10