# Accessing Knife via Ruby

**URL:** <https://discourse.chef.io/t/accessing-knife-via-ruby/2302>\
**Category:** Chef Infra (archive)\
**Created:** [December 29, 2011, 7:20pm UTC](https://discourse.chef.io/t/accessing-knife-via-ruby/2302 "2011-12-29T19:20:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zuhaib\_Siddique](https://avatars.discourse-cdn.com/v4/letter/z/d9b06d/32.png) [@Zuhaib\_Siddique](https://discourse.chef.io/u/Zuhaib_Siddique)\
**Post date:** [December 29, 2011, 7:20pm UTC](https://discourse.chef.io/t/accessing-knife-via-ruby/2302/1 "2011-12-29T19:20:47Z")

</div>

Hey,

So I am writing a ruby script that will help with our deployments and as part of that I am leveraging the Chef REST API which is great but with some of that information I want to be able to pass that over to knife to do deletes. An example code is the following:

require 'chef/knife/ec2\_server\_delete’  
delete\_ec2 = Chef:🔪:Ec2ServerDelete.new()  
delete\_ec2.name\_args = node.ec2.instance\_id  
delete\_ec2.run

This works but I get the following error:

ERROR: You did not provided a valid ‘AWS Access Key Id’ value.  
ERROR: You did not provided a valid ‘AWS Secret Access Key’ value.

But both are set and work correctly from the command line. Also If I do this using the knife exec command it works. I assume I am missing something in my ruby but I am just pulling a blank and can’t seem to find any hints from looking at the chef/knife source code. Any pointers would be great.

Thanks  
Zuhaib

---

<div class="post-metadata">

**Author:** ![coderanger](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/coderanger/32/5_2.png) [@coderanger](https://discourse.chef.io/u/coderanger)\
**Post date:** [December 29, 2011, 8:48pm UTC](https://discourse.chef.io/t/accessing-knife-via-ruby/2302/2 "2011-12-29T20:48:10Z")

</div>

On Dec 29, 2011, at 11:20 AM, Zuhaib Siddique wrote:

> Hey,
> 
> So I am writing a ruby script that will help with our deployments and as part of that I am leveraging the Chef REST API which is great but with some of that information I want to be able to pass that over to knife to do deletes. An example code is the following:
> 
> require 'chef/knife/ec2\_server\_delete'  
> delete\_ec2 = Chef:🔪:Ec2ServerDelete.new()  
> delete\_ec2.name\_args = node.ec2.instance\_id  
> delete\_ec2.run
> 
> This works but I get the following error:
> 
> ERROR: You did not provided a valid 'AWS Access Key Id' value.  
> ERROR: You did not provided a valid 'AWS Secret Access Key' value.
> 
> But both are set and work correctly from the command line. Also If I do this using the knife exec command it works. I assume I am missing something in my ruby but I am just pulling a blank and can't seem to find any hints from looking at the chef/knife source code. Any pointers would be great.

There isn't really any reason to try and use Knife like this, just use Fog directly for a much better API.

--Noah

---

<div class="post-metadata">

**Author:** ![Zuhaib\_Siddique](https://avatars.discourse-cdn.com/v4/letter/z/d9b06d/32.png) [@Zuhaib\_Siddique](https://discourse.chef.io/u/Zuhaib_Siddique)\
**Post date:** [December 29, 2011, 10:25pm UTC](https://discourse.chef.io/t/accessing-knife-via-ruby/2302/3 "2011-12-29T22:25:12Z")

</div>

Noah,

True but I just want to simplify things by using knife and accessing the keys. I guess I could just load the knife.rb and parse out the information.

Zuhaib  
On Dec 29, 2011, at 12:48 PM, Noah Kantrowitz wrote:

> On Dec 29, 2011, at 11:20 AM, Zuhaib Siddique wrote:
> 
> > Hey,
> > 
> > So I am writing a ruby script that will help with our deployments and as part of that I am leveraging the Chef REST API which is great but with some of that information I want to be able to pass that over to knife to do deletes. An example code is the following:
> > 
> > require 'chef/knife/ec2\_server\_delete'  
> > delete\_ec2 = Chef:🔪:Ec2ServerDelete.new()  
> > delete\_ec2.name\_args = node.ec2.instance\_id  
> > delete\_ec2.run
> > 
> > This works but I get the following error:
> > 
> > ERROR: You did not provided a valid 'AWS Access Key Id' value.  
> > ERROR: You did not provided a valid 'AWS Secret Access Key' value.
> > 
> > But both are set and work correctly from the command line. Also If I do this using the knife exec command it works. I assume I am missing something in my ruby but I am just pulling a blank and can't seem to find any hints from looking at the chef/knife source code. Any pointers would be great.
> 
> There isn't really any reason to try and use Knife like this, just use Fog directly for a much better API.
> 
> --Noah
