This post is to extend my post about Auto Start and Stop Your EC2 Instance. I put together a CloudFormation template to automate the process to set up the EC2 Operator instance. You can find the CloudFormation template in my github repository.
I am going to go through each section of the CloudFormation template. It consists of four sections:
The Parameters section defines input variables that will be used for creating resources.
- InstanceType – Instance type for the EC2 Operator Instance
- KeyName – Key pair to ssh the EC2 Operator Instance’s console
- SSHLocation – The IP address range that can be used to SSH to the EC2 instance
The Mappings sections define what AMI should be used while launching the EC2 Operator Instance. It is essential a mapping table between regions and AMIs. Each region uses a different AMI. They are just a standard Amazon Linux AMI. If these AMIs are no longer available, please replace them with the latest Amazon Linux AMIs.
The Resources section consists of the resources that will be created:
- SecurityGroup – A security group for the EC2 Operator Instance
- OperatorInstance – The EC2 Operator Instance
- WaitHandle – Wait handle to pair with Wait Condition
- WaitCondition – Set how long to wait for the EC2 Operator Instance to set up
- OperatorInstanceProfile – Instance Profile for the EC2 Operator Instance
- OperatorRole – IAM Role to specify the actions the EC2 Operator Instance can perform
The SecurityGroup resource defines the firewall rules for the EC2 Operator Instance:
- TCP 22 – SSH access
If you are not planning to SSH to the instance, you may want to remove this rule. The source of this rule is taken from the SSHLocation variable you specify in the Parameters section.
It installs python-pip package and gcc package, which are specified in the Metadata section. The following activities are included in the UserData section:
- Interpret metadata
- Install croniter PHP library
- Download the PHP script template
- Add crontab to run the PHP script
- Signal the WaitCondition the EC2 Operator Instance is ready
Wait Handle and Wait Condition
WaitHandle and WaitCondition are always used together. CloudFormation is set to wait 300 seconds for the EC2 Operator Instance to signal ready.
Operator Instance Profile and Operator Role
Instance Profile and IAM Role are always used together. The EC2 Operator Instance needs the following permissions to manipulate instances:
The Outputs section outputs a the instance ID of the newly created EC2 Operator Instance.