cfn-init
- The script must be called from user data
- takes in stack id, region, instance
- You’ll need to give
AWS::Cloudformation::Initcontent in the metadata section
---
Resources:
MyInstance:
Type: AWS::EC2::Instance
Properties:
AvailabilityZone: us-east-1a
ImageId: ami-0a3c3a20c09d6f377
InstanceType: t2.micro
SecurityGroups:
- !Ref SSHSecurityGroup
# we install our web server with user data
UserData:
Fn::Base64:
!Sub |
#!/bin/bash -xe
# Get the latest CloudFormation package
dnf update -y aws-cfn-bootstrap
# Start cfn-init
/opt/aws/bin/cfn-init -s ${AWS::StackId} -r MyInstance --region ${AWS::Region} || error_exit 'Failed to run cfn-init'
Metadata:
Comment: Install a simple Apache HTTP page
AWS::CloudFormation::Init:
config:
packages:
yum:
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:
command: "echo 'hello world'"
services:
sysvinit:
httpd:
enabled: 'true'
ensureRunning: 'true'
# our EC2 security group
SSHSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: SSH and HTTP
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
FromPort: 22
IpProtocol: tcp
ToPort: 22
- CidrIp: 0.0.0.0/0
FromPort: 80
IpProtocol: tcp
ToPort: 80
cfn-Signal
- Need to be invoked in the user-data script
- Takes in stack name, resource as wait condition, region and an exit code which you pass the previous commands exit code or anything of your wish
- Needs a resource of type
AWS::CloudFormation::Waticonditionwith appropirate properties - There are two things for reporting
-
Waitcondition
- resource singaling
-
---
Resources:
MyInstance:
Type: AWS::EC2::Instance
Properties:
AvailabilityZone: us-east-1a
ImageId: ami-0a3c3a20c09d6f377
InstanceType: t2.micro
SecurityGroups:
- !Ref SSHSecurityGroup
# we install our web server with user data
UserData:
Fn::Base64:
!Sub |
#!/bin/bash -x
# Get the latest CloudFormation package
dnf update -y aws-cfn-bootstrap
# Initialize EC2 Instance
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource MyInstance --region ${AWS::Region}
# Get result of last command
INIT_STATUS=$?
# send result back using cfn-signal
/opt/aws/bin/cfn-signal -e $INIT_STATUS --stack ${AWS::StackName} --resource SampleWaitCondition --region ${AWS::Region}
# exit the script
exit $INIT_STATUS
Metadata:
Comment: Install a simple Apache HTTP page
AWS::CloudFormation::Init:
config:
packages:
yum:
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:
command: "echo 'boom' && exit 1"
services:
sysvinit:
httpd:
enabled: 'true'
ensureRunning: 'true'
SampleWaitCondition:
CreationPolicy:
ResourceSignal:
Timeout: PT3M
Count: 1
Type: AWS::CloudFormation::WaitCondition
# our EC2 security group
SSHSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: SSH and HTTP
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
FromPort: 22
IpProtocol: tcp
ToPort: 22
- CidrIp: 0.0.0.0/0
FromPort: 80
IpProtocol: tcp
ToPort: 80