Skip to content

Instantly share code, notes, and snippets.

@ariesmcrae
Last active July 11, 2021 12:00
Show Gist options
  • Save ariesmcrae/e7e12f43c60906bcfc5adca75762c84c to your computer and use it in GitHub Desktop.
Save ariesmcrae/e7e12f43c60906bcfc5adca75762c84c to your computer and use it in GitHub Desktop.
NodeJS AWS SDK for JavaScript v2 SNS: publishing a message from your local laptop when behind your corporate firewall
// I was getting this error when publishing sns from my local laptop:
// arn:aws:sns:ap-southeast-2:396497070286:int-afl-sns-topic
// Inaccessible host: `sns.ap-southeast-2.amazonaws.com'.
// This service may not be available in the `ap-southeast-2' region.",
// "code":"UnknownEndpoint",
// "region":"ap-southeast-2",
// "hostname":"sns.ap-southeast-2.amazonaws.com"
// getaddrinfo ENOTFOUND sns.ap-southeast-2.amazonaws.com
// This is the solution
import AWS from 'aws-sdk'
import { SNS } from 'aws-sdk'
const proxy = require('proxy-agent')
const sns = new AWS.SNS({
apiVersion: '2010-03-31',
region: 'ap-southeast-2',
httpOptions: { agent: proxy('http://myAbcCompanyInternal.proxy.com') },
})
const snsTopicArn = 'arn:aws:sns:ap-southeast-2:XXXYYY:dev-myApp-sns-topic'
const publishEvent = async (message) => {
const params = {
TopicArn: snsTopicArn,
Message: JSON.stringify(message),
}
try {
const snsResponse = await sns.publish(params).promise()
console.debug(`My SNS Response: ${JSON.stringify(snsResponse)}`)
} catch (e) {
console.error(`My SNS exception: Error publishing message to SNS topic: ${snsTopicArn}`, e)
throw e
}
}
// This solution didn't work for me:
const sns = new AWS.SNS({ apiVersion: '2010-03-31' })
AWS.config.update({
region: 'ap-southeast-2',
httpOptions: {
agent: proxy('http://myAbcCompanyInternal.proxy.com'),
},
})
// Credits:
// https://github.com/aws/aws-sdk-js/issues/2053
/////////////////////////////////////////
// This solution would have worked as well. Just put AWS.config.update(...) before new AWS.SNS(...)
AWS.config.update({
region: 'ap-southeast-2',
httpOptions: {
agent: proxy('http://myAbcCompanyInternal.proxy.com'),
},
})
const sns = new AWS.SNS({ apiVersion: '2010-03-31' })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment