Skip to content
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

Missing access key in Next.js 13 route handlers #3732

Closed
4 tasks done
agazibaric opened this issue Sep 26, 2023 · 5 comments
Closed
4 tasks done

Missing access key in Next.js 13 route handlers #3732

agazibaric opened this issue Sep 26, 2023 · 5 comments
Labels
archived This issue has been locked. compute environment-variables question Further information is requested

Comments

@agazibaric
Copy link

agazibaric commented Sep 26, 2023

Before opening, please confirm:

App Id

d1e9ukt9805kdz

AWS Region

us-west-1

Amplify Hosting feature

Environment variables, Service role

Question

I am facing an issue when attempting to access AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY within a Next.js 13.4.12 with App Router. I'm trying to perform a server-side call to AWS SES inside of Route Handlers. Here's the sample code:

// src/app/api/route.ts

Amplify.configure({ ...awsmobile, ssr: true });

const ses = new aws.SES({
  credentials: {
    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  },
});

export async function POST(request: Request) {
// send email via ses
}

Both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are undefined. I have attached the right SES permissions to the service role of the aws amplify app.

I expect that the Route Handlers assume the role of the service which will provide those credentials inside of env.

How can I obtain those credentials without setting them manually in the build step?

@agazibaric agazibaric added the question Further information is requested label Sep 26, 2023
@Jay2113 Jay2113 self-assigned this Oct 3, 2023
@Jay2113
Copy link
Contributor

Jay2113 commented Oct 3, 2023

Hi @agazibaric 👋 , thanks for raising this!

Have you made the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables accessible to the server-side compute runtime: https://docs.aws.amazon.com/amplify/latest/userguide/ssr-environment-variables.html?

Your build specification file amplify.yml should look something like:

version: 1
frontend:
  phases:
    preBuild:
      commands:
        - npm ci
    build:
      commands:
        - env | grep -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY >> .env.production
        - npm run build
  artifacts:
    baseDirectory: .next
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*
      - .next/cache/**/*

@Jay2113 Jay2113 added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Oct 3, 2023
@agazibaric
Copy link
Author

Hi @Jay2113 , thank you for the answer!

I've tried adding those variables to the build step as you described, but unfortunately, they don't seem to be present there. I've cat the .env.production file and the secrets are missing.

As a potential workaround I could generate access keys and add them to the AWS Amplify Environment as ACCESS_KEY_ID and SECRET_ACCESS_KEY (without the AWS_ prefix, as it's reserved by Amplify).
By exposing these keys in the build step, I can retrieve them on the server side using the following configuration:

version: 1
frontend:
  phases:
    preBuild:
      commands:
        - npm ci
    build:
      commands:
        - env | grep -e ACCESS_KEY_ID -e SECRET_ACCESS_KEY >> .env.production
        - npm run build
  artifacts:
    baseDirectory: .next
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*
      - .next/cache/**/*

However, my goal is to find a solution that doesn't require manual setup of these secrets.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Oct 4, 2023
@Jay2113 Jay2113 removed their assignment Feb 13, 2024
@Jay2113
Copy link
Contributor

Jay2113 commented Feb 22, 2024

@agazibaric 👋 , currently we do not support the functionality of executing server side requests to other AWS services using IAM permissions. We are tracking this as a feature request here #3205. I'll recommend subscribing to it to track updates. Thanks!

@Jay2113 Jay2113 closed this as completed Feb 22, 2024
Copy link

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

Copy link

This issue has been automatically locked.

@github-actions github-actions bot added the archived This issue has been locked. label Feb 22, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Feb 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived This issue has been locked. compute environment-variables question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants