Parameters:
InstanceTypeParameter:
Type: String
Description: Enter t2.micro, m1.small. Default is t2.micro
Default: t2.micro
AllowedValues:
- t2.micro
- m1.small
VpcId:
Ref: MyVPC # 방법 1
VpcId: !Ref MyVPC # 방법 2
Mappings:
RegionMap:
us-east-1:
HVM64: ami-0ff8a91507f77f867
HVMG2: ami-0a584ac55a7631c0c
us-west-1:
HVM64: ami-0bdb828fd58c52235
HVMG2: ami-066ee5fd4a9ef77f1
Resources:
myEC2Instance:
Type: "AWS::EC2::Instance"
Properties:
ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", HVM64]
InstanceType: m1.small
Outputs:
StackSSHSecurityGroup:
Value: !Ref MyCompanyWideSSHSecurityGroup
Export:
Name: SSHSecurityGroup # 다른 스택에서 참조할 이름
---
Resources:
MySecureInstance:
Type: AWS::EC2::Instance
Properties:
...
SecurityGroups:
- !ImportValue: SSHSecurityGroup
Conditions:
CreateProdResources: !Equals [ !Ref EnvType, prod ]
Resources:
MonutPoint:
Type: "AWS::EC2::VolumeAttachment"
Condition: CreateProdResources
# Name: www.example.com/path
Name: !Sub
- 'www.${Domain}/${Path}'
- Domain: example.com
Path: home
Name: !Sub 'arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:vpc/${vpc}'
UserData:
Fn::Base64:
!Sub |
#!/bin/bash -xe
yum update -y aws-cfn-bootstrap
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource LaunchConfig --configsets wordpress_install --region ${AWS::Region}
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource WebServerGroup --region ${AWS::Region}
Resources:
MyInstance:
Type: AWS::EC2::Instance
Properties:
UserData:
Fn::Base64:
!Sub |
#!/bin/bash -xe
yum update aws-cfn-bootstrap
/opt/aws/bin/cfn-init -s ${AWS:StackId} -r MyInstance --region ${AWS::Region} || error_exit 'Failed to run cfn-init'
Metadata:
AWS::CloudFormation::Init:
config:
packages:
yum:
httpd: [] # yum install httpd
files:
"/var/www/html/index.html":
content: |
<h1> Hello world from EC2 Instance! </h1>
<p> This was created using cfn-init </p>
mode: '000644'
commands:
hello:
commnad: "echo 'Hello World'"
cwd: "~"
services:
sysvinit:
httpd:
enabled: 'true' # service enable httpd
ensureRunning: 'true' # service start httpd
Resources:
MyInstance: ...
# MyInstance Resource의 Base64에 아래 명령을 추가한다.
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource MyInstance --region ${AWS::Region}
SampleWaitCondition:
DependsOn: MyInstance # MyInstance가 생성 완료되어야 이 리소스를 생성 시작한다.
CreationPolicy: # 아래 조건을 만족해야 이 리소스가 생성 완료된다.
ResourceSignal: # cfn-signal을 기다린다.
Timeout: PT2M # 2분 동안 기다린다.
Type: AWS:CloudFormation::WaitCondition
Resources:
myStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: https://...
Parameters: # 해당 템플릿에서 요구하는 파라미터들
...