Skip to content

Commit

Permalink
Improve configuration of aws credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
francoispluchino committed Aug 16, 2022
1 parent 03d5264 commit 9a1ac3d
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 20 deletions.
12 changes: 10 additions & 2 deletions bin/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ let availableRegions = null;
let getAvailableRegions = async function () {
if (null === availableRegions) {
availableRegions = [];
let ec2 = new AWS.EC2({apiVersion: '2016-11-15', region: 'us-west-1'});
let ec2 = new AWS.EC2({apiVersion: '2016-11-15', region: 'us-west-1', credentials: {accessKeyId: envs.AWS_ACCESS_KEY_ID, secretAccessKey: envs.AWS_SECRET_ACCESS_KEY}});
let res = await ec2.describeRegions({}).promise().catch(utils.displayError);

if (undefined !== res.Regions) {
Expand Down Expand Up @@ -161,7 +161,15 @@ if (program.interaction) {
choices: function() {
return getAvailableRegions();
},
when: function () {
when: function (answers) {
let answerEnvs = {
'AWS_PROFILE': answers.awsProfile,
'AWS_ACCESS_KEY_ID': answers.awsAccessKeyId,
'AWS_SECRET_ACCESS_KEY': answers.awsSecretAccessKey,
};
let awsEnvs = utils.findAwsVariables(answerEnvs);
envs = utils.mergeVariables(answerEnvs, awsEnvs, envs);

return utils.showOnlyEmptyOption(program, envs, 'AWS_REGION');
},
validate: function (value) {
Expand Down
6 changes: 3 additions & 3 deletions bin/create-bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ program
utils.spawn('node bin/config -e')
.then(() => {
console.info('Creation of the AWS S3 bucket is started...');
let s3 = new AWS.S3({apiVersion: '2006-03-01', region: env['AWS_REGION']});
let s3 = new AWS.S3({apiVersion: '2006-03-01', region: env.AWS_REGION, credentials: {accessKeyId: env.AWS_ACCESS_KEY_ID, secretAccessKey: env.AWS_SECRET_ACCESS_KEY}});

return s3.createBucket({Bucket: env['AWS_S3_BUCKET_DEPLOY']}).promise();
return s3.createBucket({Bucket: env.AWS_S3_BUCKET_DEPLOY}).promise();
})
.then(() => {
console.info(`AWS S3 bucket "${env['AWS_S3_BUCKET_DEPLOY']}" was created with successfully in the "${env['AWS_REGION']}" region`);
console.info(`AWS S3 bucket "${env.AWS_S3_BUCKET_DEPLOY}" was created with successfully in the "${env.AWS_REGION}" region`);
})
.catch(utils.displayError);
6 changes: 3 additions & 3 deletions bin/delete-bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ program
utils.spawn('node bin/config -e')
.then(() => {
console.info('Deletion of the S3 bucket is started...');
let s3 = new AWS.S3({apiVersion: '2006-03-01', region: env['AWS_REGION']});
let s3 = new AWS.S3({apiVersion: '2006-03-01', region: env.AWS_REGION, credentials: {accessKeyId: env.AWS_ACCESS_KEY_ID, secretAccessKey: env.AWS_SECRET_ACCESS_KEY}});

return s3.deleteBucket({Bucket: env['AWS_S3_BUCKET_DEPLOY']}).promise();
return s3.deleteBucket({Bucket: env.AWS_S3_BUCKET_DEPLOY}).promise();
})
.then(() => {
console.info(`AWS S3 bucket "${env['AWS_S3_BUCKET_DEPLOY']}" was deleted with successfully`);
console.info(`AWS S3 bucket "${env.AWS_S3_BUCKET_DEPLOY}" was deleted with successfully`);
})
.catch(utils.displayError);
6 changes: 3 additions & 3 deletions bin/delete-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ program
utils.spawn('node bin/config -e')
.then(() => {
console.info('Deletion of the AWS Cloud Formation stack is started...');
let cf = new AWS.CloudFormation({apiVersion: '2010-05-15', region: env['AWS_REGION']});
let cf = new AWS.CloudFormation({apiVersion: '2010-05-15', region: env.AWS_REGION, credentials: {accessKeyId: env.AWS_ACCESS_KEY_ID, secretAccessKey: env.AWS_SECRET_ACCESS_KEY}});

return cf.deleteStack({StackName: env['AWS_STACK_NAME']}).promise();
return cf.deleteStack({StackName: env.AWS_STACK_NAME}).promise();
})
.then(() => {
console.info(`AWS Cloud Formation stack "${env['AWS_STACK_NAME']}" was queued for the deletion with successfully`);
console.info(`AWS Cloud Formation stack "${env.AWS_STACK_NAME}" was queued for the deletion with successfully`);
})
.catch(utils.displayError);
6 changes: 3 additions & 3 deletions bin/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ program
.description('Deploy the packaged project in AWS Cloud Formation')
.parse(process.argv);

let stackName = env['AWS_STACK_NAME'];
let cf = new AWS.CloudFormation({apiVersion: '2010-05-15', region: env['AWS_REGION']});
let stackName = env.AWS_STACK_NAME;
let cf = new AWS.CloudFormation({apiVersion: '2010-05-15', region: env.AWS_REGION, credentials: {accessKeyId: env.AWS_ACCESS_KEY_ID, secretAccessKey: env.AWS_SECRET_ACCESS_KEY}});

let createAction = function(action) {
let changeName = stackName + '-' + utils.generateId(12);
Expand Down Expand Up @@ -53,7 +53,7 @@ let createAction = function(action) {
if ('CREATE_COMPLETE' === resDesc.Status) {
cf.executeChangeSet({ChangeSetName: changeName, StackName: stackName}).promise()
.then(() => {
console.info(`AWS Cloud Formation stack "${env['AWS_STACK_NAME']}" was queued for the ${'UPDATE' === action ? 'update' : 'creation'} with successfully`);
console.info(`AWS Cloud Formation stack "${env.AWS_STACK_NAME}" was queued for the ${'UPDATE' === action ? 'update' : 'creation'} with successfully`);
done();
resolve();
}).catch(reject);
Expand Down
4 changes: 2 additions & 2 deletions bin/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ utils.spawn('node bin/build' + (program.force ? ' --force' : ''))
return newPath;
})
.then(async (filePath) => {
let s3 = new AWS.S3({apiVersion: '2006-03-01', region: env.AWS_REGION});
let s3 = new AWS.S3({apiVersion: '2006-03-01', region: env.AWS_REGION, credentials: {accessKeyId: env.AWS_ACCESS_KEY_ID, secretAccessKey: env.AWS_SECRET_ACCESS_KEY}});
let fileStream = fs.createReadStream(filePath);
fileStream.on('error', utils.displayError);

Expand Down Expand Up @@ -94,7 +94,7 @@ utils.spawn('node bin/build' + (program.force ? ' --force' : ''))
fs.writeFileSync(BUILD_CLOUDFORMATION_PATH, data);

if (program.tag || program.replace) {
let s3 = new AWS.S3({apiVersion: '2006-03-01', region: env.AWS_REGION});
let s3 = new AWS.S3({apiVersion: '2006-03-01', region: env.AWS_REGION, credentials: {accessKeyId: env.AWS_ACCESS_KEY_ID, secretAccessKey: env.AWS_SECRET_ACCESS_KEY}});
let fileStream = fs.createReadStream(BUILD_CLOUDFORMATION_PATH);
fileStream.on('error', utils.displayError);

Expand Down
6 changes: 5 additions & 1 deletion bin/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ utils.spawn('node bin/config -e')
let db = new AWS.DynamoDB({
apiVersion: '2012-08-10',
region: env.AWS_REGION,
endpoint: env.AWS_DYNAMODB_URL ? env.AWS_DYNAMODB_URL : undefined
endpoint: env.AWS_DYNAMODB_URL ? env.AWS_DYNAMODB_URL : undefined,
credentials: {
accessKeyId: env.AWS_ACCESS_KEY_ID,
secretAccessKey: env.AWS_SECRET_ACCESS_KEY,
},
});

try {
Expand Down
4 changes: 2 additions & 2 deletions bin/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ program
utils.spawn('node bin/config')
.then(async () => {
const env = require('./utils/env').loadEnvs();
let s3 = new AWS.S3({apiVersion: '2006-03-01', region: env['AWS_REGION']});
let s3 = new AWS.S3({apiVersion: '2006-03-01', region: env.AWS_REGION, credentials: {accessKeyId: env.AWS_ACCESS_KEY_ID, secretAccessKey: env.AWS_SECRET_ACCESS_KEY}});

try {
await s3.getBucketLocation({Bucket: env['AWS_S3_BUCKET_DEPLOY']}).promise();
await s3.getBucketLocation({Bucket: env.AWS_S3_BUCKET_DEPLOY}).promise();
} catch (e) {
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion bin/utils/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ module.exports.getEndpoint = async function getEndpoint(program) {
}

try {
let cf = new AWS.CloudFormation({apiVersion: '2010-05-15'});
const env = require('./../utils/env').loadEnvs();
let cf = new AWS.CloudFormation({apiVersion: '2010-05-15', credentials: {accessKeyId: env.AWS_ACCESS_KEY_ID, secretAccessKey: env.AWS_SECRET_ACCESS_KEY}});
let resources = await cf.describeStackResources({StackName: process.env.AWS_STACK_NAME, LogicalResourceId: 'Endpoint'}).promise();

if (resources.StackResources.length > 0 && 'AWS::ApiGateway::RestApi' === resources.StackResources[0].ResourceType) {
Expand Down

0 comments on commit 9a1ac3d

Please sign in to comment.