AWS 인프라에 대해 코드로 선언하는 방법이다.
미리 코드로 적어둔 AWS 인프라 자원을 생성하거나 삭제할 수 있다.
비슷한 서비스로는 Terraform이 존재하고 이를 통틀어 IaC (Infrastructure as Code) 라고 부른다.
생성할 인프라 자원을 코드로 정의한 파일
JSON, YAML 형식을 사용할 수 있다.
Parameters:
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the instances. Linked to AWS Parameter
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: must be the name of an existing EC2 KeyPair.
LatestAmiId:
Description: (DO NOT CHANGE)
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'
AllowedValues:
- /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
Resources:
MyInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !Ref LatestAmiId
InstanceType: t2.micro
KeyName: !Ref KeyName
Tags:
- Key: Name
Value: WebServer
SecurityGroups:
- !Ref MySG
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
yum install httpd -y
systemctl start httpd && systemctl enable httpd
echo "<h1>Test Web Server</h1>" > /var/www/html/index.html
MySG:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable HTTP access via port 80 and SSH access via port 22
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
위에 코드를 test_lab1.yaml 형식으로 저장하고 다음 단계를 진행