https://pulumi.com logo
#aws
Title
# aws
n

narrow-leather-14091

08/03/2023, 7:53 AM
Hi guys, I started to use Pulumi yesterday and created a few AWS lambda functions (node.js). The question: Is it possible somehow to use a debugger with breakpoints in IDE ?
l

little-cartoon-10569

08/03/2023, 8:36 PM
For the lambda functions? They're not run by Pulumi, they're run by AWS. For the Pulumi project? Yes, it's almost a normal program, your debugger should be able to attach to it. It works fine in nodejs.
n

narrow-leather-14091

08/04/2023, 7:50 AM
Yes for the lambda functions. For example, AWS SAM allows debugging lambda functions via IDE. And I was looking maybe some way to make similar with Pulumi or a workaround
j

jolly-megabyte-6233

08/04/2023, 8:21 AM
Are you testing/debugging only the function or is it backing an API that is serving a frontend?
n

narrow-leather-14091

08/04/2023, 10:30 AM
@jolly-megabyte-6233 API Gateway calls the lambda functions. To have the opportunity set breakpoints and work with the classic debugger. It will make work with some complicated Lambda functions much more comfortable, and easier to understand how each line works under the hood.
j

jolly-megabyte-6233

08/04/2023, 12:50 PM
If you're just concerned with testing/debugging the code itself and not with validating the integration with the infrastructure that triggers the lambda, you can simply call the function handler locally. My preferred mode is to call the handler directly against the actual infrastructure on a test environment. (In case of a gateway that's defined through serverless, the AWS SAM setup simply provides a wrapper to call something resembling the actual gateway via HTTP calls to localhost. But the HTTP request will be translated to a plain function call in the end. And these can be easily tested and debugged using unit tests or similar setups. Also note that the AWS SAM setup is not a full replication of an actual gateway that would be deployed on AWS and so succesful tests on this setup are not a guarantee that code will work in the live environment).
n

narrow-leather-14091

08/04/2023, 2:35 PM
@jolly-megabyte-6233 what if a lambda takes data from the DynamoDB table? In the unit test, I can mock it, but I in debug mode, it will throw an exception because it is also an integration part, and even with AWS SAM it also is not possible in debug mode to check the response from DynamoDB. Is it correct? Or is it possible to have a connection to DynamoDB from a local machine?
j

jolly-megabyte-6233

08/06/2023, 6:29 AM
is it possible to have a connection to DynamoDB from a local machine?
@narrow-leather-14091 yes it's totally possible. the local machine needs to configure credentials for an AWS user/role that has access to the resource (any AWS resource, not only DynamoDB). all access is ultimately governed by IAM policies and as long as the user/role has (at least) the same permissions as your lambda, you can just run the lambda code locally (you'll need to define the table name, region, etc. one way to do this is via environment variables). (It can be more complicated if the resources are in a VPC.)
n

narrow-leather-14091

08/08/2023, 6:20 AM
@jolly-megabyte-6233 Can you please share an example? I tried to run the lambda function locally, but it throws an error when DynamoDB is trying to get data by request.
j

jolly-megabyte-6233

08/08/2023, 10:02 AM
could you share the error? i'll see if i have some code somewhere later this day
n

narrow-leather-14091

08/08/2023, 10:43 AM
@jolly-megabyte-6233 error: "Error read item from DynamoDB ResourceNotFoundException: Requested resource not found"
@jolly-megabyte-6233 Thank you for your help, I just want to stay with Pulumi, don't want to go to SAM. Sometimes life without a debugger is like a headache
image.png
when I try to put instead of table name it's arn (long key arnawsdynamodb:eu-*****) I have this error Error read item from DynamoDB ValidationException: 1 validation error detected: Value 'arnawsdynamodb:eu-central-***' at 'tableName' failed to satisfy constraint: Valid ARN format is 'arn&lt;awsPartition&gt;<vendor>&lt;region&gt;<subscriber>:resourceType/resourceName', where Resource name must have length less than or equal to 255, Resource name must have length greater than or equal to 3, Resource name must satisfy regular expression pattern: [a-zA-Z0-9_.-]+
j

jolly-megabyte-6233

08/08/2023, 1:40 PM
"Error read item from DynamoDB ResourceNotFoundException: Requested resource not found"
Here's what i'd do to figure this out: 1. First: Validate that the table actually exsists. log into to the AWS console and go to the dynamo tables and check a. that the table actually exists b. that is actually has the name you think it has c. and that it exists in the correct region 2. Then: Make a minimal working example for a simple PUT request to the table. (The goal is to valiate if your local setup is actually able to interact with your specific table.) a. create a small
tmp-script.js
file that you run via node/ts-node
Copy code
const AWS = require('aws-sdk');

const REGION = 'YOUR_AWS_REGION';
const TABLE = 'YOUR_TABLE_NAME';

const dynamoDb = new AWS.DynamoDB.DocumentClient({ region: REGION });

const script = async () => {
  try {
    const response = await dynamoDb.put({
      TableName: TABLE,
      Item: {
        // ... your item (needs to have the correct primary key)
      }
    }).promise();
    console.log(JSON.stringify(response));
  } catch (error) {
    console.error('Error putting item into DynamoDB', error);
  }
};

script();
Now: This should work. If it does not, it might be that your AWS user is not allowed to access the table. Afterwards, update the code in the handler and make the handler work by running it locally. Afterwards, deploy.
ARN format
Nah. The
TableName
parameter should be the table name - I think DynamoDB tables are uniquely identified via the triple [account, region, name].
Also: For the production code it's best to not hardcode the table name: • get it from an env variable • set the env variable as part of your CI pipeline
n

narrow-leather-14091

08/08/2023, 2:25 PM
@jolly-megabyte-6233 Thank you immensely for your detailed guidance and support in helping me navigate through this way.