Stack JSON

Hoju·2022년 8월 27일

Troble Shting

목록 보기
7/13
  • "**AWSTemplateFormatVersion"** 포맷 버전을 말합니다. 즉, 버전을 명시하게 되면 아래와 같습니다.포맷 버전을 말합니다. 즉, 버전을 명시하게 되면 아래와 같습니다. 선택 사항 입니다
"AWSTemplateFormatVersion" : "version date"

#example
"AWSTemplateFormatVersion" : "2010-09-09"
  • "Description" 템플릿에 대한 설명을 하는 옵션 입니다. 선택 사항 입니다.
"Description" : "JSON string"

#example
"Description" : "Here are some details about the template."
  • "Metadata" 템플릿에 대한 추가 정보를 제공하는 객체 입니다. 즉, Cloudformation에서는 metadata Key아래 명시해서, metadata Key에 저장할 수 있습니다.선택 사항 입니다.
"Metadata" : {
    template metadata
  },

#example
"Metadata" : {
  "Instances" : {"Description" : "Information about the instances"},
  "Databases" : {"Description" : "Information about the databases"}
}
  • "Parameters" (스택을 생성하거나 업데이트할 때) 실행 시간에 템플릿에 전달하는 값입니다. 템플릿의 Resources및 Outputs섹션에서 파라미터를 참조할 수 있습니다. 선택 사항 입니다.
"Parameters" : {
    set of parameters
  },

#example
"Parameters" : {
  "InstanceTypeParameter" : {
    "Type" : "String",
    "Default" : "t2.micro",
    "AllowedValues" : ["t2.micro", "m1.small", "m1.large"],
    "Description" : "Enter t2.micro, m1.small, or m1.large. Default is t2.micro."
  }
}

#example(2)
"Ec2Instance" : {
  "Type" : "AWS::EC2::Instance",
  "Properties" : {
    "InstanceType" : { "Ref" : "InstanceTypeParameter" },
    "ImageId" : "ami-0ff8a91507f77f867"
  }
}
💡 Parameters는 좀 더 설명 드리겠습니다. 위에 Example(1)을 설명 드리겠습니다. InstanceType에 대한 Parameter를 구성했습니다. 즉, InstanceType에 기본으로 할당할 Type은 t2.micro를 설정해주겠다는 의미입니다. AllowedValues 부분에서는 Instance에 할당 가능한 Instance Type을 정의 했습니다.
  • "Rules" 선택적 Rules섹션에서 스택을 생성하거나 업데이트할 때 템플릿에 전달된 파라미터 또는 파라미터의 조합을 검증합니다. 템플릿 규칙을 사용하려면 어설션 다음에 템플릿에서 Rules를 명시적으로 선언합니다. 선택 사항 입니다 Rule같은 경우는 Functions이 내장되어있습니다. 다음에 이 주제를 가지고 설명드리겠습니다.
"Rules" : {
    set of rules
  },

#example
{
    "Rules": {
        "Rule01": {
            "RuleCondition": {
                "rule-specific intrinsic function": "Value01"
            },
            "Assertions": [
                {
                    "Assert": {
                        "rule-specific intrinsic function": "Value02"
                    },
                    "AssertDescription": "Information about this assert"
                },
                {
                    "Assert": {
                        "rule-specific intrinsic function": "Value03"
                    },
                    "AssertDescription": "Information about this assert"
                }
            ]
        },
        "Rule02": {
            "Assertions": [
                {
                    "Assert": {
                        "rule-specific intrinsic function": "Value04"
                    },
                    "AssertDescription": "Information about this assert"
                }
            ]
        }
    }
}
  • "Mappings" 조건부 파라미터 값을 지정하는 데 사용할 수 있는 키와 관련 값의 매핑으로, 조회 테이블과 비슷합니다. Resources 및 Outputs섹션의 Fn::FindInMap 내장 함수를 사용하여 키를 해당 값으로 매핑할 수 있습니다. 선택사항 입니다
"Mappings" : {
    set of mappings
  },

#example
"Mappings" : {
  "Mapping01" : {
    "Key01" : {
      "Name" : "Value01"
    },
    "Key02" : {
      "Name" : "Value02"
    },
    "Key03" : {
      "Name" : "Value03"
    }
  }
}

#example(자세하게)
{
  "AWSTemplateFormatVersion" : "2010-09-09",

  "Mappings" : {
    "RegionMap" : {
      "us-east-1"        : {"HVM64" : "ami-0ff8a91507f77f867", "HVMG2" : "ami-0a584ac55a7631c0c"},
      "us-west-1"        : {"HVM64" : "ami-0bdb828fd58c52235", "HVMG2" : "ami-066ee5fd4a9ef77f1"},
      "eu-west-1"        : {"HVM64" : "ami-047bb4163c506cd98", "HVMG2" : "ami-0a7c483d527806435"},
      "ap-northeast-1"   : {"HVM64" : "ami-06cd52961ce9f0d85", "HVMG2" : "ami-053cdd503598e4a9d"},
      "ap-southeast-1"   : {"HVM64" : "ami-08569b978cc4dfa10", "HVMG2" : "ami-0be9df32ae9f92309"}
    }
  },

  "Resources" : {
    "myEC2Instance" : {
      "Type" : "AWS::EC2::Instance",
      "Properties" : {
        "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "HVM64"]},
        "InstanceType" : "m1.small"
      }
    }
  }
}
💡 설명을 드리자면, Mapping 조건을 이제 AMI를 사용하는 것입니다. Mappings에 정의한 RegionMap 이름을 파라미터로 설정하고, 아래 Resource 부분에서 Instance는 RegionMap에 설정한 것 중 HVM64 이미지를 사용한다는 의미입니다.
  • "Conditions" 스택 생성 또는 업데이트 시 특정 리소스 속성에 값이 할당되는지 또는 특정 리소스가 생성되는지 여부를 제어하는 조건입니다. 예를 들면 스택이 프로덕션용인지 테스트 환경용인지에 따라 달라지는 리소스를 조건부로 생성할 수 있습니다. 선택 사항 입니다
"Conditions" : {
    set of conditions
  },

#example
{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Parameters": {
        "EnvType": {
            "Description": "Environment type.",
            "Default": "test",
            "Type": "String",
            "AllowedValues": [
                "prod",
                "test"
            ],
            "ConstraintDescription": "must specify prod or test."
        }
    },
    "Conditions": {
        "CreateProdResources": {
            "Fn::Equals": [
                {
                    "Ref": "EnvType"
                },
                "prod"
            ]
        }
    },
    "Resources": {
        "EC2Instance": {
            "Type": "AWS::EC2::Instance",
            "Properties": {
                "ImageId": "ami-0ff8a91507f77f867"
            }
        },
        "MountPoint": {
            "Type": "AWS::EC2::VolumeAttachment",
            "Condition": "CreateProdResources",
            "Properties": {
                "InstanceId": {
                    "Ref": "EC2Instance"
                },
                "VolumeId": {
                    "Ref": "NewVolume"
                },
                "Device": "/dev/sdh"
            }
        },
        "NewVolume": {
            "Type": "AWS::EC2::Volume",
            "Condition": "CreateProdResources",
            "Properties": {
                "Size": 100,
                "AvailabilityZone": {
                    "Fn::GetAtt": [
                        "EC2Instance",
                        "AvailabilityZone"
                    ]
                }
            }
        }
    }
}
  • "Transform" 서버리스 애플리케이션(Lambda 기반 애플리케이션)의 경우, 사용할 SAM의 버전도 지정합니다. 변환을 지정할 경우 구문을 사용하여 템플릿에 리소스를 선언할 수 있습니다. 선택 사항 입니다 (예시가 정확하지 않아 확인할 수 없었습니다.)
  • "Resources"(중요) Amazon Elastic Compute Cloud 인스턴스 또는 Amazon Simple Storage Service 버킷 같은 스택 리소스 및 해당 속성을 지정합니다. 템플릿의 Resources및 Outputs섹션에서 리소스를 참조할 수 있습니다. 필수 사항입니다.
"Resources" : {
    set of resources
  },

#example
"Resources": {
    "EC2Instance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "Tags": [{
            "Key": "StudentID",
            "Value": "something"
          },
          {
            "Key": "StudentName",
            "Value": "someone"
          }
        ],
        "InstanceType": "t2.micro",
        "SecurityGroups": [{
          "Ref": "WebServerSG"
        }],
        "KeyName": "MyKeyPair",
        "ImageId": "ami-0323c3dd2da7fb37d",
        "UserData": {
          "Fn::Base64": {
            "Fn::Join": ["", [
              "#!/bin/bash -ex\n",
              "yum update -y\n",
              "yum install -y httpd php mysql-server php-mysqlnd\n",
              "systemctl enable httpd\n",
              "systemctl start httpd\n",
              "usermod -a -G apache ec2-user\n",
              "chown -R ec2-user:apache /var/www\n",
              "chmod 2775 /var/www\n",
              "find /var/www -type d -exec sudo chmod 2775 {} \\;\n",
              "find /var/www -type f -exec sudo chmod 0664 {} \\;\n",
              "echo \"<?php echo '<h2>Welcome to COS80001. Installed PHP version: ' . phpversion() . '</h2>'; ?>\" > /var/www/html/phpinfo.php\n"
            ]]
          }
        }

      }
    }
  • "Outputs" 템플릿으로 생성한 것의 결과를 출력합니다.
"Outputs" : {
    set of outputs
  }

#example
"Outputs" : {
  "BackupLoadBalancerDNSName" : {
    "Description": "The DNSName of the backup load balancer",  
    "Value" : { "Fn::GetAtt" : [ "BackupLoadBalancer", "DNSName" ]},
    "Condition" : "CreateProdResources"
  },
  "InstanceID" : {
    "Description": "The Instance ID",  
    "Value" : { "Ref" : "EC2Instance" }
  }
}
profile
Devops가 되고 싶은 청소년

0개의 댓글