Cooking with Chef: How to deploy instance on AWS with knife

Today I want to try provisioning and configuring Amazon AWS instances with Chef. To do that I have:

  • Free tier account on AWS
  • Open Source Chef Server 12
  • Ubuntu Workstation with knife

 

1. First of all we need to install knife ec2 on Ubuntu Workstation:

gem install knife-ec2

Afert that I configured knife.rb at /home/vovando/chef-repo/.chef

————————————————————————————

knife[:aws_access_key_id] = "Abababababbb"
knife[:aws_secret_access_key] = "Blablablaaaaaaaaa"
# AWS Region
knife[:region] = "us-west-2"
————————————————————————————

You can find or create aws_access_key_id  and aws_secret_access_key here:

 

billing

akeys

create keys

 

Let's test our connection to AWS:

sudo knife ec2 server list

server list

 

Deleting the server

sudo knife ec2 server delete i-d8892c11

delete aws

Terminated.

terminated

 

Creating a server

You’ll need:

  • the ID of the subnet you intend to deploy into
  • the AMI you wish to use
  • the security group IDs for the new instance

    (one of these must contain a rule which allows inbound SSH access from the SSH gateway)

  • ssh key

 

If you have all that, you can use the following command:

$ sudo knife ec2 server create \
–flavor t2.micro  \
–image ami-e7527ed7 \
–ebs-size 8 \
–security-group-ids sg-6e0adc0a \
–subnet subnet-575ccd20 \
–ssh-key noteits \
–server-connect-attribute public_ip_address \
–ssh-user ec2-user \
–ssh-port 22 \
–identity-file /home/vovando/noteits.pem \
–node-name "blog.vovando.dev" \
–tags Name="blog.vovando.dev",Environment="Production" \
–run-list "role[web_server]" \
–environment prod

 

voila

 

In AWS console:

aws inst

 

Bootstrapping a server

If you already have a server in the AWS, you can also bootstrap it using the SSH:

$ sudo knife bootstrap 52.27.26.166 \
  –ssh-user ec2-user \
  –sudo \
  –identity-file /home/vovando/noteits.pem \
  –environment production \
  –node-name blog.vovando.dev \
  –run-list 'role[web_server]'
[sudo] password for vovando:

Creating new client for blog.vovando.dev
Creating new node for blog.vovando.dev
Connecting to 52.27.26.166
52.27.26.166 —–> Existing Chef installation detected
52.27.26.166 Starting first Chef Client run…
52.27.26.166 Starting Chef Client, version 12.4.1

And that’s all there is to it!