2

I am new to AWS and trying to figure out how to call a lambda function using ruby. Could someone provide me a sample code to invoke AWS lambda function using ruby

Thanks

Edit 1 : after trying the code , I am getting the following error

undefined method `match' for nil:NilClass

Here's my code :

require 'aws/decider'
require 'aws-sdk'

class U_Act
extend AWS::Flow::Activities
activity :b_u do
    {
        version: "1.0"
    }
end

def b_u(c_id)
    lambda=Aws::Lambda::Client.new(
    access_key_id: “XxXXXXXXXXX”,
    secret_access_key: “XXXXXXXXXX”
        )
        resp = lambda.invoke(
    function_name: “s_u_1” # required
    )
        print "#{resp}"
end

end

2
  • Have you looked at the AWS Ruby SDK documentation? Is there something inadequate about the example provided in the documentation for the invoke() method? docs.aws.amazon.com/sdkforruby/api/Aws/Lambda/… Commented Jun 30, 2016 at 14:15
  • I went through the documentation . However I could'nt figure out what client is or how to define client . Is client same as lambda (defined in top of page)? Commented Jun 30, 2016 at 14:17

1 Answer 1

5

Here's how you can invoke a lambda function using aws-sdk, remember that you don't need to provide the access_key_id/secret_access_key if you're running on an instance that has an IAM role attached with lambda invocation permission for your function.

require 'aws-sdk'

lambda = Aws::Lambda::Client.new(
  region: 'eu-west-1',
  access_key_id: 'access_key_id'
  secret_access_key: 'secret_access_key'
)
#=> #<Aws::Lambda::Client>

resp = lambda.invoke(function_name: 'function_name')
#=> #<struct Aws::Lambda::Types::InvocationResponse status_code=200, function_error=nil, log_result=nil, payload=#<StringIO:0x00000000000>>
Sign up to request clarification or add additional context in comments.

5 Comments

After trying your code , I am getting the error "undefined method `match' for nil:NilClass ". Here's my code require 'aws/decider' require 'aws-sdk' class U_Act extend AWS::Flow::Activities activity :b_u do { version: "1.0" } end def b_u(c_id) lambda=Aws::Lambda::Client.new( access_key_id: “XxXXXXXXXXX”, secret_access_key: “XXXXXXXXXX” ) resp = lambda.invoke( function_name: “s_u_1” # required ) print "#{resp}" end end
What's the stacktrace? I don't see you specifying a region (such as eu-west-1 in the constructor for the client) at all which is why you're probably getting an error.
Thanks for the reply . I added region and it worked . however I am facing a new issue . I get an error "Reason: An Activity cannot send a response with data larger than 32768 characters. Please limit the size of the response" . Any idea why I could be facing the error ?
That would most likely be simple workflow which is a nightmare to debug. I can't help too much regarding that but I do know that the AWS SWF console might provide additional help to show you what may have happened. In any case, the data you pass in to an activity worker or the response you receive can not be more than 32768 characters. I would debug first using a normal string as input and output and then slowly work your way up identifying what function is causing the issue.
How to specify a version? I don't want the $LATEST in production for example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.