aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.5k stars 1.17k forks source link

Cannot connect to elasticache on local invoke for lambda #4211

Closed HAK-CODE closed 2 years ago

HAK-CODE commented 2 years ago

Environment

Details I have bastion connected via this command

ssh -i "dummy.pem" -f -N -L 6379:dummy-elastic-cache.ee9ell.ng.0001.use2.cache.amazonaws.com:6379 ec2-user@someip

I am able to use all redis command and fetch all keys in remote cloud, same my lambda is working correctly on cloud sub as soon I try to execute via debugger vscode launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "aws-sam",
            "request": "direct-invoke",
            "name": "api-data-services:TimeseriesServiceData",
            "invokeTarget": {
                "target": "template",
                "templatePath": "${workspaceFolder}/template.yaml",
                "logicalId": "TimeseriesServiceData"
            },
            "lambda": {
                "environmentVariables": {
                    "AWS_ACCOUNT_ID": "12345678910",
                    "EC_ENDPOINT": "127.0.0.1",
                    "EC_PORT": "6379",
                    "EC_READER_ENDPOINT": "127.0.0.1",
                    "EC_READER_PORT": "6379"
                }
            }
        }
    ]
}

As soon code reaches the part to GET key I got this error

{
  "errorMessage": "Error 111 connecting to 127.0.0.1:6379. Connection refused.",
  "errorType": "GetParameterError",
  "requestId": "5317391e-6a68-4c9f-9ede-4e6ec70c7b3e",
  "stackTrace": [
    "  File \"/opt/python/aws_lambda_powertools/logging/logger.py\", line 354, in decorate\n    return lambda_handler(event, context, *args, **kwargs)\n",
    "  File \"/var/task/data/main.py\", line 25, in lambda_handler\n    return app.resolve(event, context)\n",
    "  File \"/opt/python/aws_lambda_powertools/event_handler/api_gateway.py\", line 509, in resolve\n    return self._resolve().build(self.current_event, self._cors)\n",
    "  File \"/opt/python/aws_lambda_powertools/event_handler/api_gateway.py\", line 571, in _resolve\n    return self._call_route(route, match_results.groupdict())  # pass fn args\n",
    "  File \"/opt/python/aws_lambda_powertools/event_handler/api_gateway.py\", line 625, in _call_route\n    return ResponseBuilder(self._to_response(route.func(**args)), route)\n",
    "  File \"/var/task/data/routers/data.py\", line 46, in get_aggregation_data\n    cache_response = lambda_utils.get_hash_key(hash_key, resource_handler)\n",
    "  File \"/var/task/lambda_utils/lambda_utils.py\", line 28, in get_hash_key\n    return resource_handler.get_string(key)\n",
    "  File \"/opt/python/utils/resource_negotiator.py\", line 71, in get_string\n    response = self.store.get(\n",
    "  File \"/opt/python/aws_lambda_powertools/utilities/parameters/base.py\", line 105, in get\n    raise GetParameterError(str(exc))\n"
  ]
}

Is there something I am missing some kind port forwarding etc

HAK-CODE commented 2 years ago

I am able to resolve this once your connection with bastion is there use docker.for.mac.localhost is on MAC as your host

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "aws-sam",
            "request": "direct-invoke",
            "name": "api-data-services:TimeseriesServiceData",
            "invokeTarget": {
                "target": "template",
                "templatePath": "${workspaceFolder}/template.yaml",
                "logicalId": "TimeseriesServiceData"
            },
            "lambda": {
                "environmentVariables": {
                    "AWS_ACCOUNT_ID": "12345678910",
                    "EC_ENDPOINT": "docker.for.mac.localhost",
                    "EC_PORT": "6379",
                    "EC_READER_ENDPOINT": "docker.for.mac.localhost",
                    "EC_READER_PORT": "6379"
                }
            }
        }
    ]
}