

# CloudFormation 템플릿 Resources 구문
<a name="resources-section-structure"></a>

`Resources` 섹션은 CloudFormation 템플릿의 필수 최상위 섹션입니다. CloudFormation에서 스택의 일부로 프로비저닝 및 구성하려는 AWS 리소스를 선언합니다.

## 구문
<a name="resources-section-structure-syntax"></a>

`Resources` 섹션에서는 다음 구문을 사용합니다.

### JSON
<a name="resources-section-structure-syntax.json"></a>

```
"Resources" : {
    "LogicalResourceName1" : {
        "Type" : "AWS::ServiceName::ResourceType",
        "Properties" : {
            "PropertyName1" : "PropertyValue1",
            ...
        }
    },

    "LogicalResourceName2" : {
        "Type" : "AWS::ServiceName::ResourceType",
        "Properties" : {
            "PropertyName1" : "PropertyValue1",
            ...
        }
    }
}
```

### YAML
<a name="resources-section-structure-syntax.yaml"></a>

```
Resources:
  LogicalResourceName1:
    Type: AWS::ServiceName::ResourceType
    Properties:
      PropertyName1: PropertyValue1
      ...

  LogicalResourceName2:
    Type: AWS::ServiceName::ResourceType
    Properties:
      PropertyName1: PropertyValue1
      ...
```

## 논리적 ID(**논리명이라고도 함)
<a name="resources-section-logical-id"></a>

CloudFormation 템플릿 내에서 리소스는 논리적 이름으로 식별됩니다. 이 이름은 영숫자(A-Z, a-z, 0-9)여야 하며 템플릿 내에서 고유해야 합니다. 논리명은 템플릿의 다른 섹션에 있는 리소스를 참조하는 데 사용됩니다.

## 리소스 유형
<a name="resources-section-resource-type"></a>

각 리소스에는 `Type` 속성이 있어야 하며, 이 속성은 AWS 리소스의 종류를 정의합니다. `Type` 속성은 `AWS::ServiceName::ResourceType` 형식을 갖습니다. 예를 들어, Amazon S3 버킷의 `Type` 속성은 `AWS::S3::Bucket`입니다.

지원되는 리소스 유형의 전체 목록은 [AWS 리소스 및 속성 유형 참조](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-template-resource-type-ref.html)를 참조하세요.

## 리소스 속성
<a name="resources-section-resource-properties"></a>

리소스 속성은 특정 리소스 유형에 대한 구성 세부 정보를 정의하기 위해 지정할 수 있는 추가 옵션입니다. 일부 속성은 필수이고, 선택 사항인 속성도 있습니다. 일부 속성에는 기본값이 있으므로 해당 속성을 지정하는 것은 선택 사항입니다.

각 리소스 유형에 지원되는 속성에 대한 자세한 내용은 [AWS 리소스 및 속성 유형 참조](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-template-resource-type-ref.html)의 주제를 참조하세요.

속성값은 리터럴 문자열, 문자열 목록, 부울, 동적 참조, 파라미터 참조, 가상 참조 또는 함수가 반환하는 값 중 하나일 수 있습니다. 다음 예제에서는 다양한 속성 값 유형을 선언하는 방법을 보여줍니다.

### JSON
<a name="resource-properties-example.json"></a>

```
"Properties" : {
    "String" : "A string value",
    "Number" : 123,
    "LiteralList" : [ "first-value", "second-value" ],
    "Boolean" : true
}
```

### YAML
<a name="resource-properties-example.yaml"></a>

```
Properties:
  String: A string value 
  Number: 123
  LiteralList:
    - first-value
    - second-value
  Boolean: true
```

## 물리적 ID
<a name="resources-section-physical-id"></a>

논리적 ID 외에도, 특정 리소스에는 EC2 인스턴스 ID 또는 S3 버킷 이름 같은 해당 리소스에 대해 실제 할당된 이름인 물리적 ID도 지정됩니다. 물리적 ID를 사용하여 CloudFormation 템플릿 외부에 있는 리소스를 식별할 수 있지만 이는 리소스가 생성된 후에만 가능합니다. 예를 들어 EC2 인스턴스에 논리적 ID `MyEC2Instance`의 리소스를 제공한다고 가정하겠습니다. CloudFormation에서 인스턴스를 생성할 때 CloudFormation은 자동적으로 물리적 ID(예: `i-1234567890abcdef0`)를 생성하여 인스턴스에 할당합니다. 이 물리적 ID를 사용하면 Amazon EC2 콘솔에서 인스턴스를 식별하고 인스턴스의 속성(예: DNS 이름)을 볼 수 있습니다.

Amazon S3 버킷과 기타 여러 리소스의 경우 사용자가 명시적으로 지정하지 않으면 CloudFormation이 리소스에 대한 고유한 물리명을 자동으로 생성합니다. 이 물리명은 CloudFormation 스택의 이름, CloudFormation 템플릿에 지정된 리소스의 논리명, 고유 ID의 조합을 기반으로 합니다. 예를 들어, `MyStack`이라는 스택에 논리명이 `MyBucket`인 Amazon S3 버킷이 있는 경우 CloudFormation은 `MyStack-MyBucket-abcdefghijk1`이라는 물리적 이름으로 버킷의 이름을 지정할 수 있습니다.

