Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to connect to DynamoDB using Lambda Runtime #595

Closed
YogeshBhattGWL opened this issue May 20, 2022 · 5 comments
Closed

Unable to connect to DynamoDB using Lambda Runtime #595

YogeshBhattGWL opened this issue May 20, 2022 · 5 comments

Comments

@YogeshBhattGWL
Copy link

YogeshBhattGWL commented May 20, 2022

I am unable to fetch Data from DynamoDB with Lambda Runtime using Local Testing.

init(context: Lambda.InitializationContext) {
       let awsclient = AWSClient(
           credentialProvider: .static(
               accessKeyId: "...",
               secretAccessKey: "...."
           ),
           retryPolicy: .default,
           httpClientProvider: .createNewWithEventLoopGroup(context.eventLoop)
       )

       let dynamoDB = DynamoDB(client: awsclient, region: .apsouth1, partition: .aws, endpoint: "https://dynamodb.ap-south-1.amazonaws.com", timeout: .seconds(10), byteBufferAllocator: ByteBufferAllocator(), options: [])
       self.dbManager = DynamoDBManager(database: dynamoDB)
   }

To Reproduce
Steps to reproduce the behavior:

  1. Take clone from project Here.
  2. Add the Initialization code I added in above code.
  3. When Hitting API I am getting error in Postman: {"errorType":"FunctionError","errorMessage":"ResourceNotFoundException: Requested resource not found"} and I Xcode console I am getting error: warning Lambda : lifecycleIteration=0 lambda handler returned an error: ResourceNotFoundException: Requested resource not found

Expected behavior
When hitting API I should get data from DynamoDB.

Setup (please complete the following information):

  • OS: MacOS 12.3.1
  • Version of soto: 5.7.0
  • Authentication mechanism [hard-coded credentials]

Additional context

  1. I have checked my Region and its proper.
  2. My Database table located in region I provided.
  3. I think this somewhat region or credentials issue but don’t t know whats missing.
@adam-fowler
Copy link
Member

I removed your accessKeyId and secretAccessKey from your text. It is not a good idea to publish them. It might be a good idea to rotate them now.

Sorry here are a load of questions first?

  • What do you mean by Local Testing? Do you mean running the lambda on your Mac?
  • Can you access the dynamoDB resource using the awscli using the same authentication details?
  • What DynamoDB function are you calling?
  • Have you setup your lambda execution role to give you access to DynamoDB?

If you create your service as follows

dynamoDB = DynamoDB(client: AWSClient).with(middlewares:[AWSLoggingMiddleware()])

it will output all the HTTP requests made to AWS which might give you an idea where things are going wrong.

Instead of using static credentials I would use environmental credentials. This will pickup the credentials the lambda is running with and the execution role will define what AWS assets you can access. Although when you run locally you will need to setup the environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. If you don't explicitly set the DynamoDB service region it will use the region the lambda is running in but again when running locally you will need to set the environment variable AWS_DEFAULT_REGION

@YogeshBhattGWL
Copy link
Author

YogeshBhattGWL commented May 23, 2022

Thanks a lot @adam-fowler.
Your answer was really helpful and I found the issue with me and fixed it.

Reason of Issue: Actually my TableName was wrong.

I can't believe this bcz I created table with name: ToDoItems using Amplify but by default Amplify created Table with name ToDoItems-3f4asanedaneku32kaqy-dev. and so I was trying to access ToDoItems.

Note:- As AWS Cli was not setup in my system so I setup and found the error of Requested resource not found This got me clicked for table name.

@YogeshBhattGWL
Copy link
Author

Hi @adam-fowler.
I am facing problem for running Lambda from AWS Server.
Whenever I hit Live URL I am getting error: Internal Server Error

I uploaded same project zip to AWS and checked in cloudWatch and got this error:


2022-05-24T07:08:36.344Z	undefined	ERROR	Uncaught Exception 	{
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module 'index'\nRequire stack:\n- /var/runtime/index.mjs",
    "stack": [
        "Runtime.ImportModuleError: Error: Cannot find module 'index'",
        "Require stack:",
        "- /var/runtime/index.mjs",
        "    at _loadUserApp (file:///var/runtime/index.mjs:726:17)",
        "    at async Object.module.exports.load (file:///var/runtime/index.mjs:741:21)",
        "    at async file:///var/runtime/index.mjs:781:15",
        "    at async file:///var/runtime/index.mjs:4:1"
    ]
}

I checked my permissions in Lambda and its all good as The error doesn't describe about anything about permission issue.
Could you Please suggest me how to track and fix issue.

@adam-fowler
Copy link
Member

adam-fowler commented May 24, 2022

That looks like a setup issue. It thinks your lambda is node.js.

It's worthwhile reading this blog to make sure you have everything setup correctly https://fabianfett.dev/getting-started-with-swift-aws-lambda-runtime. Especially Step 7 onwards

@YogeshBhattGWL
Copy link
Author

@adam-fowler Thnx for solution but this was not helpful bcz I wanted to fix something about setup in AWS and i fixed it.

Solution: Make sure you have selected Custom runtime on Amazon Linux 2. instead of Node.js in Runtime Setting.

Screenshot 2022-05-24 at 2 11 27 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants