AWSTemplateFormatVersion
: 포맷 버전을 말합니다. 즉, 버전을 명시하게 되면 아래와 같습니다.포맷 버전을 말합니다. 즉, 버전을 명시하게 되면 아래와 같습니다. 선택 사항 입니다
AWSTemplateFormatVersion: "version date"
#example
AWSTemplateFormatVersion: "2010-09-09"
Description
: 템플릿에 대한 설명을 하는 옵션 입니다. 선택 사항 입니다.
Description:
String
#example
Description: >
This is worldskills
templeate!!
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(1)
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)
Parameters:
Ec2Instance:
Type: AWS::EC2::Instance
Properties:
InstanceType:
Ref: InstanceTypeParameter
ImageId: ami-0ff8a91507f77f867
💡 Parameters는 좀 더 설명 드리겠습니다. 위에 Example(1)을 설명 드리겠습니다. InstanceType에 대한 Parameter를 구성했습니다. 즉, InstanceType에 기본으로 할당할 Type은 t2.micro를 설정해주겠다는 의미입니다. 그러나 사용자가 원하는 다른 Instance Type을 원한다면 Cloudformation 생성 시 체크 해주면 됩니다. AllowedValues 부분에서는 Instance에 할당 가능한 Instance Type을 정의 했습니다.
- 선택 부분(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
Mappings
: 조건부 파라미터 값을 지정하는 데 사용할 수 있는 키와 관련 값의 매핑으로, 조회 테이블과 비슷합니다. Resources
및 Outputs
섹션의 Fn::FindInMap 내장 함수를 사용하여 키를 해당 값으로 매핑할 수 있습니다. 선택사항 입니다
Mappings:
Mapping01:
Key01:
Name: Value01
Key02:
Name: Value02
Key03:
Name: Value03
#그럼 어떻게 같이 사용할 수 있다는 거야? 이제 설명드리도록 하겠습니다
#example
AWSTemplateFormatVersion: "2010-09-09"
Mappings:
RegionMap:
ap-northeast-2:
HVM64: ami-0ff8a91507f77f867
HVMG2: ami-0a584ac55a7631c0c
Resources:
myEC2Instance:
Type: "AWS::EC2::Instance"
Properties:
ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", HVM64]
InstanceType: t2.small
💡 설명을 드리자면, Mapping 조건을 이제 AMI를 사용하는 것입니다. Mappings에 정의한 RegionMap 이름을 파라미터로 설정하고, 아래 Resource 부분에서 Instance는 RegionMap에 설정한 것 중 HVM64 이미지를 사용한다는 의미입니다.
Conditions
: 스택 생성 또는 업데이트 시 특정 리소스 속성에 값이 할당되는지 또는 특정 리소스가 생성되는지 여부를 제어하는 조건입니다. 예를 들면 스택이 프로덕션용
인지 테스트 환경용
인지에 따라 달라지는 리소스를 조건부
로 생성할 수 있습니다. 선택 사항 입니다
Conditions:
Logical ID:
Intrinsic function
#example
Parameters:
EnvType:
Description: Environment type.
Default: test
Type: String
AllowedValues:
- prod
- test
ConstraintDescription: must specify prod or test.
Conditions:
CreateProdResources: !Equals
- !Ref EnvType
- prod #이 부분에서 환경에 Type을 정합니다
Transform
서버리스 애플리케이션(Lambda 기반 애플리케이션)의 경우, 사용할 SAM의 버전도 지정합니다. 변환을 지정할 경우 구문을 사용하여 템플릿에 리소스를 선언할 수 있습니다. 선택 사항 입니다
Transform:
- MyMacro
- 'AWS::Serverless'
Resources:
WaitCondition:
Type: 'AWS::CloudFormation::WaitCondition'
MyBucket:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: MyBucket
Tags: [{"key":"value"}]
CorsConfiguration:[]
MyEc2Instance:
Type: 'AWS::EC2::Instance'
Properties:
ImageID: "ami-123"
#MyMacro 및 AWS::Serverless에 대한 처리 가능한 콘텐츠 종료
Resource(중요!!!)
: Amazon Elastic Compute Cloud 인스턴스 또는 Amazon Simple Storage Service 버킷 같은 스택 리소스 및 해당 속성을 지정합니다. 템플릿의 Resources
및 Outputs
섹션에서 리소스를 참조할 수 있습니다. 필수 사항입니다.
Resources:
Logical ID:
Type: Resource type
Properties:
Set of properties
#example
Resources:
MyEC2Instance:
Type: "AWS::EC2::Instance"
Properties:
ImageId: "ami-0ff8a91507f77f867"
#**Instance Resource에 User Data 입력하기!!**
BastionInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-0b7546e839d7ace12
InstanceType: t2.micro
NetworkInterfaces:
- AssociatePublicIpAddress: "true"
DeviceIndex: "0"
SubnetId:
Ref: "PublicSubnet1"
GroupSet:
- !Ref Bastion
UserData: !Base64 |
#!/bin/bash
echo 'password' | passwd --stdin ec2-user
sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
sed -i 's/#Port 22/Port 4020/g' /etc/ssh/sshd_config
yum install jq -y
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
systemctl restart sshd
Tags:
- Key: Name
Value: Bastion
Outputs
: 템플릿으로 생성한 것의 결과를 출력합니다.Outputs:
Logical ID:
Description: Information about the value
Value: Value to return
Export:
Name: Value to export
#example
Outputs:
BackupLoadBalancerDNSName:
Description: The DNSName of the backup load balancer
Value: !GetAtt BackupLoadBalancer.DNSName
Condition: CreateProdResources
InstanceID:
Description: The Instance ID
Value: !Ref EC2Instance
"Outputs": {
"ARN": {
"Value": {
"Fn::GetAtt": [
"ControlPlane",
"Arn"
]
},
"Export": {
"Name": {
"Fn::Sub": "${AWS::StackName}::ARN"
}
}
},
"CertificateAuthorityData": {
"Value": {
"Fn::GetAtt": [
"ControlPlane",
"CertificateAuthorityData"
]
}
},
"ClusterSecurityGroupId": {
"Value": {
"Fn::GetAtt": [
"ControlPlane",
"ClusterSecurityGroupId"
]
},
"Export": {
"Name": {
"Fn::Sub": "${AWS::StackName}::ClusterSecurityGroupId"
}
}
},
"ClusterStackName": {
"Value": {
"Ref": "AWS::StackName"
}
},
"Endpoint": {
"Value": {
"Fn::GetAtt": [
"ControlPlane",
"Endpoint"
]
},
"Export": {
"Name": {
"Fn::Sub": "${AWS::StackName}::Endpoint"
}
}
},
"FeatureNATMode": {
"Value": "Disable"
},
"SecurityGroup": {
"Value": {
"Ref": "ControlPlaneSecurityGroup"
},
"Export": {
"Name": {
"Fn::Sub": "${AWS::StackName}::SecurityGroup"
}
}
},
"ServiceRoleARN": {
"Value": "arn:aws:iam::680360122082:role/EKS-ControlPlane-Role",
"Export": {
"Name": {
"Fn::Sub": "${AWS::StackName}::ServiceRoleARN"
}
}
},
"SharedNodeSecurityGroup": {
"Value": {
"Ref": "ClusterSharedNodeSecurityGroup"
},
"Export": {
"Name": {
"Fn::Sub": "${AWS::StackName}::SharedNodeSecurityGroup"
}
}
},
"SubnetsPrivate": {
"Value": {
"Fn::Join": [
",",
[
"subnet-0e33694ad3300346b",
"subnet-069692d3fccacfce4"
]
]
},
"Export": {
"Name": {
"Fn::Sub": "${AWS::StackName}::SubnetsPrivate"
}
}
},
"SubnetsPublic": {
"Value": {
"Fn::Join": [
",",
[
"subnet-05a42db2a93a9c392",
"subnet-0ef47cdb576fe9652"
]
]
},
"Export": {
"Name": {
"Fn::Sub": "${AWS::StackName}::SubnetsPublic"
}
}
},
"VPC": {
"Value": "vpc-04b5347f5622b957d",
"Export": {
"Name": {
"Fn::Sub": "${AWS::StackName}::VPC"
}
}
}
}
}