사용자 지정 이름을 지원하는 리소스의 경우, 물리적 이름을 할당하면 리소스를 보다 신속하게 식별할 수 있습니다. 예를 들면 로그를 저장하는 S3 버킷에 `MyPerformanceLogs` 이름을 지정할 수 있습니다. 자세한 내용은 [이름 유형](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-properties-name.html)을 참조하세요.

## 참조 리소스
<a name="using-cross-resource-references"></a>

다른 리소스의 이름이나 속성을 기반으로 한 리소스의 속성을 설정해야 하는 경우가 있습니다. 예를 들어, S3 버킷이 지원하는 CloudFront 배포 또는 EC2 보안 그룹을 사용하는 EC2 인스턴스를 생성할 수 있습니다. 이러한 모든 리소스는 동일한 CloudFormation 템플릿에서 생성할 수 있습니다.

CloudFormation은 다른 리소스 및 해당 속성을 참조하는 데 사용할 수 있는 내장 함수를 제공합니다. 이러한 함수를 사용하면 리소스 간에 종속성을 생성하고 한 리소스에서 다른 리소스로 값을 전달할 수 있습니다.

### `Ref` 함수
<a name="resource-properties-ref"></a>

`Ref` 함수는 대체로 동일한 CloudFormation 템플릿 내에 정의된 리소스의 식별 속성을 검색하는 데 사용됩니다. 반환되는 내용은 리소스 유형에 따라 달라집니다. 대부분의 리소스는 리소스의 물리명을 반환합니다. 하지만 일부 리소스 유형의 경우 `AWS::EC2::EIP` 리소스의 IP 주소나 Amazon SNS 주제의 Amazon 리소스 이름(ARN) 등의 다른 값을 반환할 수 있습니다.

다음 예제는 속성에서 `Ref` 함수의 사용 방법을 설명합니다. 각 예제에서 `Ref` 함수는 템플릿의 다른 곳에서 선언된 `LogicalResourceName` 리소스의 실제 이름을 반환합니다. YAML 예제의 `!Ref` 구문 예제는 `Ref` 함수를 보다 간단하게 작성하는 방법입니다.

#### JSON
<a name="resource-properties-ref-example.json"></a>

```
"Properties" : {
    "PropertyName" : { "Ref" : "LogicalResourceName" }
}
```

#### YAML
<a name="resource-properties-ref-example.yaml"></a>

```
Properties:
  PropertyName1:
    Ref: LogicalResourceName
  PropertyName2: !Ref LogicalResourceName
```

`Ref` 함수에 대한 더 자세한 내용은 [https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/intrinsic-function-reference-ref.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/intrinsic-function-reference-ref.html)를 참조하세요.

### `Fn::GetAtt` 함수
<a name="resource-properties-getatt"></a>

`Ref` 함수는 리소스에 대해 반환되는 파라미터나 값이 정확히 원하는 값인 경우에 유용합니다. 그러나 리소스의 다른 속성이 필요할 수 있습니다. 예를 들어 S3 오리진을 사용하여 CloudFront 배포를 생성하려는 경우 DNS 스타일 주소를 사용하여 버킷 위치를 지정해야 합니다. 일부 리소스에는 템플릿에서 사용할 수 있는 값이 지정되는 추가 속성이 있습니다. 이러한 속성을 가져오려면 `Fn::GetAtt` 함수를 사용합니다.

다음 예제는 속성에서 `GetAtt` 함수의 사용 방법을 설명합니다. `Fn::GetAtt` 함수는 두 개 파라미터, 리소스의 논리적 이름, 그리고 검색할 속성의 이름을 사용합니다. YAML 예제의 `!GetAtt` 구문 예제는 `GetAtt` 함수를 보다 간단하게 작성하는 방법입니다.

#### JSON
<a name="resource-properties-getatt-example.json"></a>

```
"Properties" : {
    "PropertyName" : {
        "Fn::GetAtt" : [ "LogicalResourceName", "AttributeName" ]
    }
}
```

#### YAML
<a name="resource-properties-getatt-example.yaml"></a>

```
Properties:
  PropertyName1:
    Fn::GetAtt:
      - LogicalResourceName
      - AttributeName
  PropertyName2: !GetAtt LogicalResourceName.AttributeName
```

`GetAtt` 함수에 대한 더 자세한 내용은 [https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/intrinsic-function-reference-getatt.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/intrinsic-function-reference-getatt.html)를 참조하세요.

## 예제
<a name="resources-section-structure-examples"></a>

다음 예제는 리소스를 선언하는 방법과 CloudFormation 템플릿이 동일한 템플릿 내에 정의된 다른 리소스와 기존 AWS 리소스를 참조하는 방법을 보여줍니다.

