Skip to content

Instantly share code, notes, and snippets.

@josefaidt
Created April 26, 2022 15:18
Show Gist options
  • Save josefaidt/e83d8d2b81712c1c579de6449e191c51 to your computer and use it in GitHub Desktop.
Save josefaidt/e83d8d2b81712c1c579de6449e191c51 to your computer and use it in GitHub Desktop.
sends SMS message from Lambda using AWS SDK v3
import { SNSClient, PublishCommand } from '@aws-sdk/client-sns'
const client = new SNSClient()
/**
* Publish a message to a phone number
* @param {string} number - phone number of recipient
* @param {string} [message] - message to send to phone number
* @returns {import('@aws-sdk/client-sns').PublishCommandOutput}
*/
async function publish(number, message) {
/** @type {import('@aws-sdk/client-sns').PublishCommandInput} */
const input = {
PhoneNumber: number || '+5555555555',
Message: message || 'SMS from Lambda!',
}
/** @type {import('@aws-sdk/client-sns').PublishCommand} */
const command = new PublishCommand(input)
const response = await client.send(command)
return response
}
/**
* @type {import('@types/aws-lambda').APIGatewayProxyHandler}
*/
export async function handler(event) {
const { number, message } = event?.body || {}
const response = await publish(number, message)
return {
statusCode: 200,
body: JSON.stringify(response),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment