简单的aws监听API

目录结构

主目录simple

  1. —— main.go

代码示例

main.go

  1. package main
  2. import (
  3. "time"
  4. "github.com/kataras/iris"
  5. "github.com/aws/aws-sdk-go/aws"
  6. "github.com/aws/aws-sdk-go/service/cloudwatch"
  7. cw "github.com/iris-contrib/middleware/cloudwatch"
  8. )
  9. // $ go get github.com/aws/aws-sdk-go/...
  10. // $ go run main.go
  11. func main() {
  12. app := iris.New()
  13. app.Use(cw.New("us-east-1", "test").ServeHTTP)
  14. app.Get("/", func(ctx iris.Context) {
  15. put := cw.GetPutFunc(ctx)
  16. put([]*cloudwatch.MetricDatum{
  17. {
  18. MetricName: aws.String("MyMetric"),
  19. Dimensions: []*cloudwatch.Dimension{
  20. {
  21. Name: aws.String("ThingOne"),
  22. Value: aws.String("something"),
  23. },
  24. {
  25. Name: aws.String("ThingTwo"),
  26. Value: aws.String("other"),
  27. },
  28. },
  29. Timestamp: aws.Time(time.Now()),
  30. Unit: aws.String("Count"),
  31. Value: aws.Float64(42),
  32. },
  33. })
  34. ctx.StatusCode(iris.StatusOK)
  35. ctx.Text("success!\n")
  36. })
  37. // http://localhost:8080
  38. //应该给:NoCredentialProviders
  39. //这是正确的,你必须授权你的aws,我们认为你知道如何。
  40. app.Run(iris.Addr(":8080"))
  41. }