**Topics**
+ [사용자 지정 이름을 사용하여 단일 리소스 선언](#resources-section-structure-examples-single-resource)
+ [`Ref` 함수로 다른 리소스 참조](#resources-section-structure-examples-ref)
+ [`Fn::GetAtt` 함수를 사용하여 리소스 속성 참조](#resources-section-structure-examples-getatt)

### 사용자 지정 이름을 사용하여 단일 리소스 선언
<a name="resources-section-structure-examples-single-resource"></a>

다음 예제에서는 논리명이 `MyBucket`인 `AWS::S3::Bucket` 유형의 단일 리소스를 선언합니다. `BucketName` 속성이 *amzn-s3-demo-bucket*으로 설정되는데, 이를 원하는 S3 버킷 이름으로 바꿔야 합니다.

이 리소스 선언을 사용하여 스택을 생성할 경우 CloudFormation에서는 기본 설정으로 Amazon S3 버킷을 생성합니다. Amazon EC2 인스턴스나 Auto Scaling 그룹 같은 기타 리소스의 경우 CloudFormation은 추가 정보를 요구합니다.

#### JSON
<a name="resources-section-structure-examples-single-resource.json"></a>

```
{
    "Resources": {
        "MyBucket": {
            "Type": "AWS::S3::Bucket",
            "Properties": {
                "BucketName": "amzn-s3-demo-bucket"
            }
        }
    }
}
```

#### YAML
<a name="resources-section-structure-examples-single-resource.yaml"></a>

```
Resources:
  MyBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: amzn-s3-demo-bucket
```

### `Ref` 함수로 다른 리소스 참조
<a name="resources-section-structure-examples-ref"></a>

다음 예제는 EC2 인스턴스와 보안 그룹을 정의하는 리소스 선언을 보여줍니다. `Ec2Instance` 리소스는 `Ref` 함수를 사용하여 해당 `SecurityGroupIds` 속성의 일부로 `InstanceSecurityGroup` 리소스를 참조합니다. 템플릿에 선언되지 않은 기존 보안 그룹(`sg-12a4c434`)도 포함됩니다. 리터럴 문자열을 사용하여 기존 AWS 리소스를 참조합니다.

#### JSON
<a name="resources-section-structure-examples-ref.json"></a>

```
{
    "Resources": {
        "Ec2Instance": {
            "Type": "AWS::EC2::Instance",
            "Properties": {
                "SecurityGroupIds": [
                    {
                        "Ref": "InstanceSecurityGroup"
                    },
                    "sg-12a4c434"
                ],
                "KeyName": "MyKey",
                "ImageId": "ami-1234567890abcdef0"
            }
        },
        "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"
                    }
                ]
            }
        }
    }
}
```

#### YAML
<a name="resources-section-structure-examples-ref.yaml"></a>

```
Resources:
  Ec2Instance:
    Type: AWS::EC2::Instance
    Properties:
      SecurityGroupIds:
        - !Ref InstanceSecurityGroup
        - sg-12a4c434
      KeyName: MyKey
      ImageId: ami-1234567890abcdef0
  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
```

### `Fn::GetAtt` 함수를 사용하여 리소스 속성 참조
<a name="resources-section-structure-examples-getatt"></a>

다음 예제에서는 CloudFront 배포 리소스와 S3 버킷을 정의하는 리소스 선언을 보여줍니다. `MyDistribution` 리소스는 버킷의 `DomainName` 속성을 가져오는 `Fn::GetAtt` 함수를 사용하여 `MyBucket` 리소스의 DNS 이름을 지정합니다. `Fn::GetAtt` 함수는 두 파라미터를 어레이 형태로 나열합니다. 여러 파라미터를 사용하는 함수의 경우 어레이를 사용하여 파라미터를 지정합니다.

#### JSON
<a name="resources-section-structure-examples-getatt.json"></a>

```
{
  "Resources": {
    "MyBucket": {
      "Type": "AWS::S3::Bucket"
    },
    "MyDistribution": {
      "Type": "AWS::CloudFront::Distribution",
      "Properties": {
        "DistributionConfig": {
          "Origins": [
            {
              "DomainName": {
                "Fn::GetAtt": [
                  "MyBucket",
                  "DomainName"
                ]
              },
              "Id": "MyS3Origin",
              "S3OriginConfig": {}
            }
          ],
          "Enabled": "true",
          "DefaultCacheBehavior": {
            "TargetOriginId": "MyS3Origin",
            "ForwardedValues": {
              "QueryString": "false"
            },
            "ViewerProtocolPolicy": "allow-all"
          }
        }
      }
    }
  }
}
```

#### YAML
<a name="resources-section-structure-examples-getatt.yaml"></a>

```
Resources:
  MyBucket:
    Type: AWS::S3::Bucket
  MyDistribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
        Origins:
          - DomainName: !GetAtt 
              - MyBucket
              - DomainName
            Id: MyS3Origin
            S3OriginConfig: {}
        Enabled: 'true'
        DefaultCacheBehavior:
          TargetOriginId: MyS3Origin
          ForwardedValues:
            QueryString: 'false'
          ViewerProtocolPolicy: allow-all
```