soto-project / soto

Swift SDK for AWS that works on Linux, macOS and iOS
https://soto.codes
Apache License 2.0
875 stars 83 forks source link

Unable to connect to DynamoDB using Lambda Runtime #595

Closed YogeshBhattGWL closed 2 years ago

YogeshBhattGWL commented 2 years ago

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):

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 commented 2 years ago

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?

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

@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