I'm using aws-sdk to list all the running EC2 instances whose IAM Role is The_Name_of_My_IAM_Role.
const AWS = require('aws-sdk')
let credentials = new AWS.SharedIniFileCredentials({
profile: 'my_profile'
})
AWS.config.credentials = credentials
AWS.config.update({
region: 'ap-northeast-1'
})
const ec2 = new AWS.EC2()
let params = {
Filters: [
{
Name: 'iam-instance-profile.arn',
Values: [`arn:aws:iam::123456789123:instance-profile/The_Name_of_My_IAM_Role`]
},
{
Name: 'instance-state-name',
Values: ['running']
}
]
}
ec2.describeInstances(params, (err, data) => {
if (err) {
console.log(`describeInstances error: ${err}`)
} else {
console.log(`data.Reservations.length: ${data.Reservations.length}`)
}
})
I expect the code to return 6 EC2 instances. But it returns only 4 of them.
The problem doesn't occur if I type aws ec2 describe-instances --filters "Name=iam-instance-profile.arn,Values=arn:aws:iam::123456789123:instance-profile/The_Name_of_IAM_Role" "Name=instance-state-name,Values=running" command in my terminal.
I mean aws ec2 describe-instances ... command returns all 6 EC2 instances.
I've set the following environment variables before running aws ec2 describe-instances ... command.
export AWS_DEFAULT_REGION=ap-northeast-1
export AWS_DEFAULT_PROFILE=my_profile
I also have my_profile defined in ~/.aws/credentials file.
What might be wrong my node.js code?
Or is this a bug of aws-sdk?