Search code examples
amazon-web-servicesamazon-ec2environment-variablesnext.js13production-environment

Storing environment variables and secrets on AWS EC2


I have a next.js app which I deployed on EC2. I wish to add some api keys and secrets. I've added some in the next.config.js file but it seems that didn't work.

Here is my next.config.js file:

module.exports = {
  env: {
    NEXT_PUBLIC_BASE_URL: 'https://djangobackend.com/api/articles/',
    NEXT_PUBLIC_COMMENT_URL: 'https://djangobackend.com/api/articles/comment/create/',
    NEXT_PUBLIC_COMMENT_LIST_URL: 'https://djangobackend.com/api/articles/comment/',
    NEXT_PUBLIC_CONTACT_URL: 'https://djangobackend.com/contact/',
},
}

I acceess it in my code this way:

const regURL = processs.env.NEXT_PUBLIC_BASE_URL

What is the best approach to store this variables and other sensitive secrets in EC2


Solution

  • As you have mentioned some secrets available in next.config.js file so I am assuming this file is version controlled in github/bitbucket etc. This is very bad practice. We should never ever put secrets in code. In AWS you can use either Secrets Manager or SSM Parameter Store service for storing sensitive data. In your code you just need to mention secret name and build it and in runtime secrets will be available