I need invoke an AWS Lambda from my view in Vue.js without AWS API Gateway
1 Answer
First, include aws-sdk in you index.html (something like that):
nameproject/public/index.html
Include this src:
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.866.0.min.js"></script>
Second, in your file.vue (something like that):
nameproject/src/views/myfile.vue
Include this code:
<script
import AWS from 'aws-sdk'
const { Lambda } = require("@aws-sdk/client-lambda");
export default {
props: {
},
computed: {
},
components: {
},
methods: {
startCallBack: function() {
},
endCallBack: function() {
},
async invokeLambdaFunction(){
var config = new AWS.Config({
region: 'us-east-1',
accessKeyId: '**YOUR_ACCESS_KEY**',
secretAccessKey: '**YOUR_SECRET_ACCESS_KEY**',
});
var lambda_fun = new AWS.Lambda(config);
var request = {
"var_1" : "0",
"var_2" : "1"
};
var pullParams = {
FunctionName : '**YOUR_FUNCTION_NAME**',
InvocationType : 'RequestResponse',
LogType: 'None',
Payload: JSON.stringify(request)
}
var pullResults;
lambda_fun.invoke(pullParams, function (error, data) {
if(error)
console.log(error);
else{
pullResults = JSON.parse(data.Payload);
console.log('returned result: ', JSON.stringify(pullResults, null, 2))
}
}
)
}
},
data() {
return {
};
},
created() {
this.invokeLambdaFunction();
}
};
</script>
Then you can see the response from Lambda.
1 Comment
LLai
I just want to add that this is NOT recommended as you are exposing your aws secret on the client side. Maybe you can lock down the IAM permission based on your client domain? But still I'd prefer either invoking the lambda from a backend or using API gateway.