SnsPublish.go - AWS Code Sample


SnsPublish.go -m MESSAGE -t TOPIC-ARN sends MESSAGE to all of the users subscribed to the SNS topic with the ARN TOPIC-ARN.

/* Copyright 2010-2020, Inc. or its affiliates. All Rights Reserved. This file is licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package main import ( "" "" "flag" "fmt" "os" ) func main() { msgPtr := flag.String("m", "", "The message to send to the subscribed users of the topic") topicPtr := flag.String("t", "", "The ARN of the topic to which the user subscribes") flag.Parse() if *msgPtr == "" || *topicPtr == "" { fmt.Println("You must supply a message and topic ARN") fmt.Println("Usage: go run SnsPublish.go -m MESSAGE -t TOPIC-ARN") os.Exit(1) } // Initialize a session that the SDK will use to load // credentials from the shared credentials file. (~/.aws/credentials). sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) svc := sns.New(sess) result, err := svc.Publish(&sns.PublishInput{ Message: msgPtr, TopicArn: topicPtr, }) if err != nil { fmt.Println(err.Error()) os.Exit(1) } fmt.Println(*result.MessageId) }

Sample Details

Service: sns

Last tested: 2020-1-6

Author: Doug-AWS

Type: full-example