{"id":452,"date":"2015-10-27T01:33:54","date_gmt":"2015-10-27T01:33:54","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/2015\/10\/27\/provision-a-aws-ec2-vm-using-chef\/"},"modified":"2021-11-19T06:33:31","modified_gmt":"2021-11-19T06:33:31","slug":"provision-a-aws-ec2-vm-using-chef","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/provision-a-aws-ec2-vm-using-chef\/","title":{"rendered":"Provision a AWS ec2 vm using chef | Step by Step Guide | AWS ec2 vm Tutorial"},"content":{"rendered":"<p>Provision a AWS ec2 vm using chef<\/p>\n<p><strong>Step 1: <a href=\"tutorials\/how-to-install-chefdk-in-rhel-ubantu-mac-and-windows\" target=\"_blank\" rel=\"noopener noreferrer\">Install chefdk<\/a><\/strong><\/p>\n<p><strong>Step 2: <a href=\"tutorials\/setup-aws-credentails-using-aws-command-line-interface\" target=\"_blank\" rel=\"noopener noreferrer\">Setup AWS Credentails<\/a><\/strong><\/p>\n<p><strong>Step X: <a href=\"tutorials\/configure-the-knife-command\" target=\"_blank\" rel=\"noopener noreferrer\">Setup your knife config<\/a><\/strong><\/p>\n<p>Step X: Make sure following is set and exported in env.<\/p>\n<pre style=\"margin-top: 0px; margin-bottom: 2.1em; padding: 0.8em 1em; border: 1px solid #05232b; font-stretch: inherit; line-height: 1.45em; font-family: Menlo, Monaco, 'Andale Mono', 'lucida console', 'Courier New', monospace; font-size: 13px; vertical-align: baseline; box-shadow: rgba(0, 0, 0, 0.0588235) 0px 0px 10px; border-radius: 0.4em; color: #93a1a1; overflow: auto; background: url('http:\/\/jtimberman.housepub.org\/images\/noise.png?1423629076') 0% 0% #002b36;\"><code style=\"margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Menlo, Monaco, 'Andale Mono', 'lucida console', 'Courier New', monospace; vertical-align: baseline;\">AWS_ACCESS_KEY_ID=secrets AWS_SECRET_ACCESS_KEY=secrets AWS_DEFAULT_REGION=us-east-1 AWS_SSH_KEY=your_ssh_key_name AWS_ACCESS_KEY=secrets AWS_SECRET_KEY=secrets<\/code><\/pre>\n<pre style=\"margin-top: 0px; margin-bottom: 2.1em; padding: 0.8em 1em; border: 1px solid #05232b; font-stretch: inherit; line-height: 1.45em; font-family: Menlo, Monaco, 'Andale Mono', 'lucida console', 'Courier New', monospace; font-size: 13px; vertical-align: baseline; box-shadow: rgba(0, 0, 0, 0.0588235) 0px 0px 10px; border-radius: 0.4em; color: #93a1a1; overflow: auto; background: url('http:\/\/jtimberman.housepub.org\/images\/noise.png?1423629076') 0% 0% #002b36;\"><code style=\"margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Menlo, Monaco, 'Andale Mono', 'lucida console', 'Courier New', monospace; vertical-align: baseline;\"><\/code><\/pre>\n<p><strong>Step 3: Genrate a new repository using the chef generate command<\/strong><strong><a href=\"scm\/configuration-management-tools\/setup-aws-credentails-using-aws-command-line-interface.html\" target=\"_blank\" rel=\"noopener noreferrer\"><br>\n<\/a><\/strong><\/p>\n<p>&gt; chef generate repo chefdk-provision-demo<br>\n&gt; cd chefdk-provision-demo<\/p>\n<p><strong>Step 4: Generate a provision cookbook. This is the required name, and it must be in the current directory.<\/strong><br>\n&gt; chef generate cookbook provision<\/p>\n<p><strong>Step 5: Edit the default recipe, $EDITOR provision\/recipes\/default.rb with following code&#8230;<\/strong><\/p>\n<pre>context = ChefDK::ProvisioningData.context with_driver 'aws::us-west-2' options = { ssh_username: 'admin', use_private_ip_for_ssh: false, bootstrap_options: { key_name: 'jtimberman', image_id: 'ami-0d5b6c3d', instance_type: 'm3.medium', }, convergence_options: context.convergence_options, } machine context.node_name do machine_options options action context.action converge true end<\/pre>\n<p>Understand the code:<br>\n&gt; To break this down, first we get the ChefDK provisioning context that will pass in options to chef-provisioning.<br>\n&gt; Then we tell chef-provisioning to use the AWS driver, and in the us-west-2 region.<br>\n&gt; The options hash is used to setup the instance.<br>\n&gt; We\u2019re using Debian 8, which uses the admin user to log in, an SSH key that exists in the AWS region, the actual AMI, and finally the instance type.<br>\n&gt; Then, we\u2019re going to set the convergence options automatically from ChefDK. This is the important part that will ensure the node has the right run list.<\/p>\n<p><strong>Step 6: Generate a Policyfile.rb and And edit its content, $EDITOR Policyfile.rb.<\/strong><br>\n&gt; chef generate policyfile<br>\n&gt; vi policyfile.rb<\/p>\n<pre>name            \"chefdk-provision-demo\" default_source  :community run_list        \"recipe[libuuid-user]\" cookbook        \"libuuid-user\"<\/pre>\n<p>Here we\u2019re simply getting the libuuid-user cookbook from Supermarket and applying the default recipe to the nodes that have this policy.<\/p>\n<p><strong>Step 7: The next step is to install the Policyfile. This generates the Policyfile.lock.json, and downloads the cookbooks to the cache, ~\/.chefdk\/cache\/cookbooks. If this isn\u2019t run, chef will complain, with a reminder to run it.<\/strong><\/p>\n<p>&gt; chef install<\/p>\n<p>Step 8: Finally, we can provision a testing system with this policy:<\/p>\n<p>&gt; chef provision testing &#8211;sync -n debian-libuuid<\/p>\n<p><strong>Reference:<\/strong><br>\n<a href=\"http:\/\/jtimberman.housepub.org\/blog\/2015\/05\/15\/quick-tip-chefdk-provision\/\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/jtimberman.housepub.org\/blog\/2015\/05\/15\/quick-tip-chefdk-provision\/<\/a><\/p>\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_99973\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_39527\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_99973\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Provision a AWS ec2 vm using chef Step 1: Install chefdk Step 2: Setup AWS Credentails Step X: Setup your knife config Step X: Make sure following is set and exported in env. AWS_ACCESS_KEY_ID=secrets AWS_SECRET_ACCESS_KEY=secrets AWS_DEFAULT_REGION=us-east-1 AWS_SSH_KEY=your_ssh_key_name AWS_ACCESS_KEY=secrets AWS_SECRET_KEY=secrets Step 3: Genrate a new repository using the chef generate command &gt; chef generate repo chefdk-provision-demo&#8230;<\/p>\n","protected":false},"author":1,"featured_media":3108,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5633],"tags":[162,1399,1400,1404,1405,407,1122,545,1402,593,1397,1401,1403,1406,732,213,968,662],"class_list":["post-452","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aws","tag-aws","tag-aws-ec2","tag-aws-ec2-vm","tag-aws-ec2-vm-guide","tag-aws-ec2-vm-tutorial","tag-chef","tag-guide","tag-how","tag-how-to-provision-a-aws-ec2-vm","tag-process","tag-provision","tag-provision-a-aws-ec2-vm-using-chef","tag-provision-a-aws-ec2-vm-using-chef-tutorials","tag-provisioning-aws-ec2-vm","tag-steps","tag-tutorial","tag-virtual-machine","tag-vm"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/452","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=452"}],"version-history":[{"count":4,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/452\/revisions"}],"predecessor-version":[{"id":25796,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/452\/revisions\/25796"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/3108"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}