How to implement aws_iam authorization with postgresql

The way the tutorial uses the authorization is like this since they’re using DynamoDB:

export const main = handler(async (event, context) => {
  const data = JSON.parse(event.body);
  const params = {
TableName: process.env.tableName,
// 'Item' contains the attributes of the item to be created
// - 'userId': user identities are federated through the
//             Cognito Identity Pool, we will use the identity id
//             as the user id of the authenticated user
// - 'noteId': a unique uuid
// - 'content': parsed from request body
// - 'attachment': parsed from request body
// - 'createdAt': current Unix timestamp
Item: {
  userId: event.requestContext.identity.cognitoIdentityId,
  noteId: uuid.v1(),
  content: data.content,
  attachment: data.attachment,
  createdAt: Date.now()
}
  };

  await dynamoDb.put(params);

  return params.Item;
});

So the parameters are passed to the dynamoDb instance and it take cares of the authorization but how can I do the same using a postgres database? Currently my code looks like this:

export const getOne = handler(async (event, context) => {
  context.callbackWaitsForEmptyEventLoop = false;

  try {
    const { User } = await connectToDatabase();
    const user = await User.findOne({
      where: { enterprise_email: event.queryStringParameters.email },
    });
    if (!user)
      throw new Error(`User with id: ${event.queryStringParameters.email} was not found`);
    return user;
  } catch (err) {
    throw ('Could not fetch the user.', err);
  }
});

Hmm there might be some confusion here, that code doesn’t do any authorization for DynamoDB. It is done through the serverless.yml. This shows which tables our Lambda functions have access to:

Thanks a lot Jayair, I got that a little later. Didn’t update the post. I’m still confused on how to do the same with postgresql though

Are you trying to allow your Lambda functions to access your Postgres databases?

1 Like

Hi Jayair, thanks for your time. I already solved the problem. I was a little confused but took my time to read the docs and the tutorial and it now works like charm.

1 Like

Awesome! It would be great if you could share it for the rest of the community!