# Chef on Amazon Linux

1. First of all create one Linux machine in AWS.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716017702645/e86fe0ac-f586-4d91-9e71-00c80979f5ad.png align="center")
    
2. Connect to Linux EC2 Instance through Putty.
    
    * Download the Private key of the instance using PuttyGen
        
    * Get access to the terminal using the downloaded private key.
        
3. Switch to the root user.
    
    `sudo su`
    
4. Update Server Packages.
    
    `yum update -y`
    
5. Download and Install the chef package
    
    [**packages.chef.io/files/stable/chef-workstat..**](https://packages.chef.io/files/stable/chef-workstation/20.7.96/el/7/chef-workstation-20.7.96-1.el7.x86_64.rpm)
    
    `wget https://packages.chef.io/files/stable/chef-workstation/20.7.96/el/7/chef-workstation-20.7.96-1.el7.x86_64.rpm`
    
6. Verify the chef package and Version.
    
    * to list the downloaded contain
        
        `ls`
        
7. Install the Chef workstation
    
    for Amazon Linux - [**chef-workstation-23.3.1030-1.el7.x86\_64.rpm**](https://packages.chef.io/files/stable/chef-workstation/23.3.1030/amazon/2/chef-workstation-23.3.1030-1.el7.x86_64.rpm)
    
    `yum install <workstation-name>`
    
8. Verify the version
    
    `chef-v`
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716017938734/914e70d3-2619-4619-9bc1-13c5b94d3d5e.png align="center")

### How to create a cookbook ?

Once we installed the Chef workstation, we need to create the directory called Cookbooks, which will have as many cookbooks with recipes.

1. Create a directory called **cookbooks**
    
    `mkdir cookbooks`
    
2. To create a cookbook, go inside the cookbooks directory and generate the cookbook
    
    `cd cookbooks`
    
    Generating the cookbook,
    
    `chef generate cookbook <cookbook-name>`
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716020152190/da2999cf-0171-483a-87dc-9dace02eb949.png align="center")
    
    **Output-** Cookbook created successfully.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716020252977/a72f968f-fbb8-48b8-a194-17f8863de9c2.png align="center")
    
3. To create the recipes we have to go inside created cookbook directory and generate the cookbook.
    
    `cd cookbook-name`
    
    Generating the recipe,
    
    `chef generate recipe <recipe-name>`
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716020282711/21a0a382-570b-4426-92b7-c5de087dd4d5.png align="center")
    
    **Output-** Recipe created successfully.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716020356723/7c5404a5-4d48-4ece-a009-e2c5a89c2fdf.png align="center")
    
4. To build recipes go to the cookbooks directory. Open the recipe using `vi` command.
    
    `vi cookbook-name/recipes/<recipe-name.rb>`
    
5. Now create a file using the Chef recipe, by editing the recipe.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716020406373/b82a10ce-384a-4cd6-8848-bad474cc3c25.png align="center")
    
    * Press **i** to enter text in file.
        
    * Press **esc**, and type **:wq** to save and exit the file.
        
6. To check Ruby scripting, with output syntax Ok or Not,
    
    `chef exec ruby -c cookbook-name/recipes/<recipe-name.rb>`
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716020452034/184c3cff-bd54-4ee0-88ae-04b5593b1307.png align="center")
    
7. Execute the recipe
    
    `chef-client -zr "recipe[cookbook-name::recipe-name]"`
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716020491000/20778401-99f5-49aa-b1a0-ed3052985bf1.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716020505749/4f927b6c-6730-413e-8499-dcd2762c3106.png align="center")
    
    **Output-** myfile is created with the content.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716020570842/45fa2360-af95-4b3e-8937-b5ac2a64361a.png align="center")
