-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IAM permission with NextJS #3205
Comments
cc @hloriana |
@calavera the service role is the one used in the edge lambda functions? Do i need to add the permissions mentioned here: https://github.com/aws-amplify/amplify-hosting/blob/main/FAQ.md#error-accessdenied-access-denied |
You don't need those permissions anymore if you're deploying Next 13. That documentation is outdated, unfortunately. We need to update that. |
Thank you for letting me know! I assume those are not longer needed due to the "Trust relationship" which is added? {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "amplify.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
} Correct? so the "custom" role needs those |
correct. The custom role needs that trust relationship. |
No success yet. // create iam role for amplify
const role = new iam.Role(this, 'AmplifyRole', {
assumedBy: new iam.ServicePrincipal('amplify.amazonaws.com'),
});
// add permissions to write logs
role.addToPolicy(new iam.PolicyStatement({
actions: ['logs:CreateLogStream','logs:CreateLogGroup','logs:DescribeLogGroups','logs:PutLogEvents'],
resources: ['*'],
}));
// add permissions to create users in table
table.grantReadWriteData(role) which gets successfully created and and permissions seems to be correct. But then inside my Amplify NextJS app I get the following error Could not load credentials from any providers {
--
message: 'Could not load credentials from any providers',
stack: 'CredentialsProviderError: Could not load credentials from any providers\n' +
' at /var/task/node_modules/@aws-sdk/credential-provider-node/dist-cjs/defaultProvider.js:13:11\n' +
' at /var/task/node_modules/@aws-sdk/property-provider/dist-cjs/chain.js:11:28\n' +
' at processTicksAndRejections (node:internal/process/task_queues:96:5)\n' +
' at async coalesceProvider (/var/task/node_modules/@aws-sdk/property-provider/dist-cjs/memoize.js:14:24)\n' +
' at async SignatureV4.credentialProvider (/var/task/node_modules/@aws-sdk/property-provider/dist-cjs/memoize.js:33:24)\n' +
' at async SignatureV4.signRequest (/var/task/node_modules/@aws-sdk/signature-v4/dist-cjs/SignatureV4.js:86:29)\n' +
' at async /var/task/node_modules/@aws-sdk/middleware-signing/dist-cjs/middleware.js:16:18\n' +
' at async /var/task/node_modules/@aws-sdk/middleware-retry/dist-cjs/retryMiddleware.js:27:46\n' +
' at async /var/task/node_modules/@aws-sdk/middleware-logger/dist-cjs/loggerMiddleware.js:5:22\n' +
' at async getUserByAccount (/var/task/node_modules/@next-auth/dynamodb-adapter/dist/index.js:59:26)',
name: 'CredentialsProviderError'
} I am trying to use the const config: DynamoDBClientConfig = {
region: process.env.NEXT_AUTH_AWS_REGION,
};
const client = DynamoDBDocument.from(new DynamoDB(config), {
marshallOptions: {
convertEmptyValues: true,
removeUndefinedValues: true,
convertClassInstanceToMap: true,
},
}) the repository is public as well: https://github.com/philschmid/aws-marketplace-example/blob/main/app/pages/api/auth/%5B...nextauth%5D.ts#L8 |
I've been reviewing this with the team, and it might actually not work, and I was wrong 🤦 The role that you setup in our Console is not the same role that the function gets execution credentials from. So the permissions won't be propagated as expected. So the only solution for now is to inject the credentials in the environment. We're going to look if we can prioritize this work in the near future, but we cannot make any commitments at the moment. |
Thank you for the response! Too bad it is not working. Adding secrets as an environment doesn't sound super secure or something you should do. I hope you ll find time to add it. |
@philschmid may be a dumb question but, did you import the DynamoDB Table with Or did you create a new DynamoDB Table with |
@williamrjribeiro i am not using the |
I shall +1 this. To the dot, the exact same problem is happening, forcing the team to potentially "hardcode" credentials. |
+1 Thank you! |
+1 |
Same problem here, is there a plan to fix this by any chance? |
+1, seems a pretty basic thing to have. |
I know this is not the best solution, but we can generate .env file with IAM user credential in Build process as workaround:
build:
commands:
- env | sed "s/DUMMY_PREFIX_//g" >> .env.production
- yarn run build |
+1 Currently, as mentioned in the above comment, we cannot find a way other than embedding the access key and secret access key in the environment variables and build of amplify. |
+1 currently using |
This works, but would be cool to not expose my credentials in the Amplify console |
Having the same issues here as well |
Any update on this? |
Oh my. Finally found this |
@sfedorov-at-wiley i switched to sst. https://docs.sst.dev There you have full control since you will create a real lambda function which is not abstracted. |
Or if you don’t like SST you can also use CDK with OpenNext https://github.com/jetbridge/cdk-nextjs |
Following this thread. |
I had the same issue where credentials were stored as env variables. The way I worked around the issue is to store the credentials in AWS secrets manager, and creating an env variable which just holds the secret id . The secret access policy was set to allow 'read' access only to the amplify pipeline service role, so that no one else can see the credentials. During the build, the amplify pipeline fetches those credentials and adds them to .env |
would be great not to create an IAM user just to have for the Access and Secret Key. is there any updates on this? |
I've been trying to solve this exact same issue for quite some time now and have landed here. From what I have read, it seems like there is no proper way to grant IAM permissions to the server side of my NextJS app hosted in Amplify. This is definitely something that is absolutely crucial to have. |
July 2024: Still annoying developers! |
From the docs here: https://docs.aws.amazon.com/amplify/latest/userguide/ssr-environment-variables.html I have these set:
After checking the logs we see: CredentialsProviderError: Could not load credentials from any providers |
This is critical. We've been trying to transition away from persistent credentials and IAM users for a long time. It's surprising to me that a modern, serverless Amazon service would encourage (require?) the use of long term credentials. There should be an execution role that the NextJS app uses when it runs. I'm too invested to move my current project away from Amplify, but I won't recommend that my company uses Amplify again until this problem is solved. I know AWS likes to say that "security is job #1." That means that this should be a top priority. |
Bump |
Hello guys, I faced a similar situation and here was my solution. The solution suits my case but I think I may fit your side or anybody else, and I'm happy if it can help. Regarding the link, it requires you to attach credentials to any AWS-SDK you want to use rather than using the local AWS config. For example, I was using "@aws-sdk/client-iot" and it should be like this
In my case, a user should log in to the app and use the services inside so I don't need to set my AWS config by env or secret params. Just create a user pool and manage policies on it. |
I was open to injecting the secrets as env variables using the ssm cli during build, but that didn't seem to work as well (secrets json is empty for me even though I have them set up in the console) |
Have you tried this approach? Until we get IAM support this is the best way I found with Amplify and NextJS. It is a deal breaker for some projects for sure, but it does work. Just mind what you expose on server and client side, on the console etc. |
Hello - this issue has now been moved to in-development! We are looking at a solution where you would create an IAM role and then have the ability to attach that role within the Amplify Hosting console. I'm curious what types of roles/policies folks want to attach? What is the use case? What will the policies do? Thank you! |
For me, one role/policy that I would like to attach is dynamoDB access. This would allow the serverless app to modify the DB (without forcing env variables) upon verified conditions. |
Hello - Various AWS services require secrets or roles/policies in order to
prevent use by unintended parties. My first use case was to restrict
access to SES for sending emails from a NextJS site hosted on Amplify
Console (not CLI). The best way for me to control the access was secrets,
but hooking them up via a connection to Secrets Manager was not possible. I
think an IAM role would be great because that is a recommended means of
restricting access to AWS resources and granting access only to specific
ARNs would effectively restrict access to resources. Certainly there are
other use cases involving other AWS resources.
…On Tue, Sep 24, 2024 at 9:52 AM Matt Auerbach ***@***.***> wrote:
Hello - this issue has now been moved to in-development! We are looking at
a solution where you would create an IAM role and then have the ability to
attach that role within the Amplify Hosting console.
I'm curious what types of roles/policies folks want to attach? What is the
use case? What will the policies do?
Thank you!
—
Reply to this email directly, view it on GitHub
<#3205 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AU6QAULWB7ETV53DL4UNJUTZYGDFVAVCNFSM6AAAAAATAF4E56VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNZRGY4TENBSGI>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
I am currently injecting an access key via env vars so that my next app can talk to DynamoDB which is currently managing sessions (lucia/arctic). |
One more question for feedback, please -- Would folks want the IAM permissions to apply at the branch or app level? meaning, if app level all branches would have access to the same IAM role credentials |
To us, app level would be sufficient, we deploy one app per each stage (dev, beta, etc). But no harm to have more granular (branch level) control, if the configuration is easy to understand and to deploy. |
We currently do the same. |
Hi Matt! We prefer it to be on a branch level since we have some long-lived branches for testing and internal use. This would enable us to assign different permissions to those branches. :) |
Thank you, everyone. It looks like we are going with an App level IAM role with the ability for branch level overrides :) |
Is there an ETA for this? If it will take some time (which is ok), I will outsource all credential stuff into an lambda function behind API. If the fix is expected to be production ready in weeks, I will plan differently. Btw my use case: Need SES from Svelte SSR to send some email and would really prefer not to use long term credentials. But exposing SES via a public API is probably also a bad idea :/ |
I'm facing the same problem, would appreciate any updates |
Hello, I implemented the following workaround in my Amplify build settings to extract AWS credentials from the ~/.aws/credentials file, and it works, I can access now AWS services (dynamodb) within my NextJS app :
PS: you need to make sure to use the aws session token as Amplify uses temporary credentials |
Hi @meryemben92 |
This works immediately after an deployment. But I don't think this is a solution. The token get expired after sometime. |
Another potential workaround, is to create a IAM User with approriate permission set (for example in my case, i want to access Secrets Manager, so I assigned SecretsManagerReadAndWrite). After, you create AccessKey for that user, and inject those to nexjts app using Amplify |
having the same problem here! please any update how to fix it using roles and note hardcoding secrets in the env variables? |
@JoseSpx
|
Before opening, please confirm:
App Id
AWS Region
us-east-1
Amplify Hosting feature
SSR
Question
Hello,
I have a question regarding IAM permissions. I successfully deployed NextJS 13 using amplify. Now, I want to use AWS service via the
aws
javascript SDK in myapi/
functions. How can i provide credentials in a secure way to it? I know that i could create a user and pass theaccessKeyId
andsecretAccessKey
as env, but i would like to avoid this.How can I assign a role to my NextJs app?
The text was updated successfully, but these errors were encountered: