Reputation: 3
I am trying to build a small vue.js app that uses aws-sdk to get all the Lightsial instances. However, I keep getting this error.
:8081/#/:1 Access to XMLHttpRequest at 'https://lightsail.us-west-2.amazonaws.com/' from origin 'http://localhost:8081' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Here is my vue component script
import { AmplifyEventBus } from 'aws-amplify-vue'
import { components } from 'aws-amplify-vue'
import AWS from 'aws-sdk'
import Lightsail from 'aws-sdk/clients/lightsail'
import Auth from '@aws-amplify/auth';
import awsconfig from '../aws-exports';
export default {
components: {
AWS, Lightsail
},
data() {
return {
}
},
mounted() {
var myCredentials =
{
accessKeyId : '***************',
secretAccessKey : '****************'
}
AWS.config.update({
credentials: myCredentials, region: 'us-west-2'
});
var lightsail = new AWS.Lightsail();
lightsail.getInstances(function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
}
}
I am also using aws-amplify-vue in this app for user authentication
Upvotes: 0
Views: 1055
Reputation: 1232
It simply because current release of <aws-sdk-js> does not allows CORS for Lightsail API.
The official document informs that clearly.
EDIT: A completely wrong answer written by another user has been deleted after I added a comment to that. So I removed the words "Right answer" from my answer. Thanks for your quick action.
Upvotes: 1
Reputation: 33
Based on my research, it should be a CORS problem. Here is a similar thread in which some workaround are mentioned. Could your go to check if it works in your scenario?
Upvotes: 1