2

I have published a Lex bot and a Lambda function on the same region. I am trying to interact with Lex from Lambda using following code.

import boto3 
client = boto3.client('lex-runtime')

def lambda_handler(event, context):
    response = client.post_text(
      botName='string',
      botAlias='string',
      userId='string',
      # sessionAttributes={
      #     'string': 'string'
      # },
      # requestAttributes={
      #     'string': 'string'
      # },
      inputText='entity list'
    )
    return response

While testing the code my lambda function is getting timed out. Kindly let me know if you need to know anything else.

Error Message:

"errorMessage": "2021-04-30T07:09:45.715Z <req_id> Task timed out after 183.10 seconds"
hR 312
  • 824
  • 1
  • 9
  • 22

1 Answers1

2

Lambda in a VPC, including a default VPC, does not have internet access. Since your lambda in a default VPC, it will fail to connect to any lex-runtime AWS endpoint, resulting in a timeout.

Since lex does not support VPC endpoints, the only way for your lambda to access the lex-runtime is:

  • disassociated your function from VPC. If you do this, your function will be able to connect to the internet, and subsequently to lex-runtime.

  • create a private subnet in a default VPC, and setup a NAT gateway. Then place your function in the new subnet. This way your function will be able to connect to the internet using NAT.

More info is in:

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • It is giving following error now "botocore.exceptions.ClientError: An error occurred (AccessDeniedException) when calling the PostText operation: User: is not authorized to perform: lex:PostText on resource: " – hR 312 Apr 30 '21 at 08:05
  • 1
    @hR312 This is a new issue, not related to the question posted. Your function lacks permissions to access lex and perform `lex:PostText` on it. You have to update its execution role. – Marcin Apr 30 '21 at 08:06
  • Thanks "AmazonLexFullAccess" policy will work? – hR 312 Apr 30 '21 at 08:09
  • 1
    @hR312 It should. Its overly permissive, bot for quick test should be ok. I would recommend making new question with current execution role, and detailed error messages if the new issue persists. – Marcin Apr 30 '21 at 08:09
  • 1
    Sure man. Thanks, will do after the role type is changed by admin. – hR 312 Apr 30 '21 at 08:10