Using JSON templates we describe the resources needed from aws. With this approach, we don't have to repeat the same manual configuration every time.
1. Setup IAM policies and roles
2. Setup EC2 instance
3. Setup Security groups
4. Configure EC2 like updating packages, setting up Nginx, uwsgi etc..
Steps 1, 2, and 3 are concerned with aws resources. CloudFormation is helpful to automate these 3 steps, so instead of configuring manually each and every time we can automate to grab the aws resources by providing a JSON template.
CloudFormation is different from Elastic Beanstalk, elastic beanstalk automates step 4 like shell scripts, ansible scripts etc..CloudFormation takes care of aws resources like how many instances we need, security groups etc..
Creating CloudFormation Template:
Manually creating a cloud formation template is complex, so CloudForm is used to simplify this task.
Before using cloud form make sure that the resources that you need are running, it means if I am going to create a template to deploy a web application then I need have my ec2 instance up and running, a security group, vpc, subnet already being created for my ec2 instance and also an IAM policy that will use for my app.
CloudForm is thus a template creating tool from existing resources.
Creating Template with CloudForm:
To run cloud form go to aws console and get into CloudFormation, here at the bottom select CloudForm.
CloudForm template creation tool runs on t2.medium instance so be careful about cost while running cloud form. Remember to stop CloudForm(t2.medium) after creating your template.
Launching CloudForm starts running the CloudForm template, the sample CloudForm template looks like:
So When you run CloudForm you will be directed through the steps for selecting required available resources, this includes storage, compute, security etc..Since we need EC2 t2.micro instance and a simple security group which allows ssh and http on their default ports, we are going to make sure that we select only these.
1. Select Region
2. Network Interfaces, select vpc and subnet, make sure that these two are in same regions.
4. Compute: select your desired ec2 instance, if nothing is displayed then probably you don't have any instances running.
5. Security groups: A list of available security groups will be listed, check the appropriate one which allows ssh and http.
6. Save the template to s3 for future usage. Just provide the bucket you want to save this template.
Launch the stack(template):
Select the template and launch it. You can view the stats for debugging. A CREATE_COMPLETE message will be displayed if everything goes well
Finally, your aws environment is setup, now you can run your scripts to deploy the app.
Next time when we have to configure the same setup or a little-modified one we can simply reuse this template and run it. Within minutes we will have our resources ready.
Beware of the cost of using CloudForm, also CloudForm isn't yet completely ready, it is in its beta state.