ec2
.createVpc(params, function (err, data) {
if (err) {
console.log(err, err.stack);
// an error occurred
} else {
logger.log.info(
`Deployment VPC ${deployment.name} has been created.`
);
db.saveData(data, deployment.name);
return data;
}
})
.promise()
I was trying to run this code above, which is the most simplest resource in the docs. But when I added .promise() the create operation was triggered twice. When I remove it, it creates only one instance of VPC. But I need to access the information about the created resource in order to save it to the database.
I would guess that this happens because you mix two ways of triggering a request - providing a callback and calling a promise. If you want to use promises, you should process the returned data in then() (or use await).
Example with then():
ec2.createVpc(params).promise().then((data) => {
logger.log.info(
`Deployment VPC ${deployment.name} has been created.`
);
db.saveData(data, deployment.name);
return data;
}, (err) => {
console.log(err, err.stack);
})