Skip to content

Instantly share code, notes, and snippets.

@413n
Last active September 15, 2023 07:08
Show Gist options
  • Save 413n/ea348c3563851ffd3a2a04ef63be13d5 to your computer and use it in GitHub Desktop.
Save 413n/ea348c3563851ffd3a2a04ef63be13d5 to your computer and use it in GitHub Desktop.
Create AWS Exports for Vercel

Create AWS Exports for Vercel

DISCLAIMER: Unfortunately this method is manual, but eventually needs to be done just the first time you create the Amplify Environment.

You need to have the aws-exports.js file (generated by Amplify) to do this process.

We're gonna use a Environment variable (in my case BASE64_AWS_EXPORTS) that will contain the aws-exports.js file content encoded in base64. The script will then decode it and create the aws-exports.js file.

The package.json needs to be changed so that this script will be created before the build.

I used this website for manually encode the content of the aws-exports.js file.

import fs from "fs";
// Path relative to execution path
const configFilePath = "src/aws-exports.js";
const logPrefix = "[CreateAwsExports]";
function readConfigFile() {
try {
return fs.readFileSync(configFilePath);
} catch (err) {
console.error(
logPrefix,
`PROBABLY NO CONFIG FILE HAS BEEN FOUND! Error occured while reading the config file ${configFilePath}!`,
);
}
}
const currentConfigFile = readConfigFile();
if (currentConfigFile) {
console.log(logPrefix, `File ${configFilePath} found. Stopping script!`);
process.exit(0);
}
console.log(
logPrefix,
`No file found at ${configFilePath}. Getting env with aws-export...`,
);
const base64AwsExportsEnv = process.env.BASE64_AWS_EXPORTS;
if (!base64AwsExportsEnv) {
console.log(
logPrefix,
`No env variable called AWS_EXPORTS has been found. Stopping script!`,
);
process.exit(1);
}
console.log(logPrefix, `Env variable found. Creating config file...`);
const awsExportsBuff = Buffer.from(base64AwsExportsEnv, "base64");
const awsExports = awsExportsBuff.toString("utf-8");
function createConfigFile() {
try {
fs.writeFileSync(configFilePath, awsExports);
return true;
} catch (err) {
console.error(
logPrefix,
`Error occured while writing the config file ${configFilePath}!`,
err,
);
}
}
const createdConfigFile = createConfigFile();
if (!createdConfigFile) {
console.log(logPrefix, `No config file has been created. Stopping script!`);
process.exit(1);
}
console.log(
logPrefix,
`File ${configFilePath} has been created successfuly! Happy deploying!`,
);
"scripts": {
"build": "node path/to/createAwsExports.mjs && next build",
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment