# How to implement aws\_iam authorization with postgresql

**URL:** <https://discourse.sst.dev/t/how-to-implement-aws-iam-authorization-with-postgresql/2015>\
**Category:** General\
**Created:** [August 10, 2020, 4:52am UTC](https://discourse.sst.dev/t/how-to-implement-aws-iam-authorization-with-postgresql/2015 "2020-08-10T04:52:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![samgj18](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/samgj18/32/965_2.png) [@samgj18](https://discourse.sst.dev/u/samgj18)\
**Post date:** [August 10, 2020, 4:52am UTC](https://discourse.sst.dev/t/how-to-implement-aws-iam-authorization-with-postgresql/2015/1 "2020-08-10T04:52:49Z")

</div>

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);
  }
});
```

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [August 23, 2020, 11:50pm UTC](https://discourse.sst.dev/t/how-to-implement-aws-iam-authorization-with-postgresql/2015/2 "2020-08-23T23:50:18Z")

</div>

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:

> <https://github.com/AnomalyInnovations/serverless-stack-demo-api/blob/master/serverless.yml#L31>

---

<div class="post-metadata">

**Author:** ![samgj18](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/samgj18/32/965_2.png) [@samgj18](https://discourse.sst.dev/u/samgj18)\
**Post date:** [August 24, 2020, 12:13am UTC](https://discourse.sst.dev/t/how-to-implement-aws-iam-authorization-with-postgresql/2015/3 "2020-08-24T00:13:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [September 7, 2020, 11:27pm UTC](https://discourse.sst.dev/t/how-to-implement-aws-iam-authorization-with-postgresql/2015/4 "2020-09-07T23:27:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![samgj18](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/samgj18/32/965_2.png) [@samgj18](https://discourse.sst.dev/u/samgj18)\
**Post date:** [September 7, 2020, 11:43pm UTC](https://discourse.sst.dev/t/how-to-implement-aws-iam-authorization-with-postgresql/2015/5 "2020-09-07T23:43:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [October 15, 2020, 6:32pm UTC](https://discourse.sst.dev/t/how-to-implement-aws-iam-authorization-with-postgresql/2015/6 "2020-10-15T18:32:07Z")

</div>